Podcast tracks to iTunes Library script

For the past few days, I've subscribed to the excellent Song of the Day podcast at KEXP. I wanted to move the "good ones" to my iTunes Library, but unfortunately, iTunes does some odd things with podcast files, so this isn't as easy as you might guess.* The way iTunes marks the files, even if you move them in-and-out of iTunes, they will always show up on the Podcasts library, instead of the Music library.

After a bit of googling, the best trick I could find was to convert the id3 tags to version 1.0, which appears to remove the "magical tag" iTunes uses to match the files with the appropriate podcast. However, in id3 version 1.0, tags are severely limited in length, leading to titles like "Black Kids – I'm Not Gonna Tea". Not acceptable, says I.

More googling turned up the id3lib library, an open-source library for reading and editing id3 tags. There's even a ruby front-end. So, I wrote my own podcast re-wirer script in ruby:

#!/usr/bin/env ruby
require 'rubygems'
require 'id3lib'

files = Dir["*.mp3"]
files.each do |filename|
  mp3 = ID3Lib::Tag.new filename
  text = mp3.detect { |t| t[:id] == :TIT3 }
  # raise text.inspect
  mp3.delete_if { |t| [:TIT3, :COMM].include? t[:id] }
  mp3.comment = text[:text]
  mp3.genre = 'Indie'
  if mp3.album =~ /^KEXP/
    artist = mp3.artist
    artist.gsub!(/^[\d]+ (.*)/, '\1')
    mp3.artist = artist
  end
  title = mp3.title.split(/ - /)
  title.delete_at(0)
  mp3.title = title.join(' - ')

  mp3.update!
  puts " ... converted '#{filename}'"
end

*Disclaimer: Under iTunes' Advanced menu sits the "Convert to mp3…" command. There's a chance this might do what I wanted, but there's also the risk that it would re-encode the mp3, leading to an unacceptable loss of quality. Since I didn't think I'd be able to reliably detect the degradation, I considered this a non-option.

Permalink • Posted in: itunes, music, programming, ruby