Optimizing Videos for the Web with HandBrakeCLI
HandBrake is an excellent application for converting your favorite Flash files, VOB folders, or almost anything else really, for use on your iPhone, iPod or AppleTV.
But it also has another great use: creating videos for the web. The GUI is a little clunky for this purpose and doesn’t even have what you need. I prefer to use the CLI, or Command Line Interface. On a Mac, use Terminal.app to access the command line.
I often use HandBrake to take client’s video files and add them to their website in a format that streams quickly and looks great. HandBrakeCLI takes a crummy Windows Media video file and turns it into something awesome 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;
Let’s explain this command and ease your apprehension about using the command line by breaking down each individual argument in the command, 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;
- Before you run your command, use the Change Directory command (
cd path/to/handbrakecli) to make sure you are in the same folder as the HandBrakeCLI file is located. Technically, this file path starting with
./means to stay in the current directory.
-imeans input file. The next part lists the path to your original video file you’d like to convert (I find it’s easiest to work from the Desktop).
-omeans output file. The next part defines the path where you’d like to place your final converted video. Technically, this file path starting with
~/Desktopmeans to the Desktop folder of the current user (you!).
- The best codec for the web is H.264 video, using x264 encoder.
- Video Bit Rate is specified in kilobits per second.
- Audio Bit Rate is specified in kilobits per second. For most small videos, 128 is fine. If the video is mostly talking go ahead and bump it down to 96.
- Set the maximum width.
- Set the maximum height. The best part about these two lines is that the aspect ratio will not be changed! HandBrakeCLI will try to fill the larger value without exceeding the latter.
- Two passes over the video is better than one.
- Your client won’t give a jack how fancy you think you are unless you include the most important part:
--optimize. Optimize means moving important data to the beginning of the file so it’s downloaded first, allowing the video to start quickly.
It’s all smooth sailing from here. If you want to process many videos in one go, just separate the commands by putting a semi-colon at the end before you start a new one.
For a quick idea of best practices for compression, I like Vimeo’s compression guidelines. If you want to know more about codecs and video on the Web in general, check out Mark Pilgrim’s awesome overview at Dive Into HTML5.
Short URL: http://jwr.cc/x/1t