HowTo: Rip a DVD video title into an x264 and Ogg encoded MKV video file

Many people like myself jumped aboard the revolution that was the DVD ten years ago (has it already been that long??) and collected a vast library of discs that now take up space on several shelves in the corner of your lounge room. In this day and age of the PVR and DVR, even I myself find it frustrating to go to the shelf, find the movie I want to watch, take the disc out, make sure it’s free of fingerprints, stick it in the drive, skip all the blasted “mandatory” ads and trailers before you can actually get to the movie itself. At least with YouTube and downloaded AVI and MPEG files, you can simply double-click and watch what you want, when you want, on demand, 24/7 – no mess, no fuss.

So here I present a guide on how to rip your DVD collection into convenient, tidy x264-encoded MKV files. You may find that you can store your entire collection of DVD’s onto a single external hard-drive to carry with you, and will certainly serve as a useful backup the day that some inconsiderate soul scratches or steals your DVD’s! This HowTo is based on The Smorgasbord HowTo, but with modifications to bring it up to date with current implementations of x264 and MEncoder.

Purpose:

To rip a DVD video disc title’s components (video, audio, subtitles, etc) into a single Matroska (.mkv) file.

Why a Matroska file instead of an MPG or AVI file?

Matroska is an open-standards container format that is rapidly gaining support. It is an envelope for which there can be many audio, video and subtitles streams, allowing the user to store a complete movie or CD in a single file. Matroska offers many benefits including:

  • Fast seeking in the file
  • High error recovery
  • Chapter entries
  • Selectable subtitle streams
  • Selectable audio streams
  • Modularly Extendable
  • Streamable over internet (HTTP and RTP audio & video streams)
  • Menus (like DVDs)

Pre-requisites:

