0

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!

kf198
  • 35
  • 1
  • 5

1 Answers1

0

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.

Will Angley
  • 1,392
  • 7
  • 11
  • Unfortunately `_tag.parse()` doesn't take something of `` either. Why is `StringIO.StringIO()` of type _'instance'_ and `io.BytesIO` _'_io.BytesIO'_ anyway? – kf198 Oct 08 '15 at 20:42
  • It sounds like `_tag.parse()` is calling [type()](https://docs.python.org/2/library/functions.html#type) on an object passed in to it and then raising a `ValueError` if it doesn't like the result. What you're seeing is the `repr()` of the Python type object that comes back from that call; but it's a bit tough to explain _why_ those type objects have the representations they do in the 500 characters of a StackOverflow comment. – Will Angley Oct 08 '15 at 20:52
  • Indeed. I probably wouldn't comprehend anyway. Do have an idea for a workaround nevertheless? – kf198 Oct 08 '15 at 21:26
  • Not really, I haven't played with eyed3 before. Now might be a good time to consider how strong a reason you have to avoid hitting the filesystem, and whether you can use an ordinary file to achieve your goal. – Will Angley Oct 08 '15 at 22:00
  • Very true! I just thought that, perhaps, I am one line of code away from a solution. Given the size of this application, keeping it in memory would be more of a psychological thing anyway. Thank you for your quick guidance! – kf198 Oct 09 '15 at 15:04