python - 如何使用 eyed3 python 模块为 mp3 设置缩略图?
问题描述
我无法在 Python 中使用 eyed3 模块为 mp3 文件设置图像缩略图。我尝试下一个脚本:
import eyed3
from eyed3.id3.frames import ImageFrame
th = 'url_to_my_pic'
file = 'to_mp3_pleer/file.mp3'
audiofile = eyed3.load(file)
audiofile.initTag()
audiofile.tag.frames = ImageFrame(image_url=th)
audiofile.tag.save()
但这对我文件中的缩略图没有任何作用。在谷歌没有关于使用 eyed3 设置缩略图的信息。我该如何设置?
解决方案
经过几个小时的 eyeD3 学习、谷歌搜索和文件封面实验,我想,我有一个解决方案给你。
您需要遵守以下规则:
- 使用 ID3v2.3 (不是 eyeD3 中默认的 v2.4);
- 为封面图片(word
cover
)添加正确的描述; - 将图像作为二进制传递;
我会给你一个代码示例,它在我的 Windows 10 上运行良好(也应该在其他平台上运行):
import eyed3
import urllib.request
audiofile = eyed3.load("D:\\tmp\\tmp_mp3\\track_example.mp3")
audiofile.initTag(version=(2, 3, 0)) # version is important
# Other data for demonstration purpose only (from docs)
audiofile.tag.artist = "Token Entry"
audiofile.tag.album = "Free For All Comp LP"
audiofile.tag.album_artist = "Various Artists"
audiofile.tag.title = "The Edge"
# Read image from local file (for demonstration and future readers)
with open("D:\\tmp\\tmp_covers\\cover_2021-03-13.jpg", "rb") as image_file:
imagedata = image_file.read()
audiofile.tag.images.set(3, imagedata, "image/jpeg", u"cover")
audiofile.tag.save()
# Get image from the Internet
response = urllib.request.urlopen("https://example.com/your-picture-here.jpg")
imagedata = response.read()
audiofile.tag.images.set(3, imagedata, "image/jpeg", u"cover")
audiofile.tag.save()
推荐阅读
- c++ - 如何找出在 C++/WinRT 中连接断开的 midiDevice 失败
- ios - Apple iOS Swift5 BLE - 如何向特征发送 1 个字节(写入函数需要数据类型,但一个字节是“UInt8”类型)
- c - 如何在 ESP32 上的单个程序中结合客户端和服务器 BLE 功能?
- tableau-api - 如何从画面中显示最大值、最小值、中值和平均值
- python-3.x - Azure 函数队列为空
- android - 我可以在 Android 中以“20 年 11 月 9 日”这种格式转换日期吗?
- sql - 最小化查询获取时间
- javascript - Javascript中两个数组的点积
- node.js - 什么是 ngrok-stable-windows-amd64
- windows - 虚拟机下如何连接模拟器