Ripping a DVD to MPEG-4 using MEncoder.

Ripping a DVD to MPEG-4 using MEncoder.

Postby Ripper » Wed Jul 28, 2010 6:32 pm

The following link provides a complete list of topics on this forum:

http://www.preearth.net/phpBB3/search.php?search_id=newposts


From RTFMwiki

http://axljab.homelinux.org/Mencoder_DVD_to_MPEG-4

Foreword

This tutorial is based upon MPlayer-20041106 (CVS version). The reason I state this is that over the last year or so MPlayer has changed certain commands. This version stated above can be downloaded from my ftp server should you have issues with your own version.

This tutorial is not a tutorial of how to use MPlayer, but how to use MEncoder to convert a full DVD from +- 6GB into an MPEG-4 (also known incorrectly as DivX) file of +- 700MB's. I presume that you do have knowledge of the console (basic commands).

Also I presume you have a working installation of MPlayer (with MP3 (lame) support and libavcodec built in, as described in the tutorial MPlayer Installation. If not, please refer to that document before going any further as it will not work is you are missing anything.

Introduction

MPlayer comes with a very handy tool called MEncoder, which is used to convert one video/audio stream into another. It uses console commands for this (there are GUI's for it, but you will have to search yourself for those). MEncoder, although supporting several different codecs, is designed around the FFmpeg's libavcodec, which provides superior quality and speed.

There are of course several ways to rip a DVD to MPEG-4, but I will explain the method that I personally use, describing the technique and use.

Calculate the bitrate to use.

So we have a working MPlayer/MEncoder with DVD support now, so the next step is to rip it to your harddrive. Of course I will not suggest ripping an entire DVD each time to your harddrive each time you want to test something. The simpler solution to this is to rip just one chapter, otherwise you have to wait several hours each time you want to see how the result turns out.

Ok, so we need to work out a bitrate for the Video. Why you ask? Well, bitrate is the quality of the video... the higher the quality, the bigger the output filesize is. These are DIRECTLY linked!

I wrote a simple QT program to do this called DivXcalc, and it works very well. Working out bitrates is not as simple as 1-2-3, namely because you are working with very large numbers. Your standard sizes are worked out normally in MB's, which turn out to be 1024 bits per Kb. But both DivX (4 and upwards) and MP3 work in kb, which is actually 1000 bits. So to make things simple I created a simple QT bitrate calculator.

HINTS:

From my findings, an audio bitrate of 96kb/s is more than sufficient for a movie. Many prople use 128kb/s for the MP3 track, however this is in my opinion a big waste of space. Remember, for every MB of extra audio, you have to reduce your video bitrate, so better sound = worse video. If you are ripping a music-video I would then say use 128kb/s audio, otherwise, like I have just said, 96kb/s is more then sufficient!

Also, if you would like to burn your DVD rip to CD, let's say a 700MB CD, I suggest chosing a CD size of 680MB's in the calculation. This gives a bit more room to play with, because there is nothing more annoying than waiting 6 hours for a film to be ripped, and after all that it's just a little too big to fit on one CD.

Once you have worked out a suitable rate, we can take the next step for your MPEG-4 rip with mencoder.

Rip a 16:9 DVD to MPEG-4 with a 3 pass rip

Why did I choose a 16:9? Well, because I had to choose an example, and most DVD films are in this format. This does not mean that other formats don't work, but they just don't work with my example as my examples use scaling, and the scaling given here is for the 16:9 format. These are also "just examples", but as you read below you may get some more understanding of the ins_and_outs of mencoder.

PLEASE NOTE:

When scaling down your movie, make sure that the values chosen are dividable by 16!

Why do I choose a 3 pass rip? Well, a DVD can be ripped in a single go. Mencoder does ripping on_the_fly, so it doesn't need to first copy all the vob files to your harddrive (several Gigabytes), but simply reads the DVD, and writes the MPEG-4 (*.avi) file. The reason I choose a 3 pass rip is simple because ripping the video twice gives much better quality. A 3-Pass rip simply means that the audio gets ripped once, and afterwards the video get's ripped twice. This saves a lot of time ripping the audio from a 131 minute (or whatever) film as the audio doesn't get any better or worse with more rips... . it remains the same.

This is the time consuming part of the whole process. To explain this best I am going to take a DVD of mine as an example (The Matrix [1999 ~ 131 minutes]). This film is in fact also 16:9, hence the examples.

Using DivX Calc it returned the results as:

CD Length / File size 680 MB's
Movie Length (minutes) 131
MP3 Bitrate (kb/s) 96
MPEG-4 Bitrate 629

I suggest you read though the following examples and explanations before actually trying it, as this may save you a lot of time.

For my example (The Matrix), the main film track is located on track 1. I used an audio bitrate of 96kb/s, and as DivX Calc stated, I used a video bitrate of 629.

The 3 commands I used were as follows, and I will explain them below:

Commands

mencoder dvd://1 -aid 128 -oac mp3lame -lameopts br=96:cbr:vol=6 -ovc frameno -o frameno.avi

mencoder dvd://1 -sws 2 -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=629:vpass=1 \
-oac copy -vf crop=720:346:0:154,scale=704:304 -o Matrix.avi

mencoder dvd://1 -sws 2 -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=629:vpass=2 \
-oac copy -vf crop=720:346:0:154,scale=704:304 -o Matrix.avi


Short Explanation

mencoder dvd://1 \ (dvd://1 states the first track on the dvd)
-aid 128 \ (AudioCodec_ID 128 [I had to use because the default language is German and I wanted English])
-oac mp3lame \ (Output_Audio Codec mp3lame [we choose lame])
-lameopts br=96:cbr:vol=6 \ (Lame_Options: Bitrate=96kb/s:Constant_Bitrate:Volume=6 [Increase volume, range from -10 to 10])
-ovc frameno \ (Output_Video_Codec=frameno [none ... we only want to rip the audio here])
-o frameno.avi (Output_File frameno.avi ["frameno.avi" is the standard for an audio-only output. Mencoder recognizes this file in the next 2 steps])

mencoder dvd://1 \
-sws 2 \ (Scaling_Method 2 [bicubic scaling [best quality]. we want the best quality here])
-ovc lavc \ (Output_Video_Codec libavcodec [here we state to use libavcodec])
-lavcopts vcodec=mpeg4:vhq:vbitrate=629:vpass=1 \ (Libavcodec Options VideoCodec=MPEG-4:Very_High_Quality:Video_Bitrate=629:Video_Pass=1 [first video rip])
-vf crop=720:346:0:154,scale=704:304 \ (Video_Filter scale=704:304 [I always use this scaling for 16:9 AVI],crop=720:346:0:154 [read below for explanation])
-oac copy \ (Output_Audio_Codec copy [copy this from the first Pass])
-o Matrix.avi (Output_File Matrix.avi [which file we want to output to])

mencoder dvd://1 \
-sws 2 \
-ovc lavc \
-lavcopts vcodec=mpeg4:vhq:vbitrate=629:vpass=2 \ (Libavcodec_Options Videocodec=MPEG-4:Very_High_Quality:Video_Bitrate=629:Video_Pass=2 [second video rip])
-vf crop=720:346:0:154,scale=704:304 \
-oac copy \
-o Matrix.avi

Longer Explanation

1) On the first rip, all I ripped was the audio. I told mencoder to rip the whole first track of the DVD (the whole film therfore) into MP3 format using LAME and a constant bitrate of 96kbits/s. You can use a variable bitrate, but because this option is variable, you will then always have to rip the audio first, see the size of the audio file and subtract it from the total size you want to achive. I suggest always using CBR though.

I had to state "-aid 128" with The Matrix because it jumps for some reason to German each time. "-aid 128" by standard is English. Read man mencoder and man mplayer for other options.

I also increased the output volume to "6" because ripping DVD's seems to reduce the volume somewhat. This option isn't a neccesity, but is handy when one has to turn the speakers / TV on loud just to hear what's going on :-) If you don't want it, just leave it out.

The output of the first rip was outputted to the file "frameno.avi". This is necessary if you want to rely on mencoder's defaults. This is a pure audio track, and can be played with mplayer after it's rip, but will only play audio of course. After the whole rip (all Passes), this file can be deleted as it's contents would have been merged into the final Matrix.avi file.

2) On the second rip, which is actually the first Video rip, I told mencoder to rip just the video file and merge the audio track into it. I tried to get the best quality possible as 629 bits per second is extremely low for MPEG-4, however it does make it possible to put a movie of over 2 hours long onto 1 CD, with great quality!

