Sunday, September 2, 2012

Rolled my own Playlist exporter

The stereo in both my Truck and the Sportwagen support SD Cards.  They don't support playlist files, so the only way to have a playlist that actually plays in order is to have all of the songs from that playlist in a single folder with a prefix of the # in the track list that is zero padded since it is using an ASCII sort.  That's a lot of words, but what does it all mean?  That means I need my songs to be named 0001_Song.mp3, 0002_Song2.mp3, etc... all in the same folder.  Since the SD card has to be formatted in FAT32, this can't be accomplished with links, so you need a copy of the file for each instance you want to use it (either in a playlist or in folder for the album).

I got tired of having my playlists be a complete jumble, so I rolled my own Perl script to read in M3U files and copy the appropriate files over.  It still needs lots of work (such as updating an existing playlist, etc...) but it got the job done for the initial copy.  I had one VERY ANNOYING BUG that was confusing the fuck out of me for a while.  I would set a variable, and unless I added a \n to the end of it, it would disappear when I tried to use it and I couldn't figure out why.  I finally realized what the hell was going on.  The playlist was generated using iTunes on a Windows machine.  The playlist was then parsed in Perl running on Cygwin (a linux environment that runs on Windows).  Either Perl or Cygwin decided the \r wasn't part of the character set to move to the next line and left it there.  That character by itself means go to the beginning of the current line, so the file name was there, but was then getting overwritten by the content after it.

Once my head finished exploding, I made a change to the script and got it done, and loaded my first playlist with the script on my SD card.  So, I will paste my crappy code here for everyone to ridicule, or possibly use.  I plan on updating this later to have better functionality, but it worked for my current need.

No comments: