|Ned Batchelder : Blog | Code | Text | Site|
M3u files in a tree, take 2
» Home : Blog : January 2004
In November, I wrote about creating m3u files in a tree, and posted a simple solution. It turns out there's a serious drawback to creating m3u files with just the file name: when Winamp loads the playlist, it reads all the files to find out how long they are, which can take a long time (minutes) with a very large playlist. This is annoying. I've put some more effort into it, and now I have a better solution.
The whole thing started because I didn't know how to find the duration of an MP3 file, and the November solution was a neat sidestep around the whole issue, but I guess not as neat as I would have liked.
Well, in December, Simon posted about extracting the length from MP3 files, mentioning a spotty solution. In one of the comments to that post, Sune Kirkeby posted a link to his mp3.py, which does an admirable job of divining the length of the song. It turns out not to be too hard, but involves a lot of fiddly interpretation of bitfields, and close reading of specs, so I'm glad someone else has already done the hard work.
Armed with that piece of the puzzle, I attacked the problem of reading the ID3 metadata with renewed vigor (see the id3reader post earlier today), and now I can create full-featured m3u files.
The complete utility is m3utree.py. It works great for my files.
Back in November, several readers complained about non-ascii characters gumming up the works. I've tried to take that into account, but if you have problems, send me an email with details (the name of the file, the version of Python, and whatever else strikes your fancy).