x246 Encoding

I started encoding DVDs when my friend got an iPod Video in 2006 and he was trying to put videos on his iPod. Since then I've given up iPod support on my videos, and instead find myself trying some of the more fun modern options available to H264. Now I watch my videos through MythVideo, and I can take some movies on my usb key to watch on a PS3 or Xbox360 with my friends.

My scripts have evolved extensively, as I've used ffmpeg, mencoder, and now handbrake to perform my encoding. The first resource I used was on the Ubuntu Wiki, although now the article has become fragmented. The only thing which I pull from this page any more is dumping a DVD in Windows (ironically).

Reading DVDs

Handbrake has issues reading DVD materials on Windows. In Linux, you merely need to be able to watch a DVD (which can be a career on its own). In Windows, Handbrake complains with copy protected DVDs. In order to get around this, I use MPlayer to dump the DVD to a vob file first, then I will encode that.

In Windows, I use the command line version of MPlayer found at http://oss.netfarm.it/mplayer-win32.php. First, check there is enough space for the DVD (around 5 gigs), then navigate to the directory with mplayer.exe with the command line. Run the following command, adjusting "dvd://1" for whichever track you desire (feature films are normally 1):

mplayer.exe -dvd-device d: dvd://1 -dumpstream -dumpfile out.vob

This operation takes a long time, around 30 minutes, so don't kill the process thinking something went wrong. If you are having trouble finding which track the movie is on, you can use the beginning of the command without the dump arguments to watch the DVD.

Linux and Windows differences

I use Linux as my primary operating system, so after this section the following directions will be assuming Linux. Windows users will be able to follow along, however when I write a script, you will need to translate it into line by line commands for the command line. This involves replacing the file paths with appropriated drive lettering, ignoring or finding the Windows equivalent of the commands, and replacing the "$1" placeholder with the relevant information.

Some handy tricks in the scripts

Encoding video is very CPU intensive, and so the first thing most people do is give the task a high "nice" setting. I have this in my scripts (nice -n 17), however the scheduler in Linux is generally smart enough to handle CPU intensive tasks. The real problem is when a task is very I/O intensive, leaving simple tasks such as opening files painfully slow. "ionice -c3" compensates for this, so your computer is usable while Handbrake is running.

I also create a temp file, and move it when finished. This lets you start a new disk before renaming the old file. It is good habit to immediately rename these output files, as this script will not ask before overwriting.

echo -e "\\a" creates a system beep to let you know the rip is finished. Another way I've signalled a finished rip is by using sendEmail. I'll still use this when encoding a lot of files on my MythTV server. The standard form is something like this:

sendEmail -f "user@server.com" -t "user@server.com" -u "subject" -xu "user" -xp "password" -s "outgoingserver:port" -m "This is the message!"

Ripping in a dedicated GNU Screen is advised. Screen lets you spawn virtual terminals, which you can disconnect from and reconnect later. While it is much more powerful than that, I will SSH into my MythTV box and spawn a screen with the command:

screen -S moviescreen

