David Eyk
David Eyk

Reputation: 12531

Editing MP3 metadata on a file-like object in Python?

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?

Upvotes: 3

Views: 2051

Answers (4)

Blimmo
Blimmo

Reputation: 363

For those finding this answer in the distant future, mutagen, has worked perfectly for me and the constructor for one of its MP3 objects takes either a file path or a file-like object (the docs call it a 'filething').

From https://mutagen.readthedocs.io/en/latest/user/filelike.html:

The first argument passed to a FileType or Metadata can either be a file name or a file-like object, such as StringIO (BytesIO in Python 3) and mutagen will figure out what to do.

MP3("myfile.mp3")
MP3(myfileobj)

Upvotes: 3

David Eyk
David Eyk

Reputation: 12531

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.

Upvotes: 0

foosion
foosion

Reputation: 7898

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

Upvotes: 0

liori
liori

Reputation: 42277

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).

Upvotes: -1

Related Questions