Brendon Walsh

Automating Mobile Entertainment


It has been several weeks since my first "Automating Home Entertainment" post and I feel it's time for a follow-up. Last time, I focussed on my automated TV workflows for my home computers with Boxee. Hence, "home" entertainment. This time I'd like to pay attention to mobile entertainment and what I do with podcasts and other media on devices such as my iPad or iPhone.

Git Repositories

First, a small note on relevance. Because these scripts are continually evolving, I've decided to move all of them to their own code repositories where people can keep up with code revisions as well as fork and publish their own revisions. This is my little way of solving the blogging problem as discussed over at Ordered List.

You can check out all my code repositories and activity at GitHub and Gists at GitHub.

HD Podcasts on iPad

The iPad has a gorgeous screen for watching video, however it seems that not all of my HD podcasts have compatible feeds for the iPad yet. Revision3 specifically hasn't updated their feeds to support the iPad as of the time of this writing. So rather than revert back to lower quality feeds that will sync through iTunes, I decided to write a script that would solve this problem for me.

I make no apologies for how much of a dirty hack these couple of scripts are that solve this problem for me. They are only temporary while Revision3 sorts out all of the encoding issues with the iPad right now.

iPad HD Podcasts

Apart from the Growl reference, the only other prerequisite for this script is MediaInfo.



cd ~/Music/iTunes/iTunes\ Media/Podcasts
array=(`find . \( -iname "*.mp4" -o -iname "*.mov" -o -iname "*.m4v" \)`)
for i in "${array[@]}"
	cd ~/Music/iTunes/iTunes\ Media/Podcasts
	folder=$(echo $path | cut -d / -f2)
	file=$(echo $path | cut -d / -f3)
	cd "$folder"
	profile=$(mediainfo "$file" | grep "Format profile" | grep -o 3.2)
	if [ "$profile" == "3.2" ]
		HandBrakeCLI --preset="High Profile" -e x264 -b 2500 -E faac -B 160 --mixdown stereo -X 1280 -Y 800 -i "$file" -o new.mp4
		rm "$file"
		mv new.mp4 "$file"
		exec osascript ~/Dropbox/Personal/Scripts/Refresh\ Podcasts.scpt &
		growlnotify --image ~/Dropbox/Personal/Scripts/Podcasts/"$folder".jpg -n iTunes -t "Podcast Added" -m "$file"

This first script examines the folder where iTunes podcasts are downloaded and then checks all video podcasts for Profile Formats that exceed High Level 3.1. While the iPad is technically capable of playing these files, iTunes does not like to sync them, so we pass it along to HandBrake to do it's thing and produce a messy, yet otherwise similar video to the original. Then we delete the original and move this new file in it's place. There's an AppleScript called just before a Growl notification. Let's see what that Applescript is doing...

tell application "iTunes"
	set lib to user playlist "Podcasts"
	repeat with t from (count of file tracks of lib) to 1 by -1
		set podcastfile to file track t of lib
			set loca to podcastfile's location as string
			set dbid to podcastfile's database ID
			if loca contains "mp4" and podcastfile's played count is not greater than 0 then
				delete (some track of library playlist 1 whose database ID is dbid)
				add loca to user playlist "Podcasts"
			end if
		end try
	end repeat
end tell

This AppleScript checks all the files in my iTunes podcast library and deletes all podcasts that haven't been played yet which are video files. Then it re-adds these files back into iTunes. Seems kind of pointless on the surface, but iTunes actually needs this little refresh in order to recognize that the once incompatible video files are now compatible with the iPad.

There are some issues with this method, like the fact that iTunes adds these videos back in as movies rather than podcasts. But like I said before, it's only temporary, so I'm happy with it.

I run the first shell script in cron each night so that by the time I wake up, all my shows are ready to be synced and leave the house with me.

More TV Automation

Aside from the other various code revisions on the automated TV scripts previously, I have also added in a feature to transcode each show to an iPad compatible format when it reaches my machine. After which, the newly created file takes it's place amongst the other episodes for said series.

It is the move to this folder where much of the magic happens. I have a Folder Action in place which watches any files added to these folders and then adds them to iTunes as TV Shows complete with metadata.

on adding folder items to this_folder after receiving these_items
		repeat with i from 1 to number of items in these_items
			set this_item to item i of these_items
			set the item_info to the info for this_item

			set filepath to path of this_item

			set AppleScript's text item delimiters to " - "
			set series to (text items 1 thru 1 of this_item) as text

			set episodeName to (text items 3 thru 3 of this_item) as text
			set AppleScript's text item delimiters to ".mp4"
			set episodeName to (text items 1 thru 1 of episodeName) as text

			set AppleScript's text item delimiters to " - S"
			set season to (text items 2 thru 2 of this_item) as text
			set AppleScript's text item delimiters to "E"
			set season to (text items 1 thru 1 of this_item) as text

			set AppleScript's text item delimiters to " - S" & season & "E"
			set episodeNumber to (text items 2 thru 2 of this_item) as text
			set AppleScript's text item delimiters to " - "
			set episodeNumber to (text items 1 thru 1 of episodeNumber) as text

			tell application "iTunes"
				copy (add filepath) to newMovie
				set video kind of newMovie to TV show
				set ((season number) of newMovie) to (season as number)
				set show of newMovie to series
				set (name of newMovie) to episodeName
				set ((episode ID) of (newMovie)) to "S" & season & "E" & episodeNumber
				set ((episode number) of newMovie) to (episodeNumber as number)
				set ((sort show) of (newMovie)) to series & " S" & season & "E" & episodeNumber
			end tell
		end repeat
	end try
end adding folder items to

More Automation Projects

That's right, the obsessions don't end here. Here's a sneak peek at other automation projects I've been working on:

Blu-Ray is my priority at the moment considering how time consuming the whole process is. I'd be very happy if I could get it close to as simple as inserting the disc and have it eject several hours later when the process is done. But that's still a ways off.

I Want Your Feedback

I want to know what you think of these formats of posts. Do you find them interesting or worthwhile? Perhaps you have some recommendations on how these workflows and scripts could be improved or other things I could be scripting that I haven't thought of yet. Let me know in the comments.

Also, how do you feel about the crazy video banner instead of the typical image? Personally, I like the idea of doing short video clips in that place, but I'm not sure how others feel about the looping or the video content itself. Is it too long? Too short?