This brings about a new terminal, where I start up Handbrake. In order to view output in the scrollback buffer (such as the language options discussed later), enter copy mode by holding ctrl, press a, then press [. The arrows navigate in this mode, and return to normal operation with esc. To disconnect from a screen, hold ctrl, then press a, then d. Later you can resume the screen with screen -r moviescreen.

Ripping a DVD with only English

Most movies I have are native to America, and so I ask Handbrake to use English audio, and scan to see if there is any short subs for foreign language segments. This script is run by ./handbrakescript 1, where "1" is the track number I am encoding. This is useful for episodic discs, where you can queue commands with a semicolon, e.g. ./handbrakescript 3; ./handbrakescript 4; ,/handbrakescript 5.

#!/bin/bash  
ionice -c3 nice -n 17 HandBrakeCLI -i /dev/sr0 -o temp.mp4 -e x264 -t "$1" -N eng -s scan -D 1.75 -q 0.600000023841858 -B 96 -O -R 48 -w 720 -E faac -5 -x keyint=300:keyint-min=30::bframes=7:b-adapt=2:b-pyramid=1:weightb:rc-lookahead=50:open-gop=normal:ratetol=5:direct=auto:trellis=1:ref=6::mixed-refs=1:direct=auto:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:nr=100:me=umh:subme=9:no-fast-pskip=1:filter=-2,-1  
mv temp.mp4 "$1".mp4  
echo -e "\\a"

Ripping a DVD with subtitles

Thanks to H.264 being able to handle soft subtitles, I can include multiple audio tracks and subtitles in foreign films. Subtitles and audio tracks have a corresponding number, which is visible in the Handbrake output at the beginning of a rip. I guess which tracks I will want (normally audio tracks 1,2 and subtitle 1), then I double check the language as the disk rips. An example output is as follows:

  \+ audio tracks:  
    \+ 1, English (AC3) (5.1 ch) (iso639-2: eng), 48000Hz, 448000bps  
    \+ 2, Japanese (AC3) (5.1 ch) (iso639-2: jpn), 48000Hz, 448000bps    
    \+ 3, English (AC3) (Dolby Surround) (iso639-2: eng), 48000Hz, 192000bps  
    \+ 4, Japanese (AC3) (Dolby Surround) (iso639-2: jpn), 48000Hz, 192000bps  
    \+ 5, Japanese (AC3) (Dolby Surround) (iso639-2: jpn), 48000Hz, 192000bps  
  \+ subtitle tracks:  
    \+ 1, English (iso639-2: eng) (Bitmap)(VOBSUB)  
    \+ 2, English (iso639-2: eng) (Bitmap)(VOBSUB)  
    \+ 3, English (iso639-2: eng) (Bitmap)(VOBSUB)

The actual script is as follows, using the track argument as seen above.

#!/bin/bash  
ionice -c3 nice -n 17 HandBrakeCLI -i /dev/sr0 -o temp.mp4 -e x264 -t "$1" -a 1,2 -s 1 -D 1.75 -q 0.600000023841858 -B 96 -O -R 48 -w 720 -N eng -E faac -5 -x keyint=300:keyint-min=30::bframes=7:b-adapt=2:b-pyramid=1:weightb:rc-lookahead=50:open-gop=normal:ratetol=5:direct=auto:trellis=1:ref=6::mixed-refs=1:direct=auto:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:nr=100:me=umh:subme=9:no-fast-pskip=1:filter=-2,-1  
mv temp.mp4 "$1".mp4  
echo -e "\\a"

Ripping a file

I pull most of my media off of DVD's, however I have used this command for archiving things such as home movies. This command is the most similar to what is used on a Windows computer. The argument is the input file.

#!/bin/bash  
ionice -c3 nice -n 17 HandBrakeCLI -i "$1" -o temp.mp4 -e x264 -D 1.75 -q 0.600000023841858 -B 96 -O -R 48 -w 720 -E faac -5 -x keyint=300:keyint-min=30::bframes=7:b-adapt=2:b-pyramid=1:weightb:rc-lookahead=50:open-gop=normal:ratetol=5:direct=auto:trellis=1:ref=6::mixed-refs=1:direct=auto:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:nr=100:me=umh:subme=9:no-fast-pskip=1:filter=-2,-1  
mv temp.mp4 "$1".mp4  
echo -e "\\a"

Closing and advanced options

My scripts are definitely a product of trial and error, and probably have some legacy features enabled which I don't need any more. If this tutorial seems daunting, the default settings in Handbrake work very well (if not better), and the GUI is much more intuitive than my command line scripts. The only reason I prefer my script to Handbrake's defaults is my qualitative evaluation of superior quality in the target file size.

My target file size for a full length movie is from 500 megabytes and 1.5 gigabytes, which is why I use the constant quality settings. My -q value is left over from an old Handbrake present I copied, and I am unsure how to translate it into the GUI for comparison.

Important audio options are bit rate -B 96 and encoder -E faac. AAC is an advanced encoder, let it do some work and drop the bit rate! The other audio option which is important is -D 1.75, which makes soft sounds louder. I feel like I'm always turning the volume up or down in a film depending if music is playing or people are talking, and this helps. A higher value is more aggressive, 1.75 is not very noticeable.

The horizontal resolution of -w 720 is specified so that the DVD native resolution is passed through. Remember that a DVD is not high definition, so setting the resolution for 720p will gain nothing aside from increase the file size.

In the advanced x264 options, I try to favour aggressive, experimental options. Some options you may want to change or may cause issues are as follows: open-gop causes problems with some decoders, cabac used to be troublesome with Apple products, b-pyramids could have troubles products older than two years. A good source for details on these options are found on the MeWiki

Previous Post Next Post