Ripping a DVD to an AVI

From Nearline Storage
Jump to: navigation, search

(This started as something I stole from Moritz Bunkus so you'll see a lot of his words here and there.)

Using MEncoder

I use MPlayer/MEncoder versions 1.0pre7try2 and 1.0rc1 so these instructions are based on those versions. Option syntax often changes between versions.

I've written a script called dvdexport that implements the process described on this page.

mplayer and mencoder reference the DVD content using the dvd://[title] and -dvd-device <path to device> options. The [title] is usually "1" but can vary. The -dvd-device option isn't necessary if you are using an actual DVD. If the DVD content is on a filesystem however, then <path to device> should specify the path down to and including the VIDEO_TS directory.

To get the best possible quality this is a three pass process, encoding the audio first in one pass and then the video in two passes. If you're not going to encode the audio, but rather you prefer to simply copy over the existing audio, you can skip the first pass.

Creating a Preview

To test out your settings before encoding the entire DVD title you can encode a short piece of the title by appending the -ss 0:[ss] -endpos 0:[nn] options to all of the MEncoder commands that follow. This tells MEncoder to start encoding at [ss] seconds into the title and end after [nn] seconds. Later, when we want to encode the entire title, we drop these options.

Remember to delete any frameno.avi file that may exist in the current directory each time return to redo Pass 1.

Encode the Audio (Pass 1)

The first pass encodes only the audio. Make sure you know what audio language you want to use - you need either the audio id (which is 128 for the first stream, 129 for the second etc.) or the two-letter country code (like "en" for English or "de" for German). You can find out what's contained in your DVD title codes by playing it with MPlayer in verbose mode: mplayer dvd://1 -v. It should print a lot of information to the console. Search for lines that look like these:

[open] audio stream: 0 audio format: ac3 language: en aid: 128
[open] audio stream: 1 audio format: ac3 language: de aid: 129
[open] audio stream: 2 audio format: ac3 language: es aid: 130

Here we see three audio streams: English, German, Spanish and their IDs.

Make sure that there isn't a frameno.avi file in the current directory before you continue.

Now call MEncoder to encode the audio for you:

nice -n 19 mencoder dvd://1 -ovc frameno -o frameno.avi -oac mp3lame -lameopts abr:br=128:vol=6 -alang en

Where:

  • nice -n 19 - Gives MEncoder the lowest process priority so it won't disrupt your normal work.
  • -ovc frameno - Output Video Codec is frameno which means that no video data is actually written or processed at all.
  • -o frameno.avi - Write the output to the file called frameno.avi. The audio must be written to a file with this exact name in the current directory, MEncoder insists, or the subsequent steps won't use the re-encoded audio, they'll use unencoded audio copied out of the DVD title.
  • -oac mp3lame - Output Audio Codec is the lame encoder library.
  • -lameopts abr:br=128:vol=6 - options for lame. It tells lame to produce a ABR (average bitrate, it's a variable bitrate setting with the average supplied by the user) with the average bitrate of 128kbit/s. It also tells lame to increase the volume a bit because there's a tendency here for the re-encoded audio to be too quiet.
  • -alang en - use the English sound track. An alternative is using -aid 128.

A side benefit of this phase is that, when it finishes, MEncoder will tell you the exact size of the audio portion of your AVI file in bytes, the exact length of your movie in seconds and it will recommend various video bitrates for the subsequent phases. You can use the recommended bitrates or you can use the other audio file size and movie length values to calculate a different bitrate value for yourself using a bitrate calculator.

Analyze the Video (Pass 2)

nice -n 19 mencoder dvd://1 -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=[bitrate]:vpass=1 -vf crop=[w:h:x:y],scale -zoom -xy 640 -o /dev/null

Where:

  • -oac copy - MEncoder needs the audio information from the first pass in order to keep audio/video synchronization. If you didn't do the first pass then this will copy the audio from the DVD title which is fine.
  • -ovc lavc - chose lavc as the Output Video Codec.
  • -lavcopts vcodec=mpeg4:vhq:vbitrate=[bitrate]:vpass=1 - libavcodec actually supports a handful of different video codecs. With vcodec=mpeg4 we chose the MPEG4 compliant encoder. [bitrate] is the desired bitrate. There's lots of information on how to calculate a bitrate so that you get the end file size you want but I just use 1000 to get about 360MB per hour or 2000 when I want real high quality, about 700MB per hour. Use a bitrate calculator to compute a more accurate bitrate if final file size is important. vhq (very high quality) tells the encoder to spend a lot of time optimizing the results. vpass=1 tells the encoder that this is the first pass.
  • -vf crop=[w:h:x:y],scale -zoom -xy 640 - tells MEncoder how to crop the frames to remove any black borders and to scale the picture to 640 by 480 pixels. The values for [w:h:x:y] can be determined by running this exact same command but with cropdetect in place of crop=[w:h:x:y]. This will output frame by frame crop values, let it run for a bit (if you're creating a preview you'll want to remove the -ss and -endpos options from the command line for this) until those settle down and then Ctrl-c the process and write down the values for use here. If you don't have black borders in your video you can leave the crop filter out, -vf scale -zoom -xy 640
  • -o /dev/null - The output of the first pass isn't needed, so just dump it. (MEncoder creates a log file called divx2pass.log during this pass that contains the information it will re-use during the next pass.)

Generate the AVI (Pass 3)

For this step we can keep most of our previous command line. We change vpass=1 to vpass=2 to tell the encoder that it's the second pass this time. We also save the final output, replacing -o /dev/null with something useful, e.g. -o title.avi.

nice -n 19 mencoder dvd://1 -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=[bitrate]:vpass=2 -vf crop=[w:h:x:y],scale -zoom -xy 640 -o title.avi

Cleanup

When you're all finished you can erase the frameno.avi file created in Pass 1, if you did that pass, and the divx2pass.log created in Pass 2.