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 ;)

#!/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


15 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”)

Comments RSS

Leave a Reply