How to rip a DVD to MPEG-4 using MEncoder & Linux
Foreword
- This is a rewritten tutorial (December 2008) replacing the previous one I had which was severely outdated (from the end of 2004), and was written with MPlayer v4.1.2. This tutorial uses the Xvid video codec due to it’s backwards compatibility with standard hardware DivX players.
- This is not a tutorial of how to use MPlayer, but how to use MEncoder to convert a full DVD from around 6GB into an MPEG-4 (also known incorrectly as DivX) file of about 700MBs.
- I presume you have basic understanding of the Linux command-line.
- You will need a working installation of MPlayer with lame (MP3), Xvid & DVD support built in. If not, please refer to the MPlayer documentation before going any further as it will not work if you are missing any of the above features.
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). MPlayer & MEncoder support many different types of audio and video codecs, both for reading and writing. I have chosen to create this rip using lame for MP3 audio, and XviD for it’s MPEG4 codec.
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.
Calculating the video bitrate
So we now have a working MPlayer/MEncoder with DVD support, and the object is to rip this DVD to your harddrive in a 700MB “DivX” file. Of course I will not suggest ripping an entire DVD each time you want to test something, as this does take quite a while to do. The simpler solution to this is to rip just one chapter when testing, 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. The bitrate used & the output filesize are directly linked, so we have to calculate the right bitrate to give us the intended output size.
I once wrote a simple QT program to do this called DivxCalc, which I have converted to an online web calculation. The QT project is not maintained any more, however the calculations still apply (should you decide to install it).
Working out bitrates is not as simple as 1-2-3, namely because you are working with very large numbers. Your standard output sizes are worked out normally in MB’s, which are 1024 bytes per Kb (1024MB in a GB etc). But both DivX (version 4 upwards) and MP3 work in kb, which is actually 1000 bytes. It may not seem much (1024 - 1000) but it does make a significant difference over a 2 hour film when calculations are incorrect!
HINTS: From my findings, an audio bitrate of 96kb/s using the lame codec is more than sufficient for a movie. Many people 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 to achieve your output goal, 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 than sufficient!
Also, if you would like to burn your DVD rip to CD, let’s say a 700MB CD, I suggest choosing a CD size of 690MB’s in the calculation. This gives a bit more room “for error”, because there is nothing more annoying than waiting 3 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 bitrate, we can take the next step for your MPEG-4 rip with MEncoder.
Rip a 16:9 DVD to MPEG-4 with a 2 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 the outs” of MEncoder.
PLEASE NOTE: When scaling down your movie, make sure that both the height and width values are dividable by 16!
Why do I choose a 2 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 2 pass rip however is that ripping the video twice gives much better quality. A 2-pass rip simply means that MEncoder will (on the first rip) simply detect all the points in your movie where a higher bitrate is required (fast motion), and slower scenes where a lower bitrate can be used, and write all its findings to a log file for its second rip. On the second rip it uses this logfile to pre-warn itself of these “spikes”, so it’s ready in advance. This ensures higher quality video, and a more accurate filesize.
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 (X-Men 2 ~ 128 minutes long). This film is in fact also 16:9, hence the examples.
Using DivxCalc it returned the results as:
What | Value |
---|---|
CD Length / File size | 690MB’s |
Movie Length (minutes) | 128 |
MP3 Bitrate (kb/s) | 96 |
MPEG-4 Bitrate | 658 |
For my example (X-Men 2), the main film track is located on track 1 on MPlayer (dvd://1). I used an audio bitrate of 96kb/s, and as DivXCalc stated, I used a video bitrate of 658.
The 2 rip commands I used were as follows, and I will explain them below:
Console commands
Here are the three commends I used to rip the DVD to my harddrive. Each one is explained in more detail in the next section.
mplayer dvd://1 -chapter 3 -vf cropdetect
mencoder dvd://1 -vf crop=720:416:0:80,scale=704:304 -ovc xvid -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=1 -oac copy -o /dev/null
mencoder dvd://1 -vf crop=720:416:0:80,scale=704:304 -ovc xvid -xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=2 -alang en -oac mp3lame -lameopts br=96:cbr:vol=6 -o X-Men-2.avi
Commands explained
1) Calculating crop values
mplayer dvd://1 \ #(dvd://1 states the first track on the DVD)
-chapter 3 \ #(start at chapter 3)
-vf cropdetect #(use the inbuilt MPlayer cropdetect filter to detect the black borders)
What I first needed to do before I could start ripping was to work how to remove the top & bottom black borders around the original movie. There is no point leaving these in as they just take up extra space. MPlayer comes with a handy feature (an output “filter”) called cropdetect, which detects the dimensions of these black borders around the main movie. In order to detect these black borders MPlayer needs a section of the video with full color, so it is generally easiest to test somewhere into the movie itself, and not at the beginning. I usually take a random chapter and test on that:
This produces output along the lines of:
Starting playback...
VDec: vo config request - 720 x 576 (preferred colorspace: Planar YV12)
VDec: using Planar YV12 as output csp (no 0)
Movie-Aspect is 1.78:1 - prescaling to correct movie aspect.
VO: [xv] 720x576 => 1024x576 Planar YV12
[CROP] Crop area: X: 0..719 Y: 74..501 (-vf crop=720:416:0:80).0
[CROP] Crop area: X: 0..719 Y: 74..501 (-vf crop=720:416:0:80).0
[CROP] Crop area: X: 0..719 Y: 74..501 (-vf crop=720:416:0:80).0
[CROP] Crop area: X: 0..719 Y: 74..501 (-vf crop=720:416:0:80).0
[CROP] Crop area: X: 0..719 Y: 74..501 (-vf crop=720:416:0:80).0
[CROP] Crop area: X: 0..719 Y: 74..501 (-vf crop=720:416:0:80).0
[CROP] Crop area: X: 0..719 Y: 74..501 (-vf crop=720:416:0:80).0
[CROP] Crop area: X: 0..719 Y: 74..501 (-vf crop=720:416:0:80).0
[CROP] Crop area: X: 0..719 Y: 74..501 (-vf crop=720:416:0:80).0
......
The output tells me the command I need to append to my rip (-vf crop=720:416:0:80) in order to crop the source video (DVD) in order to remove the black borders.
2) First pass rip
mencoder dvd://1 \ #(dvd://1 states the first track on the DVD)
-vf crop=720:416:0:80,scale=704:304 \ #(crop the main movie, and scale down the output)
-ovc xvid \ #(set the output video codec to xvid)
-xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=1 \ #(Xvid encoding options, including bitrate & stating it's the first rip [pass=1])
-oac copy \ #(do not compress/convert audio, just copy it 1:1)
-o /dev/null #(output the audio and video to /dev/null as we do not need it)
On the first pass (rip), I basically did a full scan of the entire DVD track (1) telling MEncoder:
- which DVD track I want to rip
- which video codec I am going to use (Xvid)
- what video bitrate & codec options I am going to use (refer to the official MPlayer documentation for all the possible options)
- which video crop & scale values I am going to use
- that this is the first pass rip (ie: write a logfile of all video it encodes)
- to simply copy the audio as it’s not being used (this time around)
- to dump the output file to /dev/null (delete it)
The vpass=1 tells MEncoder that this is the first video pass. What MEncoder does here is to rip the video, and at the same time it writes a log file in the same directory called divx2pass.log
. This is an important log file, which contains all the films video 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 which can cause a terrible picture with encoding.
-vf scale=704:304 is something I generally always use with 16:9 video. These measurements (in 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. If your movie is a 4:3 video you can use a scale of something like 480x320 or similar, as long as it stays in proportion, and both values are dividable by 16.
3) Second pass rip
mencoder dvd://1 \ #(dvd://1 states the first track on the DVD)
-vf crop=720:416:0:80,scale=704:304 \ #(crop the main movie, and scale down the output)
-ovc xvid \ #(set the output video codec to xvid)
-xvidencopts bvhq=1:chroma_opt:quant_type=mpeg:bitrate=658:pass=2 \ #(Xvid encoding options, including bitrate & stating it's now the second rip [pass=2])
-alang en -oac mp3lame \ #(audio language is English, and output codev is lame [MP3])
-lameopts br=96:cbr:vol=6 \ #(set the lame options, 96kb/s, constant bitrate, and increase volume by 6)
-o X-Men-2.avi #(output to a file X-Men-2.avi)
On the second pass, which has almost the same command as the first rip, I tell MEncoder that this is the second pass, and which audio codec (lame, with encoding options) it should encode the film with. I tell MEncoder:
- which DVD track I want to rip
- which video codec I am going to use (Xvid)
- what video bitrate & codec options I am going to use (refer to the official MPlayer documentation for all the possible options)
- which video crop & scale values I am going to use
- that this is the second pass rip (telling MEncoder to now use the logfile)
- which audio codec to use (lame)
- which audio codec options (96kb/s, contact bitrate)
- to output it all to a file called “X-Men-2.avi”
The vpass=2 tells MEncoder to use the previously created divx2pass.log file it created and to rip the movie again. It loads in this file to pre-warn itself of sudden movements (such as explosions) during the film, and to increase bitrates to adjust quality to ensure the picture is good. This is actually the whole point of a two-pass rip.
Conclusion
Well, that concludes my “short” updated explanation as to how to rip a DVD to MPEG-4 using MEncoder. I must emphasize to you to please give the MPlayer documentation a read some time. They contain lots of information which can be very useful which I simply cannot cover in a tutorial. 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 slightly 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 ;-)