This is a little thing that’s been bugging me since we got our TiVo a while back, but I never got around to solving it – exporting my music playlists from iTunes to TiVo. TiVo’s jukebox interface is simply horrendous, so playlists are an absolute must, unless you like scrolling through pages of your music collection. iTunes makes matters worse in that it doesn’t allow you to export M3U-formatted playlists, the format TiVo requires.
Sigh. A little shell scripting and awk is all that’s required to resolve this problem in short order. Here’s an awk script
called m3u-ify.awk to convert an iTunes playlist (exported as plaintext) to an M3U playlist:
FS = "\t"
if (match($27, /\.mp3/))
time = $8
name = $27
location = $27
# Figure out the location from the absolute location
# that iTunes exports. Note that we remove the '- ' from
# the location, which iTunes seems to add erroneously.
# Using 'sub' is not ideal, but gensub seems flakey in
# my version of gawk, otherwise I'd use the following:
# location = gensub(/D:\\Music\\\(.*\\.*\\[0-9]*\) -\(.*\)\.mp3/, "\\1\\2", "g", location)
sub(/D:\\Music\\/, "", location)
sub(/ - /, " ", location)
# Find the name of the song - this regex is giving
# me problems in Cygwin with gawk, so I coded an
# alternate way to get to the normalized song name
# name = gensub(/D:\\Music\\.*\\.*\\[0-9]* - \(.*\)\.mp3/, "\\1", "g", name)
sub(/D:\\Music\\.*\\.*\\/, "", name)
sub(/\.mp3/, "", name)
sub(/[0-9]* - /, "", name)
Note that my music collection is located in D:\Music (which needs to be stripped out of the file location to create the M3U file) – you will have to alter this script if to the location of your iTunes music folder.
To use the script, you’ll need awk installed on your machine (for those of you using Windows, you might try grabbing a copy of Cygwin) . To convert a playlist to M3U:
- Select a playlist in iTunes, export it as a text-formatted playlist (input.txt) to a folder containing the above script
- Open a shell prompt, go to the folder where you saved the text-formatted playlist
awk -f m3u-ify.awk input.txt > output.m3u
You’ll now have an M3U-formatted version of your playlist. If you really want to be fancy, export all your playlists at once, and then generate a batch shell script to process them all at once by running
ls -1 *.txt |sed -e 's/\(.*\)\.txt/awk -f m3u-ify.awk \"\1.txt\" > \"\1.m3u\" /g'>batch.sh. This will generate a shell script called
batch.sh that will process all the text playlists in once go.
Note: I point TiVo Desktop to my iTunes folder directly – I’m not sure if this affects file location specified in the M3U or not. Your mileage may vary.