通过Python修改或添加MP3格式音乐的封面
向MP3文件添加封面的Python脚本
支持格式jpg
与png
依赖
mutagen
或直接安装.wheel
文件 下载链接
1
| pip install mutagen-1.47.0-py3-none-any.whl
|
脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import os import sys from mutagen.mp4 import MP4, MP4Cover from mutagen.id3 import APIC, ID3
def change_mp3_cover(file_path:str, cover_path:str): music = ID3(file_path) music.delall('APIC') print("delete origin cover") music.save() if cover_path.endswith('png'): print('add new png cover') with open(cover_path, 'rb') as albumart: music.add( APIC( encoding=3, mime='image/png', type=3, desc=u'Cover', data=albumart.read() ) ) else: print('add new jpeg cover') with open(cover_path, 'rb') as albumart: music.add(APIC( encoding=3, mime='image/jpeg', type=3, desc=u'Cover', data=albumart.read() )) print('done') music.save(v2_version=3)
if __name__ == '__main__': file_path = './your_music.mp3' cover_path = './your_cover.png' change_mp3_cover(file_path, cover_path)
|
效果
添加前:
结果:
Error: Doesn’t start with an ID3 tag
当出现如下报错时,先向MP3文件中添加一些属性
1
| mutagen.id3._util.ID3NoHeaderError: './547085557.mp3' doesn't start with an ID3 tag
|
之后正常运行程序即可
1 2 3 4
| $ python add_cover.py delete origin cover add new png cover done
|