To rip a DVD title, you will need:

  • A DVD video disc with a title or two to rip from (yes, sounds obvious doesn’t it?).
  • Any garden-variety DVD-ROM drive to read the disc with.
  • A reasonably powerful PC – any Core 2 Duo or better will suffice. You can use a slower machine such as a Pentium 4, but the ripping and encoding time can be a difference of several hours.
  • Suitable storage space to store your ripped video plus some working space – 8GB all up is ideal for a single title.
  • An Ubuntu Intrepid 8.10 installation (which is what this guide is based on).
  • Some patience (unless you’ve got a multi-core monolith that can work out Pi to the sixth billionth place in less than a few nanoseconds, in which case you’re laughing).
  1. We’ll need to install some extra software tools. Start by opening a terminal and enter the following:

    $ sudo apt-get install mplayer mencoder normalize-audio vorbis-tools mkvtoolnix gpac x264 libdvdcss2
  2. Once all that is installed, insert the DVD you wish to rip from. Now generally most DVD’s have the main feature as title number “1”, but these days we are increasingly seeing a number of trailers or advertisements starting with this title, so to find out what title is what, type in the following:

    $ mplayer dvd://1
  3. This will attempt to play the first title on the disc in a new window. You will notice in the console output that it will also list some other useful information such as how many titles are on the disc, etc. If the title being played is not the feature you’re after, simply close the playback window and change the command to use dvd://2 or dvd://3 etc until you find the title you want. An example of this output is:

    $ mplayer dvd://1
    MPlayer 1.0rc2-4.3.2 (C) 2000-2007 MPlayer Team
    CPU: Intel(R) Core(TM)2 Quad  CPU   Q9450  @ 2.66GHz (Family: 6, Model: 23, Stepping: 7)
    CPUflags:  MMX: 1 MMX2: 1 3DNow: 0 3DNow2: 0 SSE: 1 SSE2: 1
    Compiled with runtime CPU detection.
    Playing dvd://1

    There are 3 titles on this DVD.
    There are 20 chapters in this DVD title.
    There are 1 angles in this DVD title.
    audio stream: 0 format: ac3 (5.1) language: en aid: 128.
    audio stream: 1 format: ac3 (5.1) language: es aid: 129.
    audio stream: 2 format: ac3 (stereo) language: en aid: 130.
    number of audio channels on disk: 3.
    subtitle ( sid ): 1 language: en subtitle ( sid ): 3 language: en
    subtitle ( sid ): 5 language: es subtitle ( sid ): 7 language: sv
    subtitle ( sid ): 9 language: no subtitle ( sid ): 11 language: da
    subtitle ( sid ): 13 language: fi subtitle ( sid ): 14 language: es
    subtitle ( sid ): 15 language: sv subtitle ( sid ): 16 language: no
    subtitle ( sid ): 17 language: da subtitle ( sid ): 18 language: fi
    subtitle ( sid ): 20 language: es number of subtitles on disk: 13

    …etc…
  4. In the above example, there are three titles, 20 chapters, three audio tracks and 13 subtitles for various languages. If I wanted to see what Title 3 was about, I would pass the argument dvd://3 to the mplayer command.
  5. Now let’s start ripping that title. First up, create yourself a working directory:

    $ mkdir mydvdrip
    $ cd mydvdrip

  6. Assuming Title 1 was the correct title, type in the following:

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

    This tells MPlayer to dump the title data to a physical file called “mydvdrip.vob” instead of displaying it to a window. The .vob file contains both the video and audio tracks of the selected title.
  7. Once that is done, you can optionally rip the DVD subtitles too using:

    $ mencoder dvd://1 -oac copy -ovc frameno -o /dev/null -slang en -vobsubout mydvdrip

    …which will result in the English subtitles (the “en” in the -slang argument)  files mydvdrip.idx and mydvdrip.sub being created.
  8. Now, the audio can be kept in its original AC3 surround format, but can take up a lot of extra space. If you’re not fussed about proper full surround sound, you can convert the audio to plain stereo, but to do that means we need to convert the audio into PCM format as follows (if you want to preserve the original AC3 surround audio, then skip to step 11 now):

    $ mplayer mydvdrip.vob -ao pcm:file=audio1.wav -vc dummy -aid 128 -vo null

    The “-aid 128″ part refers to the audio track to rip. In this case, it’s the first audio track. If I wanted to rip the second audio track, I would use “-aid 129″ instead. See the MPlayer console output when you were playing the disc for the other aid parameters to access the other audio tracks.
  9. Once the audio is ripped and converted, we need to normalise it so it’s not too loud or too soft with:

    $ normalize-audio audio1.wav
  10. …and then finally convert it into the final audio format we want to put into the final ripped product, in this case Ogg Vorbis which is a much cleaner-sounding and open format than MP3:

    $ oggenc -q5 audio1.wav

    The -q5 parameter encodes at 160 kbits/s which should be very clear and crisp compared to the original audio. Change this parameter to alter the compression if you want to save space. Skip to step 12 now.
  11. If you didn’t want to convert the audio to stereo, and preserve the original surround audio, you can rip the AC3 audio with:

    $ mplayer mydvdrip.vob -aid 128 -dumpaudio -dumpfile mydvdrip.ac3
  12. We have the audio and subtitles finished, so all we need now is the video itself. As you have probably noticed, many DVD titles generally have a border of black around them to some degree. We don’t want this in our rip and can save some space by omitting it, but we need to get the cropping bounds. Thankfully MPlayer does a pretty good job of detecting this by itself. Type in:

    $ mplayer mydvdrip.vob -vf cropdetect
  13. Allow the title to play for a bit, or skip forward a bit. You will notice some data output to the terminal. This is what MPlayer believes is the optimal cropping bounds for this title where it says -vf crop=688:448:18:64 (your output will vary). Stop playback and copy the series of numbers after the equals sign to the clipboard.
  14. Now we need to setup two-pass encoding. Two passes are made for quality reasons. The first pass makes note of details about the video, such as slow and fast moving scenes, which allow MEncoder to vary the bitrate of the resulting video accordingly, providing great image quality whilst maintaining a small file size. Encoding requires two executions of MEncoder one after the other, so let’s create a shell script to do it. Create an empty script with:

    $ gedit encodevideo.sh
  15. …and an empty text editor will appear. Copy & paste in the following:

    # Encoding pass #1
    mencoder -v mydvdrip.vob -vf pullup,softskip,crop=688:448:18:64 \
    -ovc x264 -x264encopts subq=4:bframes=3:b_pyramid:weight_b:\
    turbo=1:pass=1:psnr:bitrate=1000:threads=auto -oac copy \
    -of rawvideo -o mydvdrip.264

    # Encoding pass #2
    mencoder -v mydvdrip.vob -vf pullup,softskip,crop=688:448:18:64 \
    -ovc x264 -x264encopts subq=6:partitions=all:me=umh:frameref=5:\
    bframes=3:b_pyramid:weight_b:pass=2:psnr:bitrate=1000:threads=auto \
    -oac copy -of rawvideo -o mydvdrip.264

    (Remember to substitute the “crop=” values from your own crop output)

    I won’t go through describing what all the options above do suffice that they will produce a very high quality rip of your DVD title using a modest amount of disc space (generally no higher than about 700MB for a single DVD movie), and it will make full use of any multi-CPU or multi-core processing power at your disposal (a Quad-core system will encode video roughly 3 times faster than a single core system). If the resulting file size is still too big, you can make it smaller by changing the bitrate=1000 parameter to something slightly smaller, eg: 900, but you will begin to sacrifice image quality.
  16. Save the file and quit your text editor.
  17. We’re now about to begin ripping and encoding the video data. Depending on your system, this part could take anywhere between 60 minutes to several hours. In this example, my 3.2GHz Quad-Core Q9450 based system took 32 minutes to perform just the first encoding pass at 87fps, with the second pass taking 52 minutes to complete at 54fps. Execute the script with:

    $ sh ./encodevideo.sh
  18. …and go order a pizza, or wait. The video from the .vob file will be extracted, encoded using the x264 codec and the resulting raw data placed into a file called mydvdrip.264 at the end.
  19. Once the script has finished, you should now have the following files in your working directory (except the .ac3 file if you chose not to rip it):

    -rw-r--r-- 1 hyrax hyrax   91783421 2008-12-21 19:29 audio1.ogg
    -rw-r--r-- 1 hyrax hyrax 1058132012 2008-12-21 19:26 audio1.wav
    -rw-r--r-- 1 hyrax hyrax   13936736 2008-12-21 23:02 divx2pass.log
    -rw-r--r-- 1 hyrax hyrax  688634256 2008-12-21 23:49 mydvdrip.264
    -rw-r--r-- 1 hyrax hyrax  264532992 2008-12-21 19:31 mydvdrip.ac3
    -rw-r--r-- 1 hyrax hyrax      38423 2008-12-21 19:23 mydvdrip.idx
    -rw-r--r-- 1 hyrax hyrax    3426304 2008-12-21 19:23 mydvdrip.sub
    -rw-r--r-- 1 hyrax hyrax 4473358336 2008-12-21 19:16 mydvdrip.vob
    -rw-r--r-- 1 hyrax hyrax        482 2008-12-21 22:35 encodevideo.sh


    In the above example, my original DVD title rip was some 4.4GB in size. The resulting compressed video file using x264 is only a paltry 688MB – notably smaller and the visual quality on playback looks identical to the original DVD data. The original AC3 audio consumed 264MB, but the Ogg compressed version only consumes 91MB. Put the x264 video data and converted Ogg audio data together and we have a rip that will consume a total of 779MB all up (give or take a few MB for the container that will hold them both).
  20. Now we need to recombine the audio, video and subtitles into one Matroska container file. First up, the video. We need to convert the video from raw x264 data into MP4 format before we add it to the MKV container. To do this, type in:

    $ MP4Box -add mydvdrip.264 mydvdrip.mp4
  21. Now we’re ready to build the final MKV file. Type in:

    $ mkvmerge -o mydvdrip.mkv *.mp4 *.ogg *.idx

    (replace the *.ogg with *.ac3 is you want to use the original audio instead, or include them both so they are selectable on playback! Beware this will blow out your final file size, however)
  22. When the muxing is complete, your rip is complete. You can now play it in your favourite video player such as Totem or MPlayer.

