Converting a video for the Apple iPhone on MacOSX using HandbrakeCLI

After using various shell scripts with either mencoder or ffmpeg I have given up on trying to find “the perfect” (read: anything to mp4) setup.

It will work most of the time but every now and then you happen to get an input file
which will not transcode to a mp4 file which the iPod or the iPhone (read: iTunes) will accept.

Thankfully, there is now one simple solution available: Handbrake.

When using the command line version, media transcoding becomes very simple:

$ HandBrakeCLI -Z "Apple iPhone & iPod Touch" -i "BigBuckBunny.avi" -o "BigBuckBunny.mp4"

If you have many files you want to encode, it can be a lot of work.
You can avoid this by generating a script containing the specific command line for each file.

As an example on how to do this, I will encode 2 movies, Big Buck Bunny and Elephants Dream.

At first, navigate to the Desktop

$ cd
$ cd Desktop

Download the 2 movies

$ wget
$ wget

To make things a little harder, the video file names may contain a whitespace
so for the purpose of the demonstration, one file will be renamed

$ mv ED_1024.avi "ED 1024.avi"

And now create the script that will do the encoding

$ for i in *.avi
> do
> newfilename=$(basename "${i}" .avi)
> echo "HandBrakeCLI -Z \"Apple iPhone & iPod Touch\" -i \"${i}\" -o \"${newfilename}.mp4\""
> done >>

The resulting file should look like this:

$ cat
HandBrakeCLI -Z "Apple iPhone & iPod Touch" -i "ED 1024.avi" -o "ED 1024.mp4"
HandBrakeCLI -Z "Apple iPhone & iPod Touch" -i "big_buck_bunny_480p_stereo.avi" -o "big_buck_bunny_480p_stereo.mp4"

And then just run it:

$ bash