April 24, 2006

CLI Magic: Porting DVDs with HandBrake

Author: Mayank Sharma

The multimedia capabilities of the handheld electronic devices we carry has increased over the last few years. Devices such as the iPod and even several cell phones have become music-oozing gadgets for people on the go. While you can copy your favourite MP3s directly into a handheld device, DVDs (if you have the right to copy them) take some effort, due to the fact that the devices have tiny screens and lack the processing power needed for playing high-quality DVDs. HandBrake,
designed initially for BeOS and then ported to Linux and Mac OS, helps encode DVDs for your portable device.

User Level: Beginner

HandBrake under Linux doesn't yet have a graphical user interface and is only accessible from the command line. Although it requires a lot of libraries, you don't have to worry about that. Jam, a software build tool that makes building programs simple, takes care of downloading all the libraries and compiling them. Just make sure you have gcc, g++, and nasm installed. Open a terminal and run the commands:

wget -c http://download.m0k.org/handbrake/HandBrake-0.7.1.tar.gz
tar xvf HandBrake-0.7.1.tar.gz
cd HandBrake-0.7.1

Depending on your connection speed and hardware, this could take upto 30 minutes. Once its done, gain root access (either through su or sudo) and copy the file you just created to a system directory:

cp HBTest /usr/local/bin/handbrake

It's also a good idea to install a versatile video player such as VLC.

Getting started

Before you begin encoding, make sure to note the location of your DVD drive and the folder where you want to keep the encoded videos. The source will be the VIDEO_TS folder that every DVD has, and in these examples the destination will be an encoded_movies directory under your home directory. This simple command will encode the entire DVD into a MPEG-4 file format:

handbrake -i /media/dvd/VIDEO_TS/ -o ~/encoded_movies/movie.mp4

Like any good CLI application, HandBrake is highly customizable. The command above will combine all chapters into one file. You can use the -c switch to select specific chapters that you want to encode:

handbrake -c 6 -i /media/dvd/VIDEO_TS/ -o ~/encoded_movies/movie_part6.mp4

This will only encode chapter 6. HandBrake will also accept a range of chapters, such as 2-4.

You can't play the videos you create with these commands on most portable devices because the devices have speed and resolution limits. For example, my Sony Ericsson K750i phone will best play MP4 videos at 176x144 at 25 frames/second with a bitrate of 192Kbps and audio sampled at 44100Hz at a bitrate of 128Kbps. To get a DVD video to play on my phone I use HandBrake with a lot more options:

handbrake -w 144 -h 176 -b 192 -r 25 -B 128 -R 44100 -i /media/dvd/VIDEO_TS/ -o ~/encoded_movies/for_phone_movie.mp4

The -w and -h options control the width and height of the video. If you specify only one of these, HandBrake calculates the other, keeping the aspect ratio constant. -b sets the video bitrate; by default it's 1000Kbps. -r keeps track of the video frame rate. -B is responsible for the audio bit rate, and defaults to 128. -R sets the audio sample rate. Some options, such as -r and -R, have a limited set of rates they can take. You can see the choices by typing handbrake -h.

More encoding

In addition to making portable videos out of DVDs, you can use HandBrake like any other DVD ripper to store videos on your hard disk. It can output MP4, AVI, and OGM movies. You specify the format using the -f option. By default HandBrake detects the format from the output filename specified with -o.

If your DVD has multiple audio sources, such as an alternate language, you can choose to encode the output with these as well by specifying the audio channels with the -a option. Only the first audio channel is encoded by default. To get better quality movies, but at twice the encoding time, use the -2 switch. It performs much better compression, making sure those fast action scenes are crystal clear.

Handbrake is powerful encoding tool for making DVDs playable on portable devices and ripping and storing DVDs. It sports many command-line options to make it extremely controllable, and yet not so many as to scare away a new user.