Use Sed to Edit iTunes Library
by walter on Aug.03, 2009, under Firefly, Linux, Media Streamers, Soundbridge
Visited 1659 times, 1 so far today
When I rip music, I do it twice. Once in mp3 format for playback on an iPod and once in flac for playback on my local network via Roku Soundbridge players and the Firefly media server. iTunes doesn’t support flac, so I can’t make iTunes playlists for those files. I can, however, make them for the mp3 files which are named identically. And with a little Linux program called “sed”, I can edit the iTunes library to make Firefly read it to work with the flac files.
Firefly will read the “iTunes Library.xml” file when it scans for new music and use any playlists found in it. Though in this case, the playlists reference mp3 files rather than flac. As a result the Soundbridge will display the names of all the playlists, but they’re all empty because those files don’t exist within my flac music folders.
Sed is a linux program that among other things does search and replace within a file, so my strategy was to use it on the iTunes Library.xml file to search for “.mp3″ and change any occurrence to “.flac”. My library file is generated on a Windows computer, so songs within a playlist are identified with a Windows folder structure like “D:/My Music/Artist/Album/song.mp3.” Firefly is smart enough to ignore parts of the path that point to a Windows path and just make use of the filename. So on my Linux file server, Firefly ignores the “D:/My Music” part and just looks for the rest in the music folder that it regularly scans.
As a test, I copied “iTunes Library.xml” into the flac music folder that Firefly scans and because uppercase and spaces can sometimes cause heartburn, for simplicity renamed it itunes_library.xml. That turns out to have been a mistake, but more on that later. I then ran the following sed command.
sed -i 's/.mp3/.flac/g' itunes_library.xml
The -i switch tells it to do the substitution in place, ie not create a new file. The s tells it to do a substitution using .mp3 as the old text and .flac as the new text. The g tells it to keep going past the first occurrence. And of course the last part is the name of the file to do the operation on. When that finished I loaded it into a text editor and searched for .mp3 to verify that there were none left. Unfortunately, it found some. It found “.Mp3″ and “.MP3″. The case of the letters had thrown it off. So back to the sed command with a slight change.
sed -i 's/.mp3/.flac/gi' itunes_library.xml
The i option at the end makes the search case insensitive. Another run through in the text editor showed that it had worked.
When I then forced Firefly to rescan the library, it didn’t find the playlists at all. Grasping at a straw, I changed the filename back to “itunes library.xml”, putting the space back in but still leaving it lower case. After another rescan, the playlists were there and full of flac formatted files.
So the proper procedure for this was:
sed -i 's/.mp3/.flac/gi' itunes\ library.xml
The backslash in the filename is required because of the space character. My next step is to make a script to copy the mp3 itunes library into the flac folder, run sed on it, and then force a rescan of the library each day.