【问题标题】:Python Mutagen: add cover photo/album art by url?Python Mutagen:通过 url 添加封面照片/专辑封面?
【发布时间】:2014-04-25 19:18:00
【问题描述】:

使用诱变剂,我可以添加普通的元标记,例如 titleartistgenre,但是当我尝试通过 url 添加图像时,它不起作用。

from mutagen.mp4 import MP4
from mutagen.mp4 import MP4Cover
from PIL import Image
import urllib2 as urllib
import io, sys, getopt

#url is defined elsewhere
audio = MP4(url)
#clear previous meta tags
audio.delete()

#get album picture data
cover ="http://cont-sv5-2.pandora.com/images/public/amz/5/2/9/7/095115137925_500W_488H.jpg"
fd = urllib.urlopen(cover)
image_file = io.BytesIO(fd.read())
ima = Image.open(image_file)
im = ima.tostring()

#processing
#I think it is here where it breaks
covr = []
if cover.endswith('png'):
    covr.append(MP4Cover(im,MP4Cover.FORMAT_PNG))
else:
    covr.append(MP4Cover(im,MP4Cover.FORMAT_JPEG))

#add cover
audio['covr'] = covr
#save everything
audio.save()
  • 我知道它会添加除图像之外的所有标签,因为我可以使用 itunes 正确,除了专辑封面以外都是空白的
  • 当我执行ima.show() 时,它会给我图像

因此,我相信它可能会绕过这条线: covr.append(MP4Cover(im,MP4Cover.FORMAT_JPEG))

有什么想法吗?还有其他方法可以从 url 获取图像吗?

【问题讨论】:

  • 尝试跳过ioPIL 部分:audio['covr'] = MP4Cover(fd.read(), MP4Cover.FORMAT_JPEG),将字节转换为字符串可能是这里的问题。

标签: python python-imaging-library urllib pillow mutagen


【解决方案1】:

这对我有用:

fd = urllib.urlopen(cover)
# Drop the entire PIL part
covr = MP4Cover(fd.read(), getattr(
            MP4Cover,
            'FORMAT_PNG' if cover.endswith('png') else 'FORMAT_JPEG'
        ))
fd.close() # always a good thing to do

audio['covr'] = [covr] # make sure it's a list
audio.save()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-22
    • 2012-02-28
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多