我不认为可以使用嵌入专辑封面,EasyID3
但可以使用ID3
.
ID3
您可以使用如下方式嵌入专辑封面:
from mutagen.id3 import ID3, APIC
audio = ID3('music_file.mp3')
with open('img.jpg', 'rb') as albumart:
audio['APIC'] = APIC(
encoding=3,
mime='image/jpeg',
type=3, desc=u'Cover',
data=albumart.read()
)
audio.save()
如果性能不是您主要关心的问题,您可以保存文本元数据,EasyID3
然后再次加载音乐文件ID3
以嵌入专辑。否则,您可以完全使用ID3
.
因此,您在第一种情况下的代码将是:
import requests
import shutil
# you can directly import EasyID3 and ID3
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, APIC
def addMetaData(url, title, artist, album, track):
response = requests.get(url, stream=True)
with open('img.jpg', 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
audio = EasyID3('music_file.mp3')
audio['artist'] = artist
audio['title'] = title
audio['tracknumber'] = track
audio['album'] = album
audio.save()
audio = ID3('music_file.mp3')
with open('img.jpg', 'rb') as albumart:
audio['APIC'] = APIC(
encoding=3,
mime='image/jpeg',
type=3, desc=u'Cover',
data=albumart.read()
)
audio.save()
在第二种情况下:
import requests
import shutil
from mutagen.id3 import ID3, TPE1, TIT2, TRCK, TALB, APIC
def addMetaData(url, title, artist, album, track):
response = requests.get(url, stream=True)
with open('img.jpg', 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
audio = ID3('music_file.mp3')
audio['TPE1'] = TPE1(encoding=3, text=artist)
audio['TIT2'] = TALB(encoding=3, text=title)
audio['TRCK'] = TRCK(encoding=3, text=track)
audio['TALB'] = TALB(encoding=3, text=album)
with open('img.jpg', 'rb') as albumart:
audio['APIC'] = APIC(
encoding=3,
mime='image/jpeg',
type=3, desc=u'Cover',
data=albumart.read()
)
audio.save()
注意:您也可以直接嵌入相册使用urllib2
,而无需先将其保存在磁盘上。例子:
import urllib2
from mutagen.id3 import ID3, APIC
audio = ID3('music_file.mp3')
albumart = urllib2.urlopen(url)
audio['APIC'] = APIC(
encoding=3,
mime='image/jpeg',
type=3,
desc=u'Cover',
data=albumart.read()
)
albumart.close()
audio.save()