Python Mutagen:通过 url 添加封面照片/专辑艺术? [英] Python Mutagen: add cover photo/album art by url?
本文介绍了Python Mutagen:通过 url 添加封面照片/专辑艺术?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用mutagen,我可以添加普通的元标签,例如title
、artist
和genre
但是当我尝试添加图像时通过网址,它不起作用.
Using mutagen, I am able to add normal metatags such as title
, artist
, and genre
however when I try to add an image via a url, it doesn't work.
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()
时,它给了我图像 - I know it adds all tags except the image because I can open it with itunes correctly, all except the album art is blank
- when I do
ima.show()
it gives me the image
因此,我相信它可能会打破这条线:covr.append(MP4Cover(im,MP4Cover.FORMAT_JPEG))
because of this I believe that it probably breaks around this line:
covr.append(MP4Cover(im,MP4Cover.FORMAT_JPEG))
有什么想法吗?还有其他方法可以从 url 获取图像吗?
any ideas? Is there another way to get the image from a url?
推荐答案
这对我有用:
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()
这篇关于Python Mutagen:通过 url 添加封面照片/专辑艺术?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文