Convert AVI files for AppleTV

Once you’ve setup Home Sharing so you can play movies from iTunes on the AppleTV, you’ll notice that some movie types won’t play.

Handbrake is a free tool that can convert avi and other video type to mp4 movies that will play on the AppleTV.  But conversion of more than a few is tedious using the app.

It turns out there is a HandBrake CLI tool you can  download for doing batch conversions.

Change into the directory of files you want to convert. (It is okay if there are subdirectories you also want to process.)

cd /Volumes/Data/Movies

Here is the command I am using for the conversion*:

find . -type file
  -exec \
  /Volumes/HandBrake-0.10.5-MacOSX.6_CLI_x86_64/HandBrakeCLI \
  -i "{}" \
  -o "{}-AppleTV.mp4" \
  --preset="AppleTV" \;

That command will find all files and attempt to convert them using the ‘AppleTV’ preset.

It will output them in the same directory as the original with the suffix “-AppleTV.mp4”.

Of course if you only want to handle certain file types you can change or add to the options to the find command  (-name “*.avi” for example).

To clean up the original files after you are happy with the converted files:

IFS=$'\n';for orig in $(find . -name "*-AppleTV.mp4" | sed -e 's/-AppleTV.mp4//g'); do rm -i ${orig}; done

And finally you can rename the new converted files to remove the old file extension and “-AppleTV”:

IFS=$'\n';for new in $(find . -name "*-AppleTV.mp4"); do mv -i "$new" "$(echo $new | sed 's/\....-AppleTV.mp4/.mp4/g')"; done

*Use your own path to the HandBrakeCLI. I just dragged it into Terminal from the downloaded dmg.

Launchd Tools Released

Launchd Tools are for reading and creating launchd jobs.

For example, to see info about all Apple LaunchAgents and LaunchDaemons.

launchd2cmd /System/Library/Launch*/*

Or to create your own launchd job from an existing command:

cmd2launchd /usr/local/bin/daemond -d --mode foreground

Check it out here: https://github.com/kcrawford/launchd_tools

convert lines to comma separated items with tr

If you have output that is separated by new lines, but you really want it formatted into a single line with commas as separators or maybe a space as a separator, just pipe to tr.

Here is a simple example:

kserver:~ patternbuffer$ diskutil list | grep ^/dev
/dev/disk0
/dev/disk1

If we want them separated by spaces, we could do:

kserver:~ patternbuffer$ diskutil list | grep ^/dev | tr '\n' ' '
/dev/disk0 /dev/disk1

Note that there is a newline on the end of the output, so that trailing newline is also translated. So if you don’t want it there, you’ll have to chomp it off. I use sed, but use what you like.

Here is the same as above, but with commas, with sed to remove the trailing comma.

kserver:~ patternbuffer$ diskutil list | grep ^/dev | tr '\n' ',' | sed 's/.$//'
/dev/disk0,/dev/disk1