Saturday, December 06, 2008

some media learnings

I have a lot of computers and media floating around at home and last weekend it was once again time to build some tools to better manage them. Wrote some Ruby and used rb-appscript to bend iTunes to my will. Along the way learned a few lessons:

First, ASTranslate is teh shizznitz.  It takes a pile of AppleScript and spits out working rb-appscript.  Since I really wanted to minimize the amount of AppleScript I needed to learn -- and wanted to be able to work my magic on headless machines, this worked quite well.  Let's say you want to take a Quicktime reference movie, insert it into iTunes, and then trick iTunes into managing it like a TV show:

episode = app('iTunes').add(MacTypes::Alias.path("#{path}/#{name}"))
if episode
  episode.video_kind.set(:TV_show)
  episode.show.set(name)
  episode.episode_number.set(episode_num)
  episode.season_number.set(season_num)
end

Want to search TV shows?

app('iTunes').user_playlists["TV Shows"]. \
tracks[(its.name.contains(searchname))].get

Easy peasy. ASTranslate running next to Apple's Apple Script editor and Doug's amazing repository of Apple Scripts made it easy to dig through this stuff.

Second, reference movies. If you use Perian (you do have Perian installed, right?), Quicktime and Front Row will play all kinds of movie types. However, iTunes won't load them. Reference movies are snippets of XML that allow you to work around this:

def create_reference_movie(dir, name, season, destdir)
  def raw_urlencode(str) # need to escape reference name
    str.gsub(/[^a-zA-Z0-9_\.\-\/]/n) {|s| sprintf('%%%02x', s[0]) }
  end

  File.open("#{destdir}/#{refname}", "w") do |file|
    file.puts("<?xml version=\"1.0\"?>")
    file.puts("<?quicktime type=\"application/x-quicktime-media-link\"?>")
    file.puts("<embed src=\"file:#{raw_urlencode("#{dir}/#{name}")}\" />")
  end
end

That's right, three lines of XML that you can create -- by hand, even -- to let you drag drop odd formats into iTunes.

Third, a gotcha which makes all of this less cool. Even though iTunes can share and stream video, it turns out that it will not do this for reference movies. Boo!

Fourth, this is well known within the Ruby community, but multibyte string support in 1.8 is weak. Lost a few hours this afternoon to a bug that was obvious once the "oh, strings aren't UTF-8 yet!" thought hit me.

Thanksgiving was mostly about cooking but it was fun to get a bit of coding time in!

No comments: