【问题标题】:Using eyeD3 to embed album art from URL使用 eyeD3 从 URL 嵌入专辑封面
【发布时间】:2017-03-23 18:44:32
【问题描述】:

我正在编写一个 python 脚本,它从服务器下载音乐文件,然后从 URL 添加专辑图像,为此我使用 eyeD3 python 库,我在下面的原始代码。

import eyed3

mySong = eyed3.load('C:\Users\PC\Music\Test.mp3')
mySong.tag.album_artist = u'Artist-Name'
mySong.tag.images.remove(u'')
mySong.tag.images.set(3, 'None', 'https://upload.wikimedia.org/wikipedia/en/6/60/Recovery_Album_Cover.jpg')
mySong.tag.save()

我尝试了此命令的不同版本,它要么不返回错误,但不像上面的代码那样嵌入图像,要么返回一条错误消息,指出“ValueError: img_url MUST not be none when no image data”。

在我的另一种选择是直接从文件夹中的 URL 存储下载图像并从那里嵌入然后删除之前,任何人都在 eyeD3 的这一部分取得了任何成功。显然我的其他解决方案会更好。

【问题讨论】:

    标签: python metadata id3 eyed3


    【解决方案1】:

    您可以使用urllib2从URL获取图片的图片数据,然后在eyed3中使用。

    import urllib2
    response = urllib2.urlopen(your image url)  
    imagedata = response.read()
    
    import eyed3
    file = eyed3.load("song.mp3")
    file.tag.images.set(3, imagedata , "image/jpeg" ,u"Description")
    file.tag.save()
    

    【讨论】:

      【解决方案2】:

      使用

      mySong.tag.images.set(type_=3, img_data=None, mime_type=None, description=u"you can put a description here", img_url=u"https://upload.wikimedia.org/wikipedia/en/6/60/Recovery_Album_Cover.jpg")
      

      解释:

      mySong.tag.images.set 正在调用src/eyed3/id3/tag.py 中定义的类ImagesAccessorset 函数。

      签名如下:

      def set(self, type_, img_data, mime_type, description=u"", img_url=None):
          """Add an image of ``type_`` (a type constant from ImageFrame).
          The ``img_data`` is either bytes or ``None``. In the latter case
          ``img_url`` MUST be the URL to the image. In this case ``mime_type``
          is ignored and "-->" is used to signal this as a link and not data
          (per the ID3 spec)."""
      

      【讨论】:

      • 这行得通,如果你不想对检索到的图像做任何事情,我个人更喜欢这个解决方案。通过跳过 urllib 节省一步。
      • 我很好奇为什么编码器没有把这个功能放在他们的文档中。而且我仍然无法将专辑图片添加到歌曲中,我不知道为什么。没有弹出错误。
      猜你喜欢
      • 1970-01-01
      • 2012-11-19
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多