Difference between revisions of "Ripping and Encoding DVDs"

From Pterodactylus
Jump to: navigation, search
(Using mencoder: Add example command-line for 2-pass encoding.)
(Using mencoder: Add x264 example.)
Line 61: Line 61:
 
This will perform an inverse telecine transformation, reducing NTSC material from 30000/1001 (aka 29.97) fps to 24000/1001 (aka 23.976) fps.
 
This will perform an inverse telecine transformation, reducing NTSC material from 30000/1001 (aka 29.97) fps to 24000/1001 (aka 23.976) fps.
  
There are additional options to cut off black borders which can further improve the visual quality of the resulting video.
+
$ mencoder -o movie.avi movie.mpg -nosound -nosub -noautosub -vf crop=720:416:0:80 -ovc x264 -x264encopts crf=18:preset=veryslow:threads=1
 +
 
 +
This encodes the video with the x264 codec with a single thread and the “veryslow” preset (which is indeed very slow) and crops the video (reducing the height to 416 pixels, starting at pixel 80).
  
 
== Extract Subtitle Tracks ==
 
== Extract Subtitle Tracks ==

Revision as of 09:37, 10 August 2012

This page describes in large detail how to create near perfect DVD rips. “Near perfect” in this context means that the video quality is nearly indistinguishable from the DVD video, and all audio tracks contained in the movie are the AC3 tracks that are contained in the DVD without any reencoding whatsoever.

Copy the Main Title from the DVD

All these actions can be performed without copying the movie to a local hard disk first but as a DVD drive is almost always slower than a local hard disk, copying the main title of the movie to the hard disk before processing will save a lot of time.

First, we need to find out which title is the main title.

$ lsdvd -x

lsdvd can tell you a lot of things about the DVD in question. Especially the track IDs for audio and subtitle tracks will be interesting later on so try not to forget where you got it from. For now it will tell you the longest title on the disc; usually this is the main title. Copy it to the hard disk using tccat from the transcode package:

$ tccat -i /dev/dvd -T <title>,-1 > movie.mpg

While tccat is busy you can already perform the next steps.

$ cp /path/to/dvd/VIDEO_TS/VTS_<vts>_0.IFO movie.ifo

This will copy the IFO file for the main title to the disc. This file contains information that is relevant for subtitle conversion. If you do not need subtitles, you can skip this. The VTS number of the main title is listed in the lsdvd -x output from above and is always two digits long.

$ dvdxchap -t <title> /dev/dvd > movie.chap

This will extract the chapter information from the main title into a text file. If you want and if you know the names of the chapters, you can now edit this file.

Now we have to wait until tccat has finished copying the main title to the hard disk. Encoding the video is the next step as it is bound to take quite some time (depending on your hardware) and the rest of the steps (except for multiplexing, of course) can be performed while the video is being encoded.

Encode the Video

Encoding the video has a lot to do with voodoo. When you try to use Google to find “the best options” to encode a DVD you will find none. Or to be more precise, you will find plenty, and they will all contradict each other. There are about a millions ways to encode video; I will only outline two.

Using avidemux2

Using avidemux2 gives you a nice user interface and easy selection of all relevant parameters for the encoding process. These are the settings I usually use:

  • Video Codec: MPEG-4 ASP (xvid)
  • Constant Quality: 5
  • Some time-hungry options, such as High Motion Estimation, Wide Search for Rate Distortion Optimisation, Quarter Pixel Motion Estimation, and Global Motion Compensation. I don’t really care about how long the encoding takes, I’m interesting in the best possible picture.
  • Most important: The Pixel Aspect Ratio has to be set to “as input”! This allows you to crop black borders off of the image without having to recalculate the new width and height of the image.
  • Also, you do not need to set the width and height of the image. The size of the source material in combination with the Pixel Aspect Ratio will automatically take care that the final video is stretched as necessary. Not resizing the picture will also keep the quality of the video material at its maximum; it will never get better when you resize.
  • Disable all audio tracks, as the input MPEG file still contains all audio tracks from the DVD and they don’t need to be encoded.

Now save the file to movie.avi.

Using mencoder

Using a command line tool such as mencoder gives you the possibility to encode on a remote machine, e.g. on your desktop machine when you’re not at home, or on a headless server somewhere in a data warehouse or your basement. Basically, the mencoder command line looks like this:

$ mencoder -o movie.avi movie.mpg -nosound -nosub -noautosub -ovc xvid -xvidencopts fixed_quant=3:qpel:gmc:trellis:vhq=4

