5

I'd like to be able to add album cover into the file using mutagen, however when I add it as a file it returns with:

File "D:\Download\pandora\renamingMETAEFF.pyw", line 71, in <module>
    meta['covr'] = image
File "C:\Users\AMD\AppData\Local\Programs\Python\Python35\lib\site-packages\mutagen\_file.py", line 67, in __setitem__
    self.tags[key] = value
File "C:\Users\AMD\AppData\Local\Programs\Python\Python35\lib\site-packages\mutagen\mp4\__init__.py", line 357, in __setitem__
    self._render(key, value)
File "C:\Users\AMD\AppData\Local\Programs\Python\Python35\lib\site-packages\mutagen\mp4\__init__.py", line 371, in _render
    return render_func(self, key, value)
File "C:\Users\AMD\AppData\Local\Programs\Python\Python35\lib\site-packages\mutagen\mp4\__init__.py", line 732, in __render_cover 
    b"data", struct.pack(">2I", imageformat, 0) + cover))

TypeError: can't concat bytes to str

The relevant piece of code is this:

from mutagen.mp4 import MP4

image = jpgname + '.jpg'
meta['\xa9nam'] = song
meta['\xa9ART'] = artist
meta['\xa9alb'] = album
meta = MP4(songPath)
meta['covr'] = image
meta.save()

The rest of the metadata works perfectly fine, however the image part completely breaks the whole code.

Alex
  • 3,029
  • 3
  • 23
  • 46

1 Answers1

11

From the mutagen docs:

MP4 meta 'covr' – cover artwork, list of MP4Cover objects (which are tagged strs).

MP4Cover imageformat – format of the image (either FORMAT_JPEG or FORMAT_PNG)

from mutagen.mp4 import MP4, MP4Cover

video = MP4("test.mp4")

video["\xa9nam"] = "Test1"
video["\xa9ART"] = "Test2"
video["\xa9alb"] = "Test3"

with open("cover.jpg", "rb") as f:
    video["covr"] = [
        MP4Cover(f.read(), imageformat=MP4Cover.FORMAT_JPEG)
    ]

video.save()
Community
  • 1
  • 1
Simon Kirsten
  • 2,542
  • 18
  • 21
  • swapping out the file function for open function makes the whole script work thank you – Daniel Rachfał Jun 18 '16 at 15:11
  • @DanielRachfał Sorry, I meant to write `open`. Honestly, I have no idea why I wrote `file` wich only works in python 2. Anyways glad I could help. Please consider accepting the answer. – Simon Kirsten Jun 18 '16 at 15:16
  • Can you make this work with an .mp3 file instead? How? – imrek Mar 18 '17 at 15:42
  • 1
    @DrunkenMaster Sure, see here: http://stackoverflow.com/questions/409949/how-do-you-embed-album-art-into-an-mp3-using-python – Simon Kirsten Mar 19 '17 at 13:05
  • @SimonKirsten Thanks so much for the help. I'm wondering, why do you put the `MP4Cover` instance in a singleton list? Is that unnecessary/did you just mean to put the `MP4Cover` stuff on a single line and use brackets instead of parentheses? – Luke Davis Aug 25 '17 at 07:01
  • @LukeDavis I put it in a list because the documentation told me so "cover artwork, list of MP4Cover objects" and I've seen mp4's with multiple artworks so a list makes sense. With mp3's this might be different! – Simon Kirsten Aug 25 '17 at 11:48