For the npr shows that don’t podcast, I use icecream to save them off for my own time shifting. The files end up with names like “car_talk_2008_01_17.mp3”. Until recently, that was good enough, but the new Sandisk players that both my wife and I have only function on tags, not on filenames. Last night I wrote this small ruby script to fix that:
#!/usr/bin/ruby require "date" require "rubygems" require "mp3info" ARGV.each do |file| title, datestr = file.scan(/(w+)_(d+_d+_d+).mp3/)[0] if title and datestr date = DateTime.parse(datestr.gsub!(/_/,"-")) title.gsub!(/_/, " ") title = title.split.map {|a| a.capitalize}.join(" ") puts date puts title Mp3Info.open(file) do |mp3| if not mp3.tag.album == "#{title} #{date.strftime("%Y")}" mp3.tag.album = "#{title} #{date.strftime("%Y")}" mp3.tag.artist = "WAMC Recordings" mp3.tag.title = date.strftime("%Y %m %d - #{title}") end end end end
What’s going on should be pretty clear, but I’ll highlight a few things. First we are iterating over ARGV, so this takes a list of files on the command line. DateTime has a parser, which is actually pretty good. Anything that looks like a standard date can be converted back to one.
Ruby methods always return object instances, which let you do things like
title.split.map {|a| a.capitalize}.join(" ")
where you split on white space, capitalize the components in the array, and join it back into a string.
And to wrap it all up, we’ve got a great Mp3Info library as a gem. Wondering where the save call is? Well that’s one of the wonderful things about ruby do blocks, the save is implicit when we end the block as mp3 goes out of scope. No need to make sure you clean up those resources or sync manually, because by doing the action in the do block all the setup / teardown is handled by the system. I used to be confused about do blocks, now I love them for this very reason.