How to convert matroska MKV to AVI on Linux ?

Today, I discover that my M3100 player doesn’t know how to play Mastroka files. Mastroka files are really kool since the subtitles are embeded in .. but that’s it, my player refuse to play open it.

This stuff can be repack in a AVI (xivd..) file with some open source tools. Here, a little script to do the hard stuff ;)

You can find the script here : http://svn.pythonfr.org/public/pythonfr/utils/video/mkv2avi.py

#!/usr/bin/python
# Little script to depack Matroska file, and repack them
# in a AVI + subtitle format.

import sys
import os

def message(msg):
    print "=" * 78
    print "= %s" % msg
    print "=" * 78

def usage():
    print "Mastroka repacker script"
    print "  Usage: "+sys.argv[0]+ " filename"

if __name__ == "__main__":
    if len(sys.argv) < 2:
       usage()
    else:
       filename = sys.argv[1]
       basename = filename[:-4]
       message("Unpacking file: %s" % filename)
       os.system("mkvextract tracks %s 1:temp_video.avi 2:temp_audio.ogg 3:%s.srt" % (filename,basename) )

       message("Repacking file: %s.avi" % basename)
       os.system("ffmpeg -i temp_audio.ogg  -i temp_video.avi  -vcodec copy  %s.avi" % (basename) )

       message("Cleaning files")
       os.system("rm temp_video.avi temp_audio.ogg")

/Enjoy Video

Related posts :

admin May 8th, 2007


