Batch Encoding Streaming Videos with HandBrake

HandBrake is a free open-source tool for encoding video files to x264/mp4, perfect for FLV or HTML5 streaming playback over the internet. The GUI interface let's you use a predefined encoding profile, or tweak each setting individually. This is fairly easy-to-use and after a bit of trial-and-error you should find a configuration that works best for your needs.

If you have a lot of videos to convert though, you need to add them to the GUI interface one-by-one, tweak some settings for each (like the size, which doesn't get carried over when adding a new video), and add them to the queue. Then click encode on the queue window. This is fairly tedious and time-consuming, especially when you have say 50 videos or more, scattered in different directories.

Thankfully though, HandBrake has good command line support, so it's possible to do all this with a single command. The trick is to first use the GUI to experiment and find the best configuration settings for your needs (compromise between size and quality). Then click on the Console button in the GUI and click to encode one video. The HandBrake Console window will print out the command-line query used to encode the video. You can use this from a window command-prompt to achieve exactly the same thing.

The next step is to write a windows command that recursively searches for all video files in a given directory tree and runs this HandBrake query on each file. Here is an example of this:

for /r %x in (*.avi) do "C:\Program Files (x86)\Handbrake\HandBrakeCLI.exe" -i "%x" -o "c:\temp\%~nx.m4v" -t 1 -c 1 -f mp4 -O  -w 700 --loose-anamorphic  -e x264 -q 28 -a 1 -E faac -6 stereo -R Auto -B 128 -D 0.0 -x ref=2:bframes=2:subq=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0 --verbose=1

The way to use this is to navigate to the parent directory containing all your videos in a windows command-prompt, then just copy-paste the above in. It will search for all .avi files in the directory tree, and encode them using HandBreak, storing the encoded files in c:\temp, and changing the extension to .mv4.

You can modify the command as needed. Check the Windows Command Line reference documentation for all the parameters available and to see how they work.

Oh, and in case you've ever wandered what does the Web Optimized checkbox do on the HandBrake GUI, it moves the video metadata container from the end of the file to the start of the file. The metadata contains information such as movie length, key-frame indexes, etc. When streaming a video over the internet, the client player needs to read the metadata before the video can start playing. If the metadata is at the end of the file, the whole file needs to be downloaded before playback can begin. This would really suck, especially for big files. Moving the metadata to the start of the file leads to almost instant playback while the file is still downloading.


Popular posts from this blog

Wkhtmltopdf font and sizing issues

Import Google Contacts to Nokia PC Suite

Can't delete last blank page from Word