开发者

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 fil开发者_JS百科esystem 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?


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)


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