27 Responses to “How to convert matroska MKV to AVI on Linux ?”

  1. Anonymouson 10 Jun 2007 at 3:48 pm

    This script depends on package mkvtoolnix

  2. Rafael31on 25 Sep 2007 at 8:18 pm

    i have a problem with this…
    built on Jan 28 2007 22:48:38, gcc: 4.1.2 20070106 (prerelease) (Ubuntu 4.1.1-21ubuntu7)
    temp_audio.ogg: could not find codec parameters
    Cleaning files
    root@desktop:/Potter
    When it finish remove the file .ogg and .avi
    Please.
    what i need?

  3. Descoreon 01 Dec 2007 at 2:57 pm

    Cool script, thanks for that !!!

  4. bretton 08 Jan 2008 at 12:22 am

    Tried the script but then fell foul with the ffmpeg params.

    After checking out the ffmpeg site I found the latest version as of 8/1/2008 supports .mkv:

    ffmpeg -i cobra.mkv -target vcd cobra.avi

  5. Albatrosson 13 Apr 2008 at 5:09 pm

    Thanks for that update, Brett, it was exactly what I needed.

  6. Kevinon 16 Apr 2008 at 8:52 am

    Thanks,

    Very useful…

  7. Keith Hanlanon 19 Apr 2008 at 4:57 am

    I’m new to this so I’m a bit confused. I tried this on a 1.1GB 720p x264 encoding and wound up with a 26MB audio file with no video. The sound quality was seriously degraded as well. I commented out the temp_video.avi file, which looks to be about the right size, but I couldn’t find anything that could play it.

    What am I missing here?

    Thanks,
    Keith

  8. adminon 19 Apr 2008 at 12:53 pm

    Keith, check the mkv contains a ogg file. It’s perhaps something else. (try the file command)

  9. Keith Hanlanon 20 Apr 2008 at 11:07 pm

    Thanks for your reply “jkx”.

    “file *” returns the following:

    keithh@pvr [106]: file *
    temp_audio.ogg: ATSC A/52 aka AC-3 aka Dolby Digital stream, 48 kHz,, complete main (CM) 3 front/2 rear, LFE on,, 384 kbit/s reserved Dolby Surround mode
    temp_video.avi: JVT NAL sequence, H.264 video @ L 51
    XXXXX.hdtv.x264-bia.avi: RIFF (little-endian) data, AVI, 0 x 0, >30 fps,
    XXXXX.hdtv.x264-bia.mkv: Matroska data

    It managed to extract a decent sized ogg file but the file command gives no indication of whether or not it is an ogg file. It probably isn’t since it’s video. Am I barking up the wrong tree here? Was your script only intended to extract the audio?

    keithh@pvr [105]: wc -c *
    139198464 temp_audio.ogg
    1012791223 temp_video.avi
    25978426 XXXXX.hdtv.x264-bia.avi
    1153155923 XXXXX.hdtv.x264-bia.mkv
    2331132311 total

    I mispoke when I said I commented out the temp_video.avi file. I meant to say that I commented out the *removal* of that file in your script.

    It looks like the tools are willing to do what I need. Perhaps I need some different options. Any more suggestions that you might have would be very welcome.

    Thanks again,
    Keith

  10. adminon 20 Apr 2008 at 11:53 pm

    Hum, take care the ogg file is a 5.2 stream. I’m quite sure you need to resample (or tweak) ffmpeg to handle this.

  11. jonon 22 May 2008 at 1:53 am

    Hi -

    Does the conversion decrease the quality of the video/audio?

    Thanks

  12. adminon 22 May 2008 at 8:06 am

    No, the quality remains the same. In fact, it’s only a repack, not a real convert

  13. Yoshiion 16 Jun 2008 at 9:54 am

    Hi i got an error when i tried this script,
    I put this script into a file called mkvtoavi and made it executeable
    then in bash#

    ./mkvtoavi
    File “./mkvtoavi”, line 9
    print “=” * 78
    ^
    IndentationError: expected an indented block

    And i am not really good with scriptprogramming. but it seems it’s something thats missing :( please reply to my email as soon as you can :)

  14. earthforce_1on 01 Jul 2008 at 5:53 pm

    Ok, figured out an answer to my own question, and got it down to one line with no intermediate files. Your modified .py script becomes:

    #!/usr/bin/python
    # Little script to depack Matroska file, and repack them
    # in a AVI + subtitle format.

    import sys
    import os

    def message(msg):
    print “=” * 78
    print “= %s” % msg
    print “=” * 78

    def usage():
    print “Mastroka repacker script”
    print ” Usage: “+sys.argv[0]+ ” filename”

    if __name__ == “__main__”:
    if len(sys.argv) < 2:
    usage()
    else:
    filename = sys.argv[1]
    basename = filename[:-4]
    message(”Converting file: %s” % filename)
    os.system(”mencoder %s -ovc lavc -oac lavc -o %s.avi” % (filename,basename) )

  15. Stevenon 13 Jul 2008 at 6:19 pm

    To Yoshii, this script needs you to tab in some things to work…

    #!/usr/bin/python
    # Little script to depack Matroska file, and repack them
    # in a AVI + subtitle format.

    import sys
    import os

    def message(msg):
    TAB—>print “=” * 78
    TAB—>print “= %s” % msg
    TAB—>print “=” * 78

    def usage():
    TAB—>print “Mastroka repacker script”
    TAB—>print ” Usage: “+sys.argv[0]+ ” filename”

    if __name__ == “__main__”:
    TAB—>if len(sys.argv) TAB—>usage()
    TAB—>else:
    TAB—>TAB—>filename = sys.argv[1]
    TAB—>TAB—>basename = filename[:-4]

    message(”Unpacking file: %s” % filename)
    os.system(”mkvextract tracks %s 1:temp_video.avi 2:temp_audio.ogg 3:%s.srt” % (filename,basename) )

    message(”Repacking file: %s.avi” % basename)
    os.system(”ffmpeg -i temp_audio.ogg -i temp_video.avi -vcodec copy %s.avi” % (basename) )

    message(”Cleaning files”)
    os.system(”rm temp_video.avi temp_audio.ogg”)

  16. Daniel P. Clarkon 02 Aug 2008 at 3:19 pm

    What about importing the subtitles into the target avi?

  17. adminon 02 Aug 2008 at 3:26 pm

    To import the sub you can use Avidemux. I used this software a couple of times without issue.

  18. Martinon 16 Sep 2008 at 3:47 pm

    ::SIGH::

    I realize that people tend to “think” in their favorite language, but isn’t using python for this sort of like swatting a fly with a sledgehammer?

    #!/bin/bash
    [[ -z "$1" ]] && echo “usage: $0 filename” && exit
    mencoder $1 -ovc lavc -oac lavc -o `basename $1 .${1#*.}`.avi

  19. Timoon 07 Oct 2008 at 11:58 pm

    Martin: that also re-encodes the streams, better use copy. Also, basename matches first ‘.’, which means that foo.bar.mkv becomes foo.avi..

    Maybe use best of both approaches, and modify the original to just have:

    message(”Converting %s to %s.avi” % (filename, basename))
    os.system(”mencoder %s -ovc copy -oac copy -o %s.avi” % (filename, basename) )

  20. Everton 13 Oct 2008 at 6:35 pm

    So which is better/quicker to use? ffmpeg or mencoder? 8-)

  21. DaveCon 24 Oct 2008 at 12:39 am

    mencoder has long been the most flexible OSS tool for producing and repacking AVI’s … ffmpeg is more useful at low level stuff where you need to re-encode.

    using -ovc copy is both better and much faster, as it avoids re-encoding the video …. also mencoder’s default settings for video encoding are designed around non-HD quality and internet streaming, and you’ll end up having to dink around. I’m sticking with -oac lavc and leaving it to re-encode to the default non-Dolby “DVD style” audio (2 x 48kHz) since I don’t have surround sound set up yet and the file sizes are about 10% smaller.

    My goal is to get stuff to play on the Sony PS3 which has become our DVD player - it happily plays files from CD-R and DVD-R (and presumably BD-RE though the blanks are kind pricey :-) and it will play AVI’s in HDTV formats, but it does not (as of the latest PS3 OS build) speak Matroska and likely never will (Sony being pro-proprietary)

  22. Davidon 08 Dec 2008 at 9:41 am

    This is very helpful, thanks!

    I am able to use mencoder to convert mkv file to avi but it does not have audio. I read some sites online. They are saying it is because multple audio. I wonder if anyone has solution for it?

    Thanks in advance!

  23. None 1on 19 Apr 2009 at 11:22 am

    Cannot find the site to download mkvextract

    Your request for http://corecodec.org/projects/mkvextractgui could not be fulfilled, because the connection to corecodec.org (69.45.6.150) could not be established.

  24. None 1on 19 Apr 2009 at 11:47 am

    I did this manually. However, I have missed something (the python script only gave errors)

    $ mkvextract tracks down-ltroi-720p.mkv 1:temp_video.avi 2:temp_audio.ogg 3:down-ltroi-720p.srt
    $ mkdir temp
    $ cd temp
    $ mkvextract tracks ../down-ltroi-720p.mkv 1:temp_video.avi 2:temp_audio.ogg 3:down-ltroi-720p.srt
    Extracting track 1 with the CodecID ‘V_MPEG4/ISO/AVC’ to the file ‘temp_video.avi’. Container format: AVC/h.264 elementary stream
    Extracting track 2 with the CodecID ‘A_DTS’ to the file ‘temp_audio.ogg’. Container format: Digital Theater System (DTS)
    Extracting track 3 with the CodecID ‘S_TEXT/UTF8′ to the file ‘down-ltroi-720p.srt’. Container format: SRT text subtitles
    progress: 100%

    $ ffmpeg -i temp_audio.ogg -i temp_video.avi -vcodec copy temp_video.avi
    FFmpeg version SVN-rUNKNOWN, Copyright (c) 2000-2007 Fabrice Bellard, et al.
    configuration: –enable-gpl –enable-pp –enable-swscaler –enable-pthreads –enable-libvorbis –enable-libtheora –enable-libogg –enable-libgsm –enable-dc1394 –disable-debug –enable-libmp3lame –enable-libfaadbin –enable-libfaad –enable-libfaac –enable-xvid –enable-x264 –enable-liba52 –enable-amr_nb –enable-amr_wb –enable-shared –prefix=/usr
    libavutil version: 1d.49.3.0
    libavcodec version: 1d.51.38.0
    libavformat version: 1d.51.10.0
    built on Mar 17 2009 21:37:49, gcc: 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
    [mp3 @ 0xb7ec6110]Could not find codec parameters (Audio: mp2, 112 kb/s)
    temp_audio.ogg: could not find codec parameters

  25. Steveon 26 Apr 2009 at 7:14 pm

    sh -x conversion.sh
    hangs on “import os”

  26. Chrison 15 May 2009 at 4:43 pm

    Sweet script, once I got the tabs sorted out it worked great! I’ve got 6 seasons of Ranma 1/2 to convert with this now!!! Thanks for the handy script.

  27. adminon 15 May 2009 at 5:04 pm

    I fixed the tab issue, and pushed the file on the SVN …

Comments RSS

Leave a Reply