pythoneyed3bytesio

eyed3 file from bytes


Is it possible to modify the tags of a downloaded MP3, without writing it to the disk?

I am using

def downloadTrack(url)
    track_data = requests.get(url)

    audiofile = Mp3AudioInherited(track_data.content)
    audiofile.initTag()

with the class Mp3AudioInherited inherited from core.AudioFile much like mp3.Mp3AudioFile. The only signficant difference:

class Mp3AudioInherited(core.AudioFile):
    ...
    def _read(self):        
        with io.BytesIO(self.data) as file_obj:
            self._tag = id3.Tag()
            tag_found = self._tag.parse(file_obj, self._tag_version)
            ...

Unfortunately _tag.parse() throws a ValueError: Invalid type: <type '_io.BytesIO'>. Isn't BytesIO a file-like object?

Thanks and regards!


Solution

  • No, io.BytesIO objects are not file-like (i.e., they are not interchangeable with file objects) in Python 2. Try using StringIO.StringIO to get a memory-backed file-like object in Python 2.