How to batch process videos using HandBrake and Hazel

I’ve tried just about every video encoding product for MacOS X, and always keep returning to the venerable HandBrake.

Why haven’t I just stuck with HandBrake in the first place? One reason — its UI for batch converting videos sucks. Unlike all other products, you can’t just drag a bunch of videos into HandBrake. No, you have to chose them one at a time, and manually add them to the HandBrake queue.

Why haven’t I stuck with one of the other products? Because none of them have presets that are as good as HandBrake’s, and I couldn’t be bothered to learn the FFMPEG syntax. For example, all the presets in something like RoadMovie will change the resolution of the encoded video. HandBrake’s “Normal” preset will preserve the video’s original dimensions.

This weekend, with the help of Super-Makalu Justin Driscoll (@jdriscoll), I finally created a batch processing system, based on HandBrake. This article documents the setup.

What you need

  • (HandBrakeCLI). This is a version of HandBrake that can be run from the command line. Don’t expect the “doc” folder that comes with this utility to actually tell you how to install it. That’d be asking too much. Fire up Terminal, navigate to wherever HandBrakeCLI is, and move it to your /usr/bin directory with this command (you’ll be asked for your admin password).
  sudo mv ./HandBrakeCLI /usr/bin
  • Hazel. Hazel is a System Preference utility for the MacOS X, that allows you to schedule regular actions to be made on the contents of folders. Download and install.
  • This bash shell script, which you can copy and paste later from Pastie.

Basic idea

I have the following folder structure:

  /Users/mhenders/Hazel/Videos/input-ipod
  /Users/mhenders/Hazel/Videos/out-ipod
  /Users/mhenders/Hazel/Videos/processed

We’re going to tell Hazel to watch the “input-ipod” folder, and whenever it finds a new video file, to process that file with HandBrakeCLI, and then move the original into the “processed” folder.

Details

  • Once Hazel is installed, add the “input-ipod” folder to its watch list.
  • On that folder, create the following rule with two steps: (1) the first runs an embedded shell script (passing each video file to HandBrakeCLI for processing) and, (2) when done, move the original video to the “processed” folder, which you can later delete or whatever.

One setup, all you have to do is drop videos into the “input-ipod” folder, wait a few moments, and watch Hazel kick into action. I set this up on our 8-core iMac at home, and watched it blow through 100 videos. Yeah!

Caveats

The Hazel embedded script will fail if the original video has spaces in the name. So be sure to change “My Home Video.mov” to “My-Home-Video.mov” before dropping into the input folder. I haven’t taken the time to sort that out yet, but will update this blog when I do.

Enhancements

You’ll notice that I’ve named my watch folder “input-ipod”. As you can imagine, I have other input folders named “input-appletv” and “input-normal”, for dropping videos I want processed with HandBrake’s “AppleTV 2” and “Normal” presets. I modify the Hazel embedded script accordingly for those rules. A complete list of HandBrake’s presets can be found here.

Hope this article helps others wanting to do the same. If you do, I’d love for you to drop a “Hello” message in the comments below.

7 thoughts on “How to batch process videos using HandBrake and Hazel”

  1. Your problem with filenames with embedded spaces should be resolved by putting “” around $output_filename in the HandBrakeCLI command line.

  2. Hi,

    Thanks, very helpful…however, I am still not getting HandbrakeCLI to convert when there are spaces in the input file name ie., My New Movie.avi

    I’ve tried the double quotes around the $output_filename but no joy…any suggestions?

    Luis

  3. Hi,

    Thanks, very helpful…however, I am still not getting HandbrakeCLI to convert when there are spaces in the input file name ie., My New Movie.avi

    I’ve tried the double quotes around the $output_filename but no joy…any suggestions?

    The only difference I changed the preset to “iPhone 4”

    Thanks

    Luis

  4. Hi Luis,

    I modified Matts script a little bit to the following:

    — SCRIPT START —

    dir=”converted” filename=$(basename “$1″) file=${filename%.*} output_filename=”$file.mp4”

    HandBrakeCLI -i “$1” -o “$output_filename” -e x264 -q 20.0 -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 -f mp4 –detelecine –decomb –loose-anamorphic -m -x b-adapt=2:rc-lookahead=50

    mkdir -p $dir

    if [ -f “$outputfilename” ] then mv “$outputfilename” “$dir/$output_filename” rm “$1” fi

    — SCRIPT-END —

    This works quite well even if the names contain spaces.

Agree? Disagree? What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.