Ripping DVDs with Mencoder

This is a local copy of a nice howto you can find in the Ubuntuforums at:
HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska"
Some little changes I've made, are marked in red.

HOWTO: Rip DVDs in MPEG-4 AVC (x264), multi audio, subtitles, Matroska - by Heinz
MPlayer and MEncoder are amazing tools not only for watching but also for backing up DVD content. This HOWTO demonstrates how to create a very high quality rip with next generation video (H.264/x264/MPEG-4 AVC) serveral audio tracks (Vorbis in this case, can be other formats like AC3, MP3) and subtiles (vobsubs) in a Matroska container. In order to install the necessary applications you will need the multiverse repository (https://help.ubuntu.com/community/Repositories/Ubuntu).

1. Install needed applications

sudo apt-get install mplayer mencoder normalize-audio vorbis-tools mkvtoolnix mkvtoolnix-gui gpac x264-bin
2. Rip DVD to harddisk
mplayer dvd://1 -v -dumpstream -dumpfile title.vob
3. Subtitles
In order to determine which subtitles are available on your DVD run the following command
mplayer dvd:// -v -vo null -ao null | grep "subtitle"
3.1 Rip subtitles
mencoder dvd://1 -nosound -ovc frameno -o /dev/null -slang en -vobsubout title
4. Audio
In order to determine which audio tracks are available on your DVD run the following command
mplayer dvd:// -v | grep "audio stream"
In case you want to keep the original AC3 audio step forward to 4.4.

4.1 Convert audio to PCM
mplayer title.vob -ao pcm:file=audio1.wav -vc dummy -aid 128 -vo null
If you would like to rip another audio track (e.g. commentary or different language) repeat the above with the next track number (-aid 129 would be the second track) and save as audio2.wav.

4.2 Normalize audio
normalize-audio audio1.wav
Repeat on audio2.wav etc. if you have more than one audio track.

4.3 Encode audio into Ogg Vorbis
oggenc -q3 audio1.wav
4.4 Keep original Dolby Digital AC3 audio
In case you do not want to compress audio but keep the original AC3 track simply extract it from the VOB with
mplayer title.vob -aid 128 -dumpaudio -dumpfile title.ac3
5. Video
This example uses the two-pass-method and presumes progressive PAL video. Read here how to deal with telecined, interlaced and NTSC video.

First, we have to get rid of black borders around the movie. Hence we playback the file with the cropdetect filter.
mplayer title.vob -vf cropdetect
Move a little forward in the movie using the arrow-up key and let MPlayer find the correct settings for you. If you are finished quit MPlayer and copy the part -vf crop=720:432:0:76 from the console. Of course your values might differ from this example.

5.1 Determine video bitrate
The choice of the bitrate depends. If you don't care about filesize anything above 1000 deliveres superb quality. If you do however plan the final file size to be about 700 MB or a multiple calculate like this (copied from MPlayer documentation):

If you aim at a certain size, you will have to somehow calculate the bitrate. But before that, you need to know how much space you should reserve for the audio track(s), so you should rip those first. You can compute the bitrate with the following equation: bitrate = (target_size_in_Mbytes - sound_size_in_Mbytes) * 1024 * 1024 / length_in_secs * 8 / 1000 For instance, to squeeze a two-hour movie onto a 702MB CD, with 60MB of audio track, the video bitrate will have to be: (702 - 60) * 1024 * 1024 / (120*60) * 8 / 1000 = 740kbps

5.2 Start video encoding process
Create a file which runs the first and second pass consecutively.
gedit videnc
Paste the following into that file and adjust the cropping and video bitrate values with the ones you got from the procedures above
# First pass
mencoder -v\
title.vob\
-vf crop=720:432:0:76,harddup,pp=lb\
-ovc x264 -x264encopts subq=4:bframes=3:b_pyramid:weight_b:turbo=1:pass=1:psnr:bitrate=800<\
-oac copy\
-of rawvideo\
-o title.264

# Second pass
mencoder -v\
title.vob\
-vf crop=720:432:0:76,harddup,pp=lb,scale=-1:-2 -aspect 16:9\\
-ovc x264 -x264encopts subq=6:4x4mv:8x8dct:me=3:frameref=5:bframes=3:b_pyramid:weight_b:pass=2:psnr:bitrate=800\
-oac copy\
-of rawvideo\
-o title.264
x264encopts threads=auto will speed things up if you own a dual-core cpu.
I've added deinterlacing (pp=lb) and scaling (scale=-1:-2 -aspect 16:9), since the automated scaling seemed not to work here. Besides I changed the bitrate from 1000 to 800, which produces good enough quality here.

Since MEncoder is not able to save directly into Matroska containers we encode the video in raw format convert it later into .mp4 and finally mux everything (video, audio, subtitles) together with mkvmerge. Interested in what all those options mean? If everything fits your needs save videnc.

Encoding of MPEG-4 AVC video is a time consuming matter. On my AMD Athlon64 3000+ a 90 minute movie takes about 3 hours for the first and again about 5 hours for the second pass. Best is to let your machine work over night while you sleep.

Run videnc
sh videnc
6. Muxing
6.1 Mux video into MP4 container
Good morning! ;)
If encoding went fine we are ready to put that x264 file into an MP4 container
MP4Box -add title.264 title.mp4
You can already verify the result by playing it in MPlayer.

6.2 Muxing it all together into Matroska container
Start up the MKV files creator
mmg
and simply drag & drop your files into the Input files box You might have noticed that we did not scale the video during the encoding process. That is because Matroska handles the aspect ratio itself. (See above. Seems wrong here.) Simply define the languages and track names in the Tracks box and choose the correct aspect ratio. Choose an output filename (the default would produce title.mkv) and hit Start muxing.



permanent link to this entry