— o O o —

To recap the whole process:

  1. mplayer dvd://1 -v -dumpstream -dumpfile mydvdrip.vob
  2. mencoder dvd://1 -oac copy -ovc frameno -o /dev/null -slang en -vobsubout mydvdrip
  3. mplayer mydvdrip.vob -ao pcm:file=audio1.wav -vc dummy -aid 128 -vo null
  4. normalize-audio audio1.wav
  5. oggenc -q5 audio1.wav
  6. mplayer mydvdrip.vob -vf cropdetect
  7. mencoder -v mydvdrip.vob -vf pullup,softskip,crop=688:448:18:64 \ -ovc x264 -x264encopts subq=4:bframes=3:b_pyramid:weight_b:\ turbo=1:pass=1:psnr:bitrate=1000:threads=auto -oac copy \ -of rawvideo -o mydvdrip.264
  8. mencoder -v mydvdrip.vob -vf pullup,softskip,crop=688:448:18:64 \ -ovc x264 -x264encopts subq=6:partitions=all:me=umh:frameref=5:\ bframes=3:b_pyramid:weight_b:pass=2:psnr:bitrate=1000:threads=auto \ -oac copy -of rawvideo -o mydvdrip.264
  9. MP4Box -add mydvdrip.264 mydvdrip.mp4
  10. mkvmerge -o mydvdrip.mkv *.mp4 *.ogg *.idx

Or, to preserve the original AC3 surround audio track without conversion, replace steps 3 to 5 with:

mplayer mydvdrip.vob -aid 128 -dumpaudio -dumpfile mydvdrip.ac3

…and replace step 10 with:

mkvmerge -v -o mydvdrip.mkv *.mp4 *.ac3 *.idx

That’s it. Happy ripping!