Recently I’ve been working on some video I shot back in 2009 while in Whistler, Canada on a mountain biking trip.  I’ve been exporting them as fairly standard .AVI files because that produces good results but I’ve recently started adding them into my iTunes library so I can watch them on my Apple TV.  I use HandBrake to convert them to .M4V files which can then be imported into iTunes.  This is a manual process, though, so I set about trying to automate it without having to get my video editing program to export to .M4V (the results weren’t very good for some reason).

Hazel

Using Hazel from NoodleSoft, I’ve setup some rules so that when a file of a particular type appears in a selected folder, it runs a Bash script to convert the video and put it into ~/Movies/iTunes.  Hazel isn’t free but is worth every cent of the $21.95 USD that I paid for it.  Anyway, if you’ve got Hazel you’ll probably know how to add a folder rule so I won’t go into that here, although the screenshot below should be all you need.  Here’s the script I run from Hazel.

FILEIN=$1
FILEOUT=`echo "$FILEIN" | awk -F. '{ for (i=1;i<NF;i++) printf $i"."; printf "m4v"}'`
HandBrakeCLI --preset "AppleTV 2" --input "$FILEIN" --output "$FILEOUT"
mv "$FILEOUT" ~/Movies/iTunes/
mv "$FILEIN" ~/.Trash/

Important note: I’ve just noticed that the Syntax Highlighter plugin kinda breaks the code if it has certain characters in it.  If you’re going to copy the script above, change &lt; to the < symbol.

And here’s a screenshot of how my Hazel rule’s configuration looks.

Hazel rule
Hazel rule to run HandBrakeCLI

That script will set a variable called FILEIN to the name of the file being processed, set a variable called FILEOUT to the same filename but with the extension changed to "m4v" without the extension then, using those variables, run HandBrakeCLI to convert the video using the built-in preset called "AppleTV 2".  The processed video is then moved to the Movies/iTunes folder in my home directory and the source file is moved to the trash.  Note that the file isn’t deleted per se, just moved to the trash.  I might want it back later, who knows …

HandBrakeCLI

The second part, of course, is that you need HandBrakeCLI before the script above will work.  I’ve got HandBrakeCLI in ~/_Applications but you can put it anywhere.  Don’t forget to specify the full path to HandBrakeCLI unless you add the directory name to your PATH environment variable.  HandBrakeCLI is in my PATH so I haven’t specified it above.

The Results

So what happens?  Once a file had finished exporting into the appropriate folder, Hazel realises it’s there and runs the script that calls HandBrakeCLI.  The command line options specify that the video should be output in a format that’s compatible with Apple TV, and therefore iTunes.  I could use the iTunes "Automatlcally Add to iTunes" folder but I’ve decided not to at this point.

I’ve got this setup on my Mac Mini server and it just sits there, patiently waiting for the .AVI files to appear in the appropriate folder.  Easy.