Waiter, there's a Unicode in my MP3!
- 4 minutes read - 759 wordsSo, Pink Goddess loves music. And I like to try to make her happy, so I like to help her play her music in the car when we’re driving places together. However, she’s (rightly) not a fan of messing about. And so, I told her that if she simply dumped the music she wanted to play into a folder on my key drive, I’d sort the rest out.
Somehow, the 16 GB Ultra-speed SD card that I bought to go in a Raspberry Pi ended up being our in-car music receptacle. The way this works is quite simple. Pink Goddess decides what music she’d like to listen to — my collection of eclectic dirges is generally not included, of course. She then creates a playlist in iTunes of the music she wants, and then exports it to a folder. This results in around a hundred or so assorted music files, some MP3, some M4A, and the occasional M4P. Then she hands whatever storage the folder is on to me, and glares prettily at me. Due to my usual creative laziness, and knowing I’d have to do this more than once, I decided that this job was best left to the computer.
So I wrote a couple of scripts to sort this out. This isn’t any kind of finished product, by the way, as will become clear. The first thing I do is delete the M4P files. These are actually DRM’d files, and so are entirely useless. The next thing I do is to transcode any M4A files to MP3. I know transcoding is at best ill-advised, but the car only plays MP3s, so it’s basically the only option. Due to a problem in the script, I get files named “Song.m4a.mp3” at this point, so I use an Automator action to get rid of that. I then remove the M4A files, so I have just a bunch of MP3s left now.
The last step of the process is to move the MP3s, one by one, into folders named “Artist-Album”, so that they sort neatly in the car. Again, I have a script for this, written in Python. I boxed a bit clever here, as the script actually checks the ID3V2 tags in the file, and processes them based on that, rather than the file name. It doesn’t break the file names, though. This is handy because iTunes is actually quite strict on the ID3v2 tags, so given that the music is coming from iTunes, I can rely on the tags being sensible. I can also rely on the file names starting with the track number, so they play in album order as well.
This process has worked fine since we bought the car about three years ago. Except today, the final script failed horribly in a way I hadn’t seen before. I was rather baffled, but eventually tracked down the problem. The problem was, for the first time, I was trying to process music from Beyoncé. Note the last letter in that name — it’s not an ASCII character, and that was the source of the problem. Amusingly, the code to create the folder worked fine, but the string logic to create the operating system “mv” command to move the file into the folder didn’t work. I ended up having to add some Unicode string handling in the code where I plug together the string that I then execute. But now that it’s done, the script works fine again, and Beyoncé’s works end up where they should.
Just goes to show that Unicode can bite you when you least expect it. And that three-year old scripts can still have bugs. And that I probably should just take a day out and rewrite the whole lot so that it does everything in one hit.
Technical sidebar: After realising what the problem was, I basically went from this:
os.system('mv "%s" "%s"' % (fname,newer)) to a block of code where I build up the string chunk by chunk. The crucial piece looks like this:
> os_str += newdir.encode("utf-8", "ignore") where I explicitly encode the strings as UTF-8, meaning that the Unicode character gets pushed into the string properly. Looking back at the code now, I notice that I must have stumbled across this before, as I explicitly state that we’re using Unicode where I read the string out of the ID3v2 tag:
artist = unicode(id3r.getValue('performer')) That puts this officially into “How the heck did this ever work?” territory. I wish I could remember if it was Beyoncé last time too…