Hack 60. Encode a DVD to MPEG4 from the Command Line
Use mencoder 2-pass encoding to encode VOB files to MPEG4 .avi files (and other formats), all from the command line.
If you travel with a laptop over long distances, you might decide to take some DVDs along with you to pass the time. Of course, optical drives require a lot of power, and on some laptops you might not have enough power to get through an entire movie. However, if you rip and then encode the movie to a smaller format ahead of time, you can store a number of movies on your laptop and watch them directly from the hard drive, which saves a considerable amount of battery life, because you don't need to spin the DVD. This hack talks about how to encode a DVD to an MPEG4 (also known as "DivX") .avi file from the command line. This hack assumes that you have already ripped the DVD to your hard drive, as explained in [Hack #59].
There are a few DVD-encoding command-line tools available to Linux users, but one of the simpler methods involves 2-pass encoding with mencoder, part of the MPlayer project. If you don't have mencoder installed yet, follow the installation instructions in "Use MPlayer" [Hack #48] and be sure to grab mencoder packages along with your MPlayer packages. mencoder's 2-pass encoding actually processes the video twice using information gained in the first pass to create a final video with much better image quality in the second pass for about the same file size.
I provide several encoding scenarios in this hack, all of which assume that you have already ripped the DVD to a directory on the filesystem. For each example, you will need to choose a chapter from the DVD to rip. Often the main feature of a DVD is on chapter one, but there are exceptions. Each chapter will have the format VTS_[Chapter]_[Section].VOB, so chapter one, section one of a DVD would be named VTS_01_1.VOB. You can usually identify the main feature of a DVD just by looking at the file sizes of each chapter's filesthe largest group is probably the main feature. If in doubt, open up the DVD in your favorite video player application and check the different chapters to see. In the following examples, I will encode all of chapter one from a DVD I have ripped into ~/example/VIDEO_TS, so change the chapter and file paths to suit your DVD.
3.15.1. Full Quality Encoding with MP3 Audio
The simplest method for 2-pass encoding encodes the DVD at the full bitrate (or how many bits you can use to store a second of video). This method will create a larger resulting file, but it won't require a preliminary step of calculating a bitrate to create an output file of a specific size. This requires two different mencoder commands, one for each pass:
$ cat ~/example/VIDEO_TS/VTS_01*.VOB | mencoder -oac mp3lame -ovc lavc - \ lavcopts vcodec=mpeg4:vpass=1 -o example.avi $ cat ~/example/VIDEO_TS/VTS_01*.VOB | mencoder -oac mp3lame -ovc lavc - \ lavcopts vcodec=mpeg4:vpass=2 -o example.avi -
These commands concatenate all the VOB files for chapter one as input for mencoder. mencoder sets the output audio codec (-oac) to MP3, and sets the output video codec to MPEG4 (vcodec=mpeg4). At the end of the second pass example.avi will be finished and ready to play. On my 1.2 GHz Pentium-M processor, 2-pass encoding usually takes twice as long as the length of the movie, so a two-hour movie takes four hours to encode.
3.15.2. Encode with Specified Bitrates
If space is a concern, you will likely want to specify a particular bitrate for mencoder to use so you can create a smaller output file. Although you could figure out which bitrate to use by picking different bitrates at random and seeing how big the resulting file is, or possibly through calculation, mencoder can calculate some acceptable bitrates for you. This calculation will add some extra time (on a 1.2 GHz Pentium-M, it adds 15 to 20 minutes for an average movie) to the process, but after you use it a few times you will probably notice a range of bitrates that fit with the file size you want. The first step is to remove any previous temporary files created by the calculation process and then launch the special mencoder calculation command:
$ rm frameno.avi $ cat ~/example/VIDEO_TS/VTS_01*.VOB | mencoder -oac mp3lame -ovc frameno -o \ frameno.avi …
Recommended video bitrate for 650MB CD: 710 Recommended video bitrate for 700MB CD: 785 Recommended video bitrate for 800MB CD: 934 Recommended video bitrate for 2 x 650MB CD: 1679 Recommended video bitrate for 2 x 700MB CD: 1828 Recommended video bitrate for 2 x 800MB CD: 2126
Text will scroll by as mencoder processes through the video. The last lines of the output will give you rough estimates for bitrates to use for 600, 700, and 800 MB files (suitable for CDs). After you have chosen which bitrate to use, run the same 2-pass encoding commands as before, but with the addition of the vbitrate option (in this example 780):
$ cat ~/example/VIDEO_TS/VTS_01*.VOB | mencoder -oac mp3lame -ovc lavc -\ lavcopts vcodec=mpeg4:vpass=1:vbitrate=780 -o example.avi $ cat ~/example/VIDEO_TS/VTS_01*.VOB | mencoder -oac mp3lame -ovc lavc -\ -lavcopts vcodec=mpeg4:vpass=2:vbitrate=780 -o example.avi-
mencoder will process through the video as in the previous example, only this time with an output file that meets your space requirements.