This will use the xvid codec at a fixed quantizer (3), enabling a couple of quality-improving options (quarter pixels, global motion compensation, Trellis quantization, motion search algorithm), omitting all audio tracks and subtitles from the original MPEG file.

$ mencoder -o movie.avi movie.mpg -nosound -nosub -noautosub -ovc xvid -xvidencopts qpel:gmc:trellis:vhq=4:cartoon:interlacing:pass=1:bitrate=-307200

This will used the xvid codec again but tell it to optimize for interlaced cartoon material, using 2-pass encoding heading for a total amount of 307200 KiB of video stream. (You need to run this command again, substituting :pass=2 at the appropriate place.)

$ mencoder -o movie.avi movie.mpg -nosound -nosub -noautosub -ovc xvid -xvidencopts fixed_quant=3:qpel:gmc:trellis:vhq=4 -vf pullup,softskip -ofps 24000/1001

This will perform an inverse telecine transformation, reducing NTSC material from 30000/1001 (aka 29.97) fps to 24000/1001 (aka 23.976) fps.

$ mencoder -o movie.avi movie.mpg -nosound -nosub -noautosub -vf crop=720:416:0:80 -ovc x264 -x264encopts crf=18:preset=veryslow:threads=1

This encodes the video with the x264 codec with a single thread and the “veryslow” preset (which is indeed very slow) and crops the video (reducing the height to 416 pixels, starting at pixel 80).

Extract Subtitle Tracks

After tccat has finished copying the movie to your hard disk you can extract the subtitle tracks. This is necessary because they have to be converted before mkvmerge can handle them.

$ tcextract -i movie.mpg -a 0x20 -x ps1 > movie.en.ps1

The ID of the subtitle track (here: 0x20) can be found in the output of lsdvd. Choose the subtitles you like and extract them one after another.

Now they have to be converted to the VobSub format:

$ subtitle2vobsub -p movie.en.ps1 -i movie.ifo -o movie.en

This will create the files movie.en.sub and movie.en.idx. Both are necessary for mkvmerge to do its magic.

We’re almost there.

Mux the Final Video

Now all that we have created has to be put together.

$ mkvmerge -o movie.mkv --title "Movie" --chapter-language eng --chapters movie.chap --compression 0:none movie.avi -D -a 1,2 --compression 1:none --language 1:eng --compression 2:none --language 2:ger movie.mpg --compression 0:none --language 0:eng --default-track 0:0 movie.en.idx --compression 0:none --language 0:ger --default-track 0:0 movie.de.idx

Yes, this command line needs some explaining.

-o movie.mkv
This is the output file that we are creating.
--title “Movie“
Here you can specify the title of the movie. This parameter is optional.
--chapter-language eng --chapters movie.chap
This includes the chapter definitions from the given file and also specifies that the lanuage of the chapters file is English.
--compression 0:none movie.avi
Includes all tracks from the given file in the output file. The “--compression 0:none” parameters tells mkvmerge to not use any kind of header compression. As we only stored a video track in movie.avi we do not need to restrict inclusion of this file. If you accidentally encoded an audio track as well, you need to specify “-A” before “movie.avi” - this will tell mkvmerge to not include any audio tracks.
-D -a 1,2 --compression 1:none --language 1:eng --compression 2:none --language 2:ger movie.mpg
These parameters tell mkvmerge to include no video track (“-D”) and audio tracks 1 and 2 (“-a 1,2”) from the file movie.mpg. This is our source file from the DVD and thus contains the MPEG-2 video stream that we reencoded earlier, as well as the original AC3 audio tracks that we are interested in. Note that the audio tracks are tracks 1 and 2 because track 0 in the file movie.mpg is the video track! Use “mkvmerge --identify movie.mpg” to list all tracks that mkvmerge recognizes. Also, these parameters tell mkvmerge that audio track 1 is English, and audio track 2 is German. If you want to include more than two audio tracks you need to repeat the respective parameters for all additional audio tracks.
--compression 0:none --language 0:eng --default-track 0:0 movie.en.idx
This includes all tracks from the movie.en.idx file which is a single subtitle track in the English language. The “--default-track 0:0” specifies that the included track with the index 0 (i.e. the only subtitle track in the file) is not a default track. This needs to be specified for all subtitles unless you really want to show a certain subtitle as default when a movie starts. This can be used to include forced subtitles for additional foreign languages spoken in a movie, e.g. the Na’vi language in “Avatar”, or Elfish in “The Lord of the Rings.”
--compression 0:none --language 0:ger --default-track 0:0 movie.de.idx
Includes the German subtitles.

Voilá, movie.mkv now contains your movie, all audio tracks, and all subtitle tracks that you want to be in there.