Using Matroska - mkv - Media Containers 1

Posted by JD 05/22/2010 at 20:38

The Matroska container format for video has become popular over the last few years because it merges a number of good ideas and let’s a single file contain multiple video, audio and subtitle streams. No longer do you have to keep multiple .vob/.mpg/.avi.mp4, and multiple .sub/.srt/.idx files to have 3 different audio and subtitles. All of them can be placed into a single .mkv file. For me having the subtitles efficiently contained inside the same file is good, but the real, fantastic reason that MKV containers are brilliant is you can easily correct aspect ratio issues without re-encoding.

Here’s another article for why you should also use MKV if you’d like more specifics.

There’s another nice bonus. In my testing, the .mkv files are always smaller than the .avi files from which they are made.

Jumping into the How-To

First, you’ll need to install the MKVToolNix using your package manager and repositories. On my Lubuntu system, that was just
sudo apt-get install mkvtoolnix

I use Linux, but the mkv tools are cross platform. There’s even a GUI for people that demand it.

Let’s suppose you have a few files.

  • FILE.avi (xvid video + mp3 audio in an AVI container)
  • FILE.srt (Text subtitles) and
  • FILE.idx (PS subtitles – may contain multiple languages)

To convert the AVI file into an MKV file, simply use

mkvmerge -o FILE.mkv FILE.avi

To convert the AVI file into an MKV file with SRT subtitles, simply use

mkvmerge -o FILE.mkv FILE.avi FILE.srt

If you’d like to label the language for the subtitles, that open exists. Check the man page.

To convert the AVI file into an MKV file with PS subtitles, simply use

mkvmerge -o FILE.mkv FILE.avi FILE.idx

To Correct the Aspect Ratio, simply use

mkvmerge --aspect-ratio '1:4/3' -o FILE.fix.mkv FILE.mkv
where ‘1’ is the video stream number and ‘4/3’ is the desired aspect ratio for display. Note that this only seems to work when the inputfile is already inside an MKV container.

I only show AVI/xvid files in these examples, but MPEG2 and MPEG4 files will work exactly the same. The MKV is a smart container, so it won’t let you put unsupported video, audio or subtitles into the container. It is also FAST, about the speed of a file copy, which makes sense because the tool isn’t doing much more than building a small XML file containing the metadata for the different streams, then zlib compressing it, and shoving everything into a file with .mkv at the end. I’m certain that I’ve over simplified what it really happening, but it is still FAST.

The ease for these sample commands show the simplicity of the MKV container. I’m sold on them now that I own a network media player that knows how to deal with them, the WD TV-Live. VLC and mplayer will both playback these files and subtitles too.

Win7 Media Center doesn’t appear to support the MKV format out of the box. Here’s an article on how to convince 7MC to support MKV. I expect their instructions will work, but have not tried them yet.

Trackbacks

Use the following link to trackback from your own site:
https://blog.jdpfu.com/trackbacks?article_id=655

  1. JD 07/24/2010 at 19:28

    So the last update to mkvmerge enabled compression of the audio header information. This has broken audio on every mkv file that I’ve created since, at least from a playback perspective.

    So I checked with the maintainer of the format and he was well aware of the issue – it is not a bug as far as he is concerned. Compression of the audio headers has been in the specification for over 6 years so anyone who hasn’t implemented it yet was just asking for trouble. Ok, so the mkvmerge command has an option to turn off the compression, except it doesn’t work for me. Or, perhaps I’m a little dense?

    mkvmerge --compression "0:none" --default-duration "0:${FPS}fps" -o "${NEW}" "${IN}" 

    This doesn’t work. Sometimes the mkvmerge command requires that you initially perform the avi to mkv, then rework the mkv. That didn’t work either. I’ve tried about 10 different versions of the compression option and none have worked so far.

    I even tried the GUI-mkvmerge program – no joy with it either.

    I think the option is broken. Further research found that -1 will select all tracks, so

     —compression “-1:none”

    should work. Tested and it does.

    In my reading, it seemed that everyone – EVERYONE was using the GUI tool for this. It doesn’t make any sense to me. In the GUI, you need to point and click a bunch, while using the shell version is trivial to setup with every option you like and turn into a 1-drag-n-drop solution, even on MS-Windows. I don’t understand.