Spectralcat's Blog

August 21, 2009

Turning DVDs into AVI Files the Hard Way: Mencoder

Filed under: Uncategorized — Tags: , , , , , , — spectralcat @ 6:03 am

There are lots of GNU/Linux programs for turning DVDs into AVI files.  The two most popular ones are Acidrip (a frontend for mencoder) and DVD:Rip (a frontend for transcode).  I’ve run into problems using both of these programs, though.  I’ve found that the more reliable way to create .avi files is to use mencoder directly. Below I’ve provided a series of mencoder commands that you can copy and paste into the command Terminal.

We’ll use the following programs to create our .avi file(s): vobcopy, midentify.sh, mplayer, DVD:Rip, and mencoder.  Midentify.sh is included in mplayer’s source code in a folder called TOOLS.  You can download mplayer’s source code directly from mplayer’s website and then cp the midentify.sh script to your /usr/local/bin directory.  We’ll use DVD:Rip to calculate our frame size and video bitrate, but we’ll make the actual .avi file(s) with mencoder.  The resulting files will be made using the xvid codec and will be deinterlaced so that they play better on computer screens.  If you don’t want them to be interlaced, just remove the -vf yadif line from the end of each script.

Using Mplayer and Midentify.sh to find out the Title and Language Track Numbers:
1) First, we need to copy the DVD to the hard drive and then rename the folder containing the DVD’s VIDEO_TS folder so that it is compatible our mencoder scripts below.  To copy the DVD to a folder named dvd that is located on our Desktop, we open the Terminal and do this:

cd Desktop
vobcopy -m
mv folder-vobcopy-created dvd

2) Next, we’ll determine which Title on the DVD we want to copy.  DVDs often have several Titles (different ones for the main feature and for special features, previews, etc.).  Run the following in the Terminal and see if the feature you want to convert starts playing:

mplayer -dvd-device dvd dvd://1

The number 1 in the above command identifies the first Title on the DVD.  If this is not the right DVD feature, press “q” to stop mplayer and replace the number 1 in the above command with the number 2 and hit enter again.  Repeat this process until you find the correct DVD title.  Once you’ve found the correct Title number, write it down.

3) Next we want to use midentify.sh to tell us the language track number of the audio track we want to copy.  A given Title on a DVD may have several different Language Tracks (for director’s commentary, other dubbed languages, etc.).  The main language track is normally 128 (though sometimes it’s different). To do this run the following in the Terminal:

midentify.sh -dvd-device dvd://NUMBER-OF-CORRECT-TITLE

In the above command, replace NUMBER-OF-CORRECT-TITLE with the Title number you found in the previous step.  Midentify.sh should spit out a pile of information about that Title number.  Look for the lines that begin ID_AUDIO_ID=.  These lines should identify the different language track numbers.  After you find the language track you want to rip, run the following command to test that track:

mplayer -dvd-device dvd dvd://NUMBER-OF-CORRECT-TITLE -aid NUMBER-OF-CORRECT-LANGUAGE-TRACK

Replace NUMBER-OF-CORRECT-TITLE with the appropriate title number and replace NUMBER-OF-CORRECT-LANGUAGE-TRACK with the appropriate language track number.  If the resulting movie looks and sounds correct, then both numbers should be correct.  Write down the correct Language Track number.