I chose the video codec mpeg4, which is standard for MPEG-4. Libavcodec has a very advanced codec which can be compared to DivX 5 Pro. The only noticiable difference is that libvcodec is a lot faster, and of course open source.

The vpass=1 tells mencoder that this is the first video pass. What mencoder does here is to rip the video (merging the audio), and at the same time it writes a log file in the same directory called "divx2pass.log". This is an important log file, which logs the films movements and so on. On the second rip mencoder will read this file while encoding, and will in turn produce a better quality picture as it will be prepared in advance for sudden movements or color change, the 2 things producing a terrible picture with encoding.

-vf scale=704:304 is something I use standard. These measurements (pixels) are a scaled-down version of a standard 16:9 movie, and must be dividable by 16. You probably will find that these values can be used for almost every 16:9 DVD. It produces a descent size correctly scaled down. Again, with my DVD of The Matrix the scaling totally sucks. If I rip without scaling, it stretches the picture to horrible proportians. If your movie is a 4:3 video you can use a scale of something like 400:300 or whatever, as long as it stays in proportion.

crop=720:346:0:154 is what I used for The Matrix. This is used acually only to cut out the black borders above and below the film. Although it doesn't use much space, it still isn't neccesary to have these black borders. It seems to me that each film is different here, but fortunately mencoder has a very handy built_in feature, called "cropdetect". This feature automatically detects the black borders, and gives you the measurements where to crop the film. To use it, first determine what scaling you want, then you have to replace what I have as "crop=720:346:0:154" with "cropdetect", like

