views:

273

answers:

3

We're generating MP3 files on the fly in Python, and need to edit the ID3 headers in-memory using a file-like object.

All the ID3 libraries on PyPI appear to require you to pass a filesystem path as a string. I find this rather frustrating!

Writing our generated MP3 out to disk (or ramdisk) just to add ID3 tags is unacceptable for a number of reasons, especially performance.

Given the plentitude of ID3 libraries, is there an ID3 library that simply works with file-like objects?

A: 

AFAIR tags are appended to the end of file. You might want to study the format and make a simple library yourself, that should not be very difficult.

Also, you could consider storing them temporary on a filesystem like tmpfs (ramdisk).

liori
Actually, ID3 tags are best prepended. http://en.wikipedia.org/wiki/ID3#ID3v2
David Eyk
Ha, I didn't know they moved the tags to the beginning of file in ID3v2...
liori
Yeah. As for tmpfs, it's too hackish. I already have the object in memory--why do I need to copy it to work with it?
David Eyk
Because you'll spare your programming time. Copying in RAM is relatively cheap (5MB file takes 0.05s on my 3-year-old laptop). So the deciding factor is how you value your effort. The only thing that could be more important is the server load introduced by that copying, compared to other tasks your server needs to do... and I know nothing about your code, so decide yourself.
liori
You're totally right, and we may end up doing that or roll our own frame-hacking strategy. The point of the question, however, was to avoid all that by discovering an out-of-the-box solution that would spare us programming time. Hacking frames or hacking servers are more work, not less, and outside the scope of this question.
David Eyk
A: 

Does StringIO help? http://docs.python.org/library/stringio.html

foosion
The problem is, all the libraries I've seen won't work w/ StringIO, or anything like it, because they require a filesystem path.
David Eyk
and you don't want to modify one of the simpler libs?
foosion
Would you recommend a simple one to modify?
David Eyk
I'd just browse the libs on PyPI to find one that does what you want and is small. You may be able to just redefine open to take a string (rather than a filename) and return fh, where fh = StringIO.StringIO(string_variable).
foosion
A: 

Well, the answer seems to be that no such animal exists. The advantages of programming to an interface are apparently lost on the python MP3 frame hackers. We solved the problem by modifying an existing library.

David Eyk