需求是目前手里有几千个mp3文件,歌曲的名字、歌手、专辑名、专辑封面等数据也都有,但是这些数据都是分开的,没有写入mp3中,需要用代码批量修改mp3的元数据。
一、由于苹果没有mp3的编辑版权,用AVFoundation只能读取mp3的元数据,不能进行修改。其他像mp4等格式的元数据是可以编辑的,可参考AVFoundation之元数据操作进行编辑。
二、如果只需要编辑少量的mp3元数据,可以使用一些mac平台的工具,比如:Mp3tag、Meta等。
三、找来找去只找到通过Python用mutagen库修改mp3元数据这一种方式。步骤如下:
- 1.安装pip
curl https://bootstrap.pypa.io/pip/get-pip.py -o get-pip.py
sudo python3 get-pip.py
- 2.安装mutagen
pip install mutagen
- 3.编辑mp3元数据(以下四种方式亲测都可行)
import mutagen
from mutagen.id3 import ID3, APIC, TIT2, TPE1, TALB
audio = mutagen.File('/Users/mac/Desktop/6005661JACL2.mp3')
audio['TIT2'] = TIT2(encoding=3, text='歌名')
audio['TPE1'] = TPE1(encoding=3, text='歌手')
audio['TALB'] = TALB(encoding=3, text='专辑')
with open('/Users/mac/Desktop/6005661JACL2.jpeg', 'rb') as image:
audio['APIC'] = APIC(encoding=3, mime='image/jpeg', type=3, desc=u'Cover', data=image.read())
audio.save()
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, TIT2, TPE1, TALB
audio = MP3('/Users/mac/Desktop/6005661JACL2.mp3')
audio['TIT2'] = TIT2(encoding=3, text='歌名')
audio['TPE1'] = TPE1(encoding=3, text='歌手')
audio['TALB'] = TALB(encoding=3, text='专辑')
with open('/Users/mac/Desktop/6005661JACL2.jpeg', 'rb') as image:
audio['APIC'] = APIC(encoding=3, mime='image/jpeg', type=3, desc=u'Cover', data=image.read())
audio.save()
from mutagen.id3 import ID3, APIC, TIT2, TPE1, TALB
audio = ID3('/Users/mac/Desktop/6005661JACL2.mp3')
audio['TIT2'] = TIT2(encoding=3, text='歌名')
audio['TPE1'] = TPE1(encoding=3, text='歌手')
audio['TALB'] = TALB(encoding=3, text='专辑')
with open('/Users/mac/Desktop/6005661JACL2.jpeg', 'rb') as image:
audio['APIC'] = APIC(encoding=3, mime='image/jpeg', type=3, desc=u'Cover', data=image.read())
audio.save()
from mutagen.easyid3 import EasyID3
audio = EasyID3('/Users/mac/Desktop/6005661JACL2.mp3')
audio['title'] = '歌名'
audio['artist'] = '歌手'
audio['album'] = '专辑'
audio.save()