pythonpython-3.xid3mutagen

Mutagen: TypeError: a bytes-like object is required, not 'str'


What should I do? I'm getting this error. I want add some tags for FLAC. I searched but i didnt find anythings. Please help me.

    Traceback (most recent call last):
  File "indir.py", line 50, in <module>
    audio.save()
  File "/usr/local/lib/python3.6/dist-packages/mutagen/_util.py", line 169, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/mutagen/_util.py", line 140, in wrapper
    return func(self, h, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/mutagen/flac.py", line 847, in save
    self._save(filething, self.metadata_blocks, deleteid3, padding)
  File "/usr/local/lib/python3.6/dist-packages/mutagen/flac.py", line 864, in _save
    metadata_blocks, available, content_size, padding)
  File "/usr/local/lib/python3.6/dist-packages/mutagen/flac.py", line 154, in _writeblocks
    data += cls._writeblock(block)
  File "/usr/local/lib/python3.6/dist-packages/mutagen/flac.py", line 126, in _writeblock
    datum = block.write()
  File "/usr/local/lib/python3.6/dist-packages/mutagen/flac.py", line 620, in write
    f.write(self.data)
TypeError: a bytes-like object is required, not 'str'

My Code:

audio = FLAC("music.flac")
audio['artist'] = sarki.artist.name
audio['title'] = sarki.name
pic = Picture()
pic.type = id3.PictureType.COVER_FRONT
pic.width = 640
pic.height = 640
pic.mime = 'image/jpeg'
pic.data = "music.jpg"

audio.add_picture(pic)
audio.save()

Solution

  • I believe the error is here:

    pic.data = "music.jpg"
    

    You are attempting to set the image data of the picture to be a string. I'm guessing you wanted to set the image data to be the contents of the file music.jpg instead. If so, try replacing this line with the following two:

    with open("music.jpg", "rb") as f:
        pic.data = f.read()
    

    This follows an example in the Mutagen API reference.