1. Andrew's Corner :

The Matrix, MPlayer, Mencoder and Matroska

This page aims to document my own exploration of the amazing program MPayer as I make a backup copy of one of my favourite movies "The Matrix" suitable for playback on my computer. In publishing this exploration on the Web I should state at the very outset that I am not by any means an expert but that I am in fact simply having a fine old time experimenting with some amazing software! For the most part I believe have achieved what I set out to do and my backup copy of "The Matrix" is of a quality that is completely satisfactory for my own personal taste. I have endeavoured to make this page an easy and enjoyable read by dividing it into into three logical sections: Converting the video, Converting the audio and Creating the Matroska file. Please feel to use the email link at the base of this page to let me know if I have succeeded in this goal or indeed if you have found areas that need some improvement!

Converting the video

I am planning on creating a single video stream and two audio streams for this backup copy of "The Matrix" and to create the video stream it is necessary to rip the movie, decide on cropping parameters and finally to actually encode. First to rip the movie:

Ripping the movie

There are of course several ways to rip the movie but quite frankly I needed software that started with "M" to fit in with the catchy title of this page! So MPlayer it is and just for the record I am using MPlayer dev-SVN-r27326-4.2.3 which I downloaded and compiled under Slackware 12.1. The following command rips audio, video and subtitles from the dvd:

$ mplayer -v dvd://1 -dumpstream -dumpfile matrix.vob

This produces a perfectly playable file of about 5 gig and if space was no consideration it would be best to leave this as it is. However efficient encoding with the parameters of my choice will drop the size to 1 gig and produce, I believe, a better quality image and sound. First to calculate the cropping parameters for this big vob file:

Finding the cropping parameters

Because I am a little lazy I usually use the excellent tools provided by MPlayer to calculate to cropping area. So the following command shows me what is required:

$ mplayer -vf cropdetect=round:16 -ss 10:00 matrix.vob

This gives -vf crop=720:416:0:82 which of course correspond to width, height, x-axis and y-axis taken from the top left corner. Of course ever-mindful that you cannot really trust a machine in the Matrix world this can be tested with the rectangle filter:

$ mplayer -vf rectangle=720:416:0:82 -ss 20:00 matrix.vob 

There is a small loss of pixels from top and bottom with these settings but I gather it is always better to throw a few pixels away rather than encode black borders. I cordially dislike scaling and I intend to omit this step both from video encoding and from the final creation of the matroska file. It is a personal choice but resizing is easy on a computer screen and autoaspect is your best friend. But now to start the encoding preparation:

Video encoding parameters

I am using x264 core:60 r914 fee2f2c for the h264 encoding which I downloaded and compiled from the VideoLan x264 git repository. There are infinite arguments concerning the best command line to run when encoding video files with h264 and for the record this command line was sourced directly from the MPlayer HTML help files. The first pass runs as follows:

$ mencoder -v matrix.vob -o /dev/null -alang en -oac copy -ovc x264 \
-x264encopts pass=1:threads=auto:turbo=1:bitrate=1000:subq=6:partitions=all:8x8dct:\
me=umh:frameref=5:trellis=1:bframes=3:b_pyramid:weight_b \
-vf crop=720:416:0:82,harddup

The video goes straight to /dev/null as it is the second pass that will be kept. The audio is copied across to help maintain A/V synchrony later when the Ogg Vorbis audio will be muxed into the Matroska container. Although this means audio and video are mixed in this file the audio can be excluded with mkvmerge when the Matroska container is created. Now for the second pass:

$ mencoder -v matrix.vob -o matrix.264 -alang en -oac copy -ovc x264 \
-x264encopts pass=2:threads=auto:bitrate=1000:subq=6:partitions=all:8x8dct:\
me=umh:frameref=5:trellis=1:bframes=3:b_pyramid:weight_b \
-vf crop=720:416:0:82,harddup

This is a long, slow process and I usually run it overnight but the result is always quite impressive. This completes the initial work on the video and now for a much quicker process where the audio stream is ripped and encoded:

Converting the audio

Deciding on the audio format was easy as I am an old fan of the Ogg Vorbis format and I have used it extensively before. But first the audio streams have to be identified and then ripped to wav format:

Identify and rip the audio streams

First I have another look at the DVD and see which audio stream to use:

$ mplayer dvd://1 -identify -frames 0 | grep "audio stream"
audio stream: 0 format: ac3 (5.1) language: en aid: 128.
audio stream: 1 format: ac3 (stereo) language: en aid: 129.
audio stream: 2 format: ac3 (stereo) language: en aid: 130.

A bit of investigation showed that 130 contained a director's commentary combined with music only and no dialogue while 129 contained a director's commentary combined with slightly muted dialogue. The easiest way to check this out is to use something like the following to actually play the track:

$ mplayer -aid 130 dvd://1 -vo null

The audio track I was actually after was 128 but the "music and commentary" caught my attention so I will encode that one as well! The following command copies both tracks from the ripped file:

$ mplayer -aid 128 matrix.vob -vc null -vo null \
-ao pcm:fast:waveheader:file=matrix.wav 
$ mplayer -aid 130 matrix.vob -vc null -vo null \
-ao pcm:fast:waveheader:file=matrix_commentary.wav 

This leaves two monster wav files of 1.4 gig each and the next step is to convert them to Ogg Vorbis files although mp3 would be another choice.

Convert to Ogg Vorbis

As well as converting these files to Ogg Vorbis in an optional step I run the amazing vorbisgain over both files.

$ oggenc -q 6 matrix.wav -o matrix.ogg
$ oggenc -q 6 matrix_commentary.wav -o matrix_commentary.ogg
$ vorbisgain matrix.ogg
$ vorbisgain matrix_commentary.ogg

The audio bitrate is a little high with an average of 165.8 kb/s and an eventual file sizes of 156 mb each but this is a personal preference and one that I will stand by. The music and sound effects of "The Matrix" are truly amazing with this setting! Now to mux it all together in a Matroska container:

Creating the Matroska file

MPlayer / Mencoder cannot directly create Matroska containers so for this the tool of choice is mkvmerge which is part of the mkvtoolnix package. Newer versions of mkvmerge deal directly with h264 files and you would be advised to use a modern version if only for this reason; to see if your version can directly mux h264 try the following:

$ mkvmerge --list-types | grep h264

To create the Matroska container the following command line is required. You will note the -A -S options in front of the video file, this precludes adding the audio and subtitle information from this file into the container:

$ mkvmerge --default-language eng -o matrix.mkv -A -S matrix.264 \
matrix.ogg matrix_commentary.ogg

This is a very basic command line and of course much more is possible if you delve into the mkvmerge documentation. The order of the command line is important as this sets the "default" track, mkvmerge will promote the first track of each type that it finds to be the default track, but this can be set by the command line also if you wish. But this rather spartan command line will produce the desired Matroska file that should play perfectly with MPlayer. The "alternate" soundtrack can be selected by specifying -aid 1 in the MPlayer command line. And now I am going to sit back and enjoy the movie!

And in conclusion ...

Well believe it or not I did not create this page from scratch, although I hope that I have put my own stamp on it. I would like to especially acknowledge a nicely written guide found on the Ubuntu Forums: HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska. Please feel free to contact me with any errors of fact that you have found on this page, any errors of opinion will probably remain uncorrected. In the meantime I am having a great time exploring Linux and the amazing world of video and audio encoding, what about you?