mencoder dvd://1 ... ... -vf cropdetect,scale=704:304 .... ....

This will output something like:

Code: Select all
         audiocodec: framecopy (format=55 chans=2 rate=48000 bits=0 bps=12000 sample=1)
         Writing AVI header...
         crop area: X: 63.719 Y: 160..499 (-vf crop=656:340:64:160)  [0:0]
         crop area: X: 63.719 Y: 157..499 (-vf crop=656:342:64:158)  [0:0]
         crop area: X: 63.719 Y: 157..499 (-vf crop=656:342:64:158)  [0:0]
         crop area: X: 63.719 Y: 157..499 (-vf crop=656:342:64:158)  [0:0]
         crop area: X: 0..719 Y: 157..499 (-vf crop=720:342:0:158)00 [0:0]
         crop area: X: 0..719 Y: 157..499 (-vf crop=720:342:0:158)00 [0:0]
         crop area: X: 0..719 Y: 157..499 (-vf crop=720:342:0:158)00 [0:0]
         crop area: X: 0..719 Y: 157..499 (-vf crop=720:342:0:158)00 [0:0]
         crop area: X: 0..719 Y: 157..499 (-vf crop=720:342:0:158)00 [0:0]
         crop area: X: 0..719 Y: 157..499 (-vf crop=720:342:0:158)00 [0:0]
         crop area: X: 0..719 Y: 157..499 (-vf crop=720:342:0:158)00 [0:0]
         crop area: X: 0..719 Y: 157..499 (-vf crop=720:342:0:158)00 [0:0]
         crop area: X: 0..719 Y: 157..499 (-vf crop=720:342:0:158)00 [0:96]
         crop area: X: 0..719 Y: 157..499 (-vf crop=720:342:0:158)00 [0:96]
         crop area: X: 0..719 Y: 157..499 (-vf crop=720:342:0:158)00 [0:96]
         crop area: X: 0..719 Y: 154..499 (-vf crop=720:346:0:154)00 [0:96]
         crop area: X: 0..719 Y: 154..499 (-vf crop=720:346:0:154)00 [0:96]
         crop area: X: 0..719 Y: 154..499 (-vf crop=720:346:0:154)00 [0:96]
         crop area: X: 0..719 Y: 154..499 (-vf crop=720:346:0:154)00 [0:96]
         crop area: X: 0..719 Y: 154..499 (-vf crop=720:346:0:154)00 [0:96]
         crop area: X: 0..719 Y: 154..499 (-vf crop=720:346:0:154)00 [0:96]
         crop area: X: 0..719 Y: 154..499 (-vf crop=720:346:0:154)00 [0:96]


