Sunday, 25 August 2013

Mutagen : Replacement of cover in MP4 file

Mutagen : Replacement of cover in MP4 file

I'm trying to reduce the image (cover) of multiple audio files. I have
troubles to set the newer image in metadata.
from mutagen.mp4 import MP4
from mutagen.mp4 import MP4Cover
from PIL import Image
from StringIO import StringIO
def main():
# Open mp4 file for reading
my_mp4 = MP4("1.m4a")
# Retrieves all covers in list
covers = my_mp4.tags['covr']
if not covers:
exit()
tmp = StringIO(covers[0])
# Reduce the image
img = Image.open(tmp)
img.thumbnail((128, 128), Image.ANTIALIAS)
img.save(tmp, 'JPEG')
# Set the image in metadata
my_mp4.tags['covr'] = MP4Cover(tmp.getvalue())
tmp.close()
my_mp4.pprint()
# Save metadata
my_mp4.save()
The code crash when I'm calling the pprint function. Without it, the
result isn't as expected.
File "convert.py", line 41, in <module>
main()
File "convert.py", line 36, in main
my_mp4.pprint()
File "/[…]/python2.7/site-packages/mutagen/__init__.py", line 138, in
pprint
try: tags = self.tags.pprint()
File "/[…]/python2.7/site-packages/mutagen/mp4.py", line 611, in pprint
values.append("%s=%s" % (key, " / ".join(map(unicode, value))))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 22:
ordinal not in range(128)
I already tried to call some decode and encode functions. I think the
problem is because my jpeg image is of type str and metadata of type
unicode str.

No comments:

Post a Comment