You’re reading an article by Jay Robinson, a designer living in California.

Optimizing Videos for the Web with HandBrakeCLI

Hand­Brake is an excel­lent appli­ca­tion for con­vert­ing your favorite Flash files, VOB fold­ers, or almost any­thing else really, for use on your iPhone, iPod or AppleTV.

But it also has another great use: cre­at­ing videos for the web. The GUI is a lit­tle clunky for this pur­pose and doesn’t even have what you need. I pre­fer to use the CLI, or Com­mand Line Inter­face. On a Mac, use Terminal.app to access the com­mand line.

I often use Hand­Brake to take client’s video files and add them to their web­site in a for­mat that streams quickly and looks great. Hand­Brake­CLI takes a crummy Win­dows Media video file and turns it into some­thing awe­some for the web. Here is my command:

./HandBrakeCLI -i ~/Desktop/my_original_file.wmv -o ~/Desktop/my_new_file.mp4 --encoder x264 --vb 1800 --ab 128 --maxWidth 640 --maxHeight 480 --two-pass --optimize;

Expla­na­tion

Let’s explain this com­mand and ease your appre­hen­sion about using the com­mand line by break­ing down each indi­vid­ual argu­ment in the com­mand, line-by-line:

 1) ./HandBrakeCLI 
 2)     -i ~/Desktop/my_original_file.wmv 
 3)     -o ~/Desktop/my_new_file.mp4 
 4)     --encoder x264 
 5)     --vb 1800 
 6)     --ab 128 
 7)     --maxWidth 640 
 8)     --maxHeight 480
 9)     --two-pass 
10)     --optimize;
  1. Before you run your com­mand, use the Change Direc­tory com­mand (cd path/to/handbrakecli) to make sure you are in the same folder as the Hand­Brake­CLI file is located. Tech­ni­cally, this file path start­ing with ./ means to stay in the cur­rent directory.
  2. The -i means input file. The next part lists the path to your orig­i­nal video file you’d like to con­vert (I find it’s eas­i­est to work from the Desktop).
  3. The -o means out­put file. The next part defines the path where you’d like to place your final con­verted video. Tech­ni­cally, this file path start­ing with ~/Desktop means to the Desk­top folder of the cur­rent user (you!).
  4. The best codec for the web is H.264 video, using x264 encoder.
  5. Video Bit Rate is spec­i­fied in kilo­bits per second.
  6. Audio Bit Rate is spec­i­fied in kilo­bits per sec­ond. For most small videos, 128 is fine. If the video is mostly talk­ing go ahead and bump it down to 96.
  7. Set the max­i­mum width.
  8. Set the max­i­mum height. The best part about these two lines is that the aspect ratio will not be changed! Hand­Brake­CLI will try to fill the larger value with­out exceed­ing the latter.
  9. Two passes over the video is bet­ter than one.
  10. Your client won’t give a jack how fancy you think you are unless you include the most impor­tant part: --optimize. Opti­mize means mov­ing impor­tant data to the begin­ning of the file so it’s down­loaded first, allow­ing the video to start quickly.

It’s all smooth sail­ing from here. If you want to process many videos in one go, just sep­a­rate the com­mands by putting a semi-colon at the end before you start a new one.

Resources

To try this out, you’ll need to down­load Hand­Brake and the Hand­Brake Com­mand Line Inter­face.

For a quick idea of best prac­tices for com­pres­sion, I like Vimeo’s com­pres­sion guide­lines. If you want to know more about codecs and video on the Web in gen­eral, check out Mark Pilgrim’s awe­some overview at Dive Into HTML5.

Short URL: http://jwr.cc/x/1t