After several seconds (5-10) I see that -vf crop=720:346:0:154 keeps repeating itself. Press Control+c (CTRL c) to cancel this mencoder process. Mencoder will not actually output any file here, but simply the advised crop settings. Use these crop settings in your rip.

3) The last rip simply does what the first Video Rip (second rip altogether) did, except that it has a log file to go by. This logfile warns mencoder where to be prepared for sudden movements / color changes and so on.

The options here must be EXACTLY the same as the first pass, except that "vpass=1" must become "vpass=2"

Conclusion

Well, that concludes my "short" explanation as to how to rip a DVD to MPEG-4 using mencoder. I must emphasize to you to please give the MPlayer Docs a read some time. They contain lots of information which can be very useful. The only reason I wrote this tutorial is because there is just too much in the documentation to remember, and this confuses so many people.
Please remember that the values I used in this tutorial vary with each rip you might want to do. That is fine, as there are no 2 DVDs the same. You will have to experiment yourself a bit to find the best values for your rip. Scaling might be different, cropping most likely will be different, and of course the MPEG-4 bitrate you require will almost definitely always be different (based on the length of the film and audio quality used).

Happy ripping

Just in case you want to automate this process a little, here's the above advice taken together as a Bourne shell script. Remember, only use this in a totally legal way:

Code: Select all
#!/bin/sh -x

# three-pass rips the indicated title from the indicated DVD (image) at the
# indicated bit rate and sends the output to the indicated file with the
# output video native horizontal pixel width at the indicated size
#
# of course, you should only use this on DVDs you personally own for making a
#   single backup copy for archival purposes ;)

if [ "0$1" = "0-h" -o "0$1" = "0--help" ] ; then
    echo 'Usage: ripdvd [[[[[image_path] title_number] bitrate] output_file] x-size]'
    echo -e '\n\tdevice/path to pre-ripped directory = /dev/dvd\n\ttitle number = 1'
    echo -e '\tbitrate = 1500\n\toutput file = dvd.avi\n\toutput x-size = 512'
    exit 1
fi

device=/dev/dvd
if [ "0$1" != "0" ] ; then
    device=$1
fi

titlenum=1
if [ "0$2" != "0" ] ; then
    titlenum=$2
fi

bitrate=1500
if [ "0$3" != "0" ] ; then
    bitrate=$3
fi

title=dvd.avi
if [ "0$4" != "0" ] ; then
    title=$4
fi

xsize=512
if [ "0$5" != "0" ] ; then
    xsize=$5
fi

rm -f frameno.avi divx2pass.log $title /tmp/cropdetect.out
mencoder -alang en -dvd-device $device dvd://$titlenum -o $title -oac mp3lame -lameopts br=96:cbr \
    -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=${bitrate}:keyint=130 -vf cropdetect 2>&1\
    tr '\015' '\012' | grep 'vf crop' | head -1500 > /tmp/cropdetect.out
crop=`tail -1 /tmp/cropdetect.out | awk -F\( '{print $3}' | awk -F\) '{print $1}' | awk -F= '{print $2}'`

if [ "0$crop" = "0" ] ; then
    exit 1
fi

mencoder -alang en -dvd-device $device dvd://$titlenum dvd://$titlenum -oac mp3lame -lameopts br=96:cbr \
    -ovc frameno -o frameno.avi 2>&1
mencoder -alang en -dvd-device $device dvd://$titlenum -o $title -sws 2 -oac copy \
    -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=${bitrate}:keyint=130:vpass=1 \
    -vf "crop=${crop},scale" -zoom -xy $xsize 2>&1
mencoder -alang en -dvd-device $device dvd://$titlenum -o $title -sws 2 -oac copy \
    -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=${bitrate}:keyint=130:vpass=2 \
    -vf "crop=${crop},scale" -zoom -xy $xsize 2>&1
rm -f frameno.avi divx2pass.log /tmp/cropdetect.out

exit 0


Last bumped by Anonymous on Wed Jul 28, 2010 6:32 pm.
Ripper
 

Return to Music/Video/Film

Who is online

Users browsing this forum: No registered users and 2 guests

cron