Using DVD:Rip to Determine a Frame Size and Bitrate:
1) Open DVD:Rip by going to Applications: Sound and Video: dvd:rip
2) File: New Project
3) Project name: type a name here and select “Create Project” and on the pop up window hit OK
4) Select “Choose DVD Image Directory” and navigate to the dvd directory on your Desktop; hit OK
5) Select “Encode DVD on the Fly”
6) Select “Rip Title” on the left side of the screen
7) Select “Read DVD Table of Contents”
8) If you like, you can double-check your chosen Title number and Language Track Number by looking at the information provided on this screen.
9) Select “Clip & Zoom” on the left side of the screen
10) Select “DVD Title” at the top of the screen
11) Hit “Grab Frame”; don’t worry if nothing happens
12) Select “Open Zoom Calculator”
13) In the top right hand corner of the calculator select the “By Quality” tab
14) On the BPP Value drop down menu pick a number between .10 and .25.  The higher this number the better our .avi file will look, but the larger it will be.  Generally, good quality .avi files should be between .20 and .25.  The closer you go to .10, the blockier the .avi file will look.  If you don’t care much about creating a nice looking .avi file, you should be fine with an bpp of .10.  If you want it to look really nice, pick a bpp of around .20 or .23.  Don’t go above .25 as the resulting file will be needlessly big.
15) Now look at the 3 numbers in bold: v-rate, bpp, and total size.  Under “Zoom Calculations” the calculator lists a series of acceptable screen resolutions (ones that will preserve the aspect ratio.  Picking a lower screen resolution is like picking a lower BPP; doing so reduces file size, but it produces blurrier videos when the .avi is played at full screen.  Try to pick one of the screen resolutions near the top of the list within the first 5 or 6 entries.  In addition, the numbers in red are slightly better than the numbers in black as the red numbers will preserve the screen resolution exactly.  Experiment by selecting different screen resolutions and clicking “Apply Click and Zoom Settings” until you find a “total size” that is acceptable to you.  For a good quality .avi file, I assume that 10-12 MBs for every minute of video is reasonable; if you want to squeeze an .avi file onto a CD though you will want to go with a smaller file size (around 700 MB).  If the picture display is working (sometimes it doesn’t!) you should be able to compare different frame sizes.  The picture at the far left is the original dvd’s frame size; the picture at the far right is the frame size you’ll end up with.  You can click on them to make them bigger so you can compare them.
16) When you’ve settled on an acceptable frame size, write down the following numbers: V-RATE, Zoom Size, and Result Size.  When we plug these numbers into mencoder, the V-RATE will be your video bitrate; the Zoom Size will be your scale rate, and Result size will be your Crop rate.

Creating the AVI file with Mencoder:
Below are several mencoder scripts.  You will need to replace the following values in each script:

NUMBER-OF-TITLE-TO-RIP: Replace with the number of the appropriate Title. You must replace this value twice.
LANGUAGE-NUMBER: Replace this with the number of the appropriate language track.  You must replace this value twice.
REPLACE-BITRATE: Replace this with your chosen Video Bitrate (DVD:Rip’s V-RATE).
SCALE-WIDTH:SCALE-HEIGHT: Replace this with the width and height you want to scale to (DVD:Rip’s Zoom Size specfied as “width x height”); mencoder wants these values separate with a colon as in width:height (ex. 720:480).
CROP-WIDTH:CROP-HEIGHT: Replace this with the width and height you want to crop to (DVD:Rip’s Result Size specfied as “width x height”); mencoder wants these values separate with a colon as in width:height (ex. 720:480).

The following scripts are two-pass.  This means that they have two parts that are separated by a &&.  The NUMBER-OF-TITLE-TO-RIP and LANGUAGE-NUMBER must be replaced in two places in the script (before and after the &&).  The values REPLACE-BITRATE, SCALE-WIDTH:SCALE-HEIGHT, and CROP-WIDTH:CROP-HEIGHT only need to be replaced in the second part of the script (after the &&).  I would recommend coping and pasting the script into a text editor like Gedit: Applications: Accessories: Text Editor. Replacing the appropriate values in the text editor and then copying and pasting your finalized script into a Terminal window.

To convert your chosen DVD Title to one long .avi file do:

rm -f divx2pass.log && mencoder -dvd-device dvd dvd://NUMBER-OF-TITLE-TO-RIP -aid LANGUAGE-NUMBER pass=1 -o /dev/null && mencoder -dvd-device dvd dvd://NUMBER-OF-TITLE-TO-RIP -aid LANGUAGE-NUMBER -ovc xvid -xvidencopts pass=2:bitrate=REPLACE-BITRATE -vf scale=SCALE-WIDTH:SCALE-HEIGHT -vf crop=CROP-WIDTH:CROP-HEIGHT -ffourcc XVID -oac mp3lame -lameopts cbr:br=128 -vf yadif -o movie.avi

To convert one chapter of your DVD to one .avi file, also replace the values CHAPTER-NUMBER-CHAPTER-NUMBER before and after the &&. (For example, to rip chapter 5 only replace CHAPTER-NUMBER-CHAPTER-NUMBER with 5-5):

rm -f divx2pass.log && mencoder -dvd-device dvd dvd://NUMBER-OF-TITLE-TO-RIP -chapter CHAPTER-NUMBER-CHAPTER-NUMBER -aid LANGUAGE-NUMBER pass=1 -o /dev/null && mencoder -dvd-device dvd dvd://NUMBER-OF-TITLE-TO-RIP -chapter CHAPTER-NUMBER-CHAPTER-NUMBER -aid LANGUAGE-NUMBER -ovc xvid -xvidencopts pass=2:bitrate=REPLACE-BITRATE -vf scale=SCALE-WIDTH:SCALE-HEIGHT -vf crop=CROP-WIDTH:CROP-HEIGHT -ffourcc XVID -oac mp3lame -lameopts cbr:br=128 -vf yadif -o chapter.avi

To convert multiple chapters of your DVD to several .avi files (one for each chapter), the above script just gets longer. You want to copy the above script and then for each additional chapter you want to copy, paste the following to the end of the script. (Be sure to replace the two CHAPTER-NUMBER-CHAPTER-NUMBER sections and the final chapter.avi. Rename chapter.avi to chapter-2.avi, chapter-3.avi, chapter-4.avi, etc.):

&& rm -f divx2pass.log && mencoder -dvd-device dvd dvd://NUMBER-OF-TITLE-TO-RIP -chapter CHAPTER-NUMBER-CHAPTER-NUMBER -aid LANGUAGE-NUMBER pass=1 -o /dev/null && mencoder -dvd-device dvd dvd://NUMBER-OF-TITLE-TO-RIP -chapter CHAPTER-NUMBER-CHAPTER-NUMBER -aid LANGUAGE-NUMBER -ovc xvid -xvidencopts pass=2:bitrate=REPLACE-BITRATE -vf scale=SCALE-WIDTH:SCALE-HEIGHT -vf crop=CROP-WIDTH:CROP-HEIGHT -ffourcc XVID -oac mp3lame -lameopts cbr:br=128 -vf yadif -o chapter-2.avi

To convert a specific section of the movie to an .avi file do the following:
First, use mplayer to find the beginning and ending of the section:

mplayer -dvd-device dvd dvd://NUMBER-OF-CORRECT-TITLE -aid NUMBER-OF-CORRECT-LANGUAGE-TRACK

Use the arrows and page-up and page-down the find the appropriate section. When you get to the start of the section you want, press spacebar to pause the video. Then look at mplayer’s output in the Terminal; write down the first number following the letter A (for example: A:1231) and replace the value START in the following with this number. Continue watching the video until you get to the end of the section you want to copy. Pause the video again and write down the second number following the letter A (for example: A:1250); subtract the first number from the second number (for example 1250-1231); replace END in the following command with this new number.

mplayer -dvd-device dvd dvd://NUMBER-OF-CORRECT-TITLE -aid NUMBER-OF-CORRECT-LANGUAGE-TRACK -ss START -endpos END

After you’ve determined the appropriate starting and ending position, do this command (replace START and END in two separate places):

rm -f divx2pass.log && mencoder -dvd-device dvd dvd://NUMBER-OF-TITLE-TO-RIP -aid LANGUAGE-NUMBER pass=1 -ss START -endpos END -o /dev/null && mencoder -dvd-device dvd dvd://NUMBER-OF-TITLE-TO-RIP -aid LANGUAGE-NUMBER -ss START -endpos END -ovc xvid -xvidencopts pass=2:bitrate=REPLACE-BITRATE -vf scale=SCALE-WIDTH:SCALE-HEIGHT -vf crop=CROP-WIDTH:CROP-HEIGHT -ffourcc XVID -oac mp3lame -lameopts cbr:br=128 -vf yadif -o movie.avi

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: