Search:  
Gentoo Wiki

MEncoder/Rip_DVD


This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Contents

Introduction

This HOWTO will give you a very basic introduction to ripping and encoding DVDs with mencoder, the powerful command that comes with mplayer.

Requirements

mplayer must be built with encode to build mencoder. You also need permission to read your dvd drive so make sure you are in the cdrom group. Also for xvid and x264 compression, you need to have xvid and x264 enabled. For AAC audio you need aac. Use:

emerge -va media-video/mplayer

to see different choices available for customizing mplayer.

Titles

Before you can begin, you need to find out which title on the DVD has the actual movie. lsdvd (media-video/lsdvd) is a fine tool to show you which track on the dvd is the longest:




$ lsdvd /dev/dvd

libdvdread: Using libdvdcss version 1.2.9 for DVD access

Please send bug report - no VTS_TMAPT ??

Disc Title: VANISHING

Title: 01, Length: 01:46:30 Chapters: 25, Cells: 43, Audio streams: 01, Subpictures: 01

Title: 02, Length: 00:00:00 Chapters: 01, Cells: 01, Audio streams: 01, Subpictures: 00

Title: 03, Length: 00:01:32 Chapters: 02, Cells: 01, Audio streams: 01, Subpictures: 00

Longest track: 1





For the rest of the howto, dvd://1 will be used in place of whatever titles you find are necessary.

Calculating bitrate

In general, the formula is:

video bitrate = (target filesize / movie length) - audio bitrate (FIXME: adjust this for overhead too)

Note: target filesize should be in kilobits, not kilobytes (generally measured in kilobits per second)

Encoding Options

There are a couple of choices you'll have to make here.

  1. One-pass or multipass encoding?

One-pass encoding is a lot faster, but the video quality isn't as good. For this reason, most people choose to do multipass encodes.

  1. Which codec to use?

XviD is the preferred codec for DVD rips these days, because it's a good tradeoff of encoding quality and time, and because it's well supported. As of recently, it's also possible to encode with the x264 codec, which has much better compression (and therefore better quality at a given bitrate), but isn't well supported yet, and requires a much faster processor than does XviD. Unlike Xvid, though, x264 supports threading, meaning that it can use both cores on a multicore processor (if threads=num is set).

Single-pass encoding

An example:

$ mencoder dvd:// -ovc lavc -lavcopts vcodec=mpeg4:vhq:vbitrate=694 -oac mp3lame -lameopts br=128 -o dvdvideo.avi

Multi-pass encoding

An example:

// Encode audio
$ mencoder dvd://1 -ovc frameno -o audio.avi -oac mp3lame -lameopts abr:br=128
// Encode video - first pass
$ mencoder dvd://1 -audiofile audio.avi -oac copy -o /dev/null -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=800:vhq:vpass=1:vqmin=1:vqmax=31
// Encode video - second pass
$ mencoder dvd://1 -audiofile audio.avi -oac copy -o dvdvideo.avi -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=800:vhq:vpass=2:vqmin=1:vqmax=31

The audio is encoded separately from the video so we don't end up doing it twice, and wasting our precious CPU cycles.


Commands

Xvid

Run the following two commands to start ripping but be sure to change the "#" to the number of the dvd title that you want to rip (normally 1 but this can sometimes be the copyright notice or trailer). If you leave the # blank, it will rip the largest video track on the disc.

mencoder dvd://# -ovc xvid -xvidencopts pass=1 -alang en -oac copy -o /dev/null
mencoder dvd://# -ovc xvid -xvidencopts pass=2:bitrate=1000 -alang en -oac mp3lame -lameopts vbr=3 -o movie.avi

"alang en" is in there because certain dvd's will give you the directors commentary if you dont specify the audio language specifically. A "bitrate" value is required for the second pass.

If the DVD has already been copied off of DVD media, then you can use the "-dvd-device" option like so:

 mencoder dvd://1 -dvd-device /home/movies/Herbie/ -ovc xvid -xvidencopts pass=1 -alang en -oac copy -o /dev/null

Now go find something to do because it will take a long time.

H264 (MPEG4 Part 10)

Another possibility is to use the new x264 based codec. It is MPEG-4 AVC while Xvid is MPEG-4 ASP. AVC is newer generation codec, which improves visual quality at the same bitrate, but requires more processing power. Mplayer documentation gives good explanations for all supported options. The following example uses also faac, which is free AAC-implementation. AAC stands for Advanced Audio Codec and offers some improvements over mp3, most notably better quality for the same bitrate.

This method of ripping involves first dumping the DVD to hard disk, then encoding it with 2 passes at very high quality. This takes about 50-75% longer than a single pass, but improves overall quality for a given bitrate. But since this is for personal archival purposes, I want to be sure about high quality.

Convert the DVD to a file

Copy a DVD title to hard disk, saving to file title1.vob:

mplayer dvd://1 -dumpstream -dumpfile title1.vob

Remove earlier subtitles

rm subtitles.idx subtitles.sub

Before we start lets first get the details on how to crop our video. -vf (video filter) crops the black borders from the movie, right settings are detected by running:

mplayer dvd://1 -vf cropdetect=:2

Pass 1

Video codec is x264 with very high quality options, I'm voting here for quality, not encoding time with bitrate 1500. If you are trying to do video, which fits certain amount of CDs, try playing around with some of the bitrate calculators, which can be found online.

We are analyzing here only video, so audio is just copied over and resulting video is dumped to /dev/null. It is no use anyways.

I'm also creating vobsubtitles with subtitlelanguage en. Use lsdvd -s to find out which subtitles are available.

-ofps-setting is for progressive NTSC encoding. More about it, telecine and interlacing etc. can be found from mplayer documentation. Note: You need Mplayer-1.0pre8 for AAC encoding.

mencoder -v -noskip \
         title1.vob \
        -alang en \
        -vf crop=720:352:0:64,scale=752:320 \
        -ovc x264 -x264encopts subq=5:bframes=4:b_pyramid:weight_b:pass=1:psnr:bitrate=1500:turbo=1:bime \
        -oac copy \
        -ofps 24000/1001 \
        -vobsubout subtitles -vobsuboutindex 0 -slang en \
        -o /dev/null

Note that we use the crop settings from cropdetect, and that the video should be scaled according to the aspect ratio of the movie (and should be a multiple of 16).

Pass 2

Next we go for the 2nd pass. At this point, mencoder/x264 reads statistics from logfile it created and compresses video accordingly. We also compress audio with faac so next stage takes longer than 1st, and use higher quality compression. Some of those filters can be dropped or quality decreased, if more speed is desired.

mencoder -v -noskip \
         title1.vob \
        -alang en \
        -vf crop=720:352:0:64,spp,scale=752:320,hqdn3d=2:1:2 \
        -ovc x264 -x264encopts subq=5:partitions=4x4:8x8dct:frameref=3:me=hex:bframes=4:b_pyramid:pass=2:psnr:bitrate=1500:bime \
        -oac faac -faacopts object=1:tns:quality=100 \
        -ofps 24000/1001 \
        -o title1.avi

With this setup the first pass runs at 15fps with my Pentium-M 725 (1.6Ghz). Second depends, but normally around 10fps. Takes a long time, but the quality is great. You can speed up the first pass by lowering some of the settings in the first pass only, such as subq and frameref (turbo=1 or 2 actually does this), as mentioned in the official docs.

As I said before bitrate 1500 is really high for x264 at this resolution. You should change it for each movie depending of the length and quality you want. Slow movies look good with lower bitrates. Real bitrate might be around 700kbps for 1CD and around 1400 for 2CD version.

Encoding h264 using x264 binary

First get the x264 binary. The x264 release that is in portage is outdated so download a new snapshot from ftp://ftp.videolan.org/pub/videolan/x264/snapshots/ and then make it. .

tar -xjvf x264-snapshot-XXXXXXXX-XXXX.tar.bz2
cd x264-snapshot-XXXXXXXX-XXXX
./configure 
make

Now there should be a x264 binary in this directory.

Then we need to create a named pipe:

mkfifo stream.y4m

Then run mplayer to dump the raw video into the named pipe. Replace dvd://1 with your source video.

mplayer -nosound -noframedrop -noautosub -vo yuv4mpeg:file=stream.y4m dvd://1 

Then run the x264 binary you just made in another console.

x264 --progress -o dvd1.mp4 stream.y4m

Please note that this x264 command only uses defaults settings and does not produce the best quality. See below for better settings.

Notes

The video produced should be playable on any half decent media player (including windows or mac software with no modification), though it won't work in QuickTime. For tips on making vidoes work in QuickTime see the mecoder documentation

You can run this command to find the number that corresponds to the title you actually want to rip

mplayer dvd://1

Change the "1" to "2" and "3" and so on until you find the right title.

The program "lsdvd" is also very useful. When you run it, it displays a list of the titles on a DVD, along with information such as number of chapters, length, etc. When ripping a film, for example, you run "lsdvd" and look for the title that is the same length as is written on the back of the DVD box.

The filename at the end can obviously be changed to whatever you want.

There are a huge number of options available to you when ripping DVD's with mencoder and you should really read the man page and other documentation if you want to get the best possible results. The above commands are intended to be a simple way to rip from a DVD without knowing much about video encoding.

Below are two screen shots. The first is taken straight from the DVD. The second is encoded with x264 using the command line:

x264  --sar 91:64 -m 6 --b-rdo --me umh --bime -b 4 --b-pyramid --bitrate 1300 -p 2 -w -8 --threads 2 --progress -o sincity.try2.mp4 encode 720x576

Where encode is a named pipe. Also, two pass compression was used.

Image:Mpeg2.png Image:Avc.png

Note the graininess in the sky in the first picture that was smoothed by x264, and the slight blockiness in the x264 image.

Credit

The commands here are taken from the documentation for mplayer.

See Also

External Links

Retrieved from "http://www.gentoo-wiki.info/MEncoder/Rip_DVD"

Last modified: Fri, 03 Oct 2008 07:56:00 +0000 Hits: 86,891