使用 python 3 诱变剂写入 id3 标签的函数
Function to write id3 tag with python 3 mutagen
为了用 mutagen 修改 id3 标签值 - 让我们说曲目号 (TRCK) - 我发现了这个:
filename = '/myDir/myFile.mp3'
from mutagen.mp3 import MP3
audio = MP3(fileName)
from mutagen.id3 import ID3NoHeaderError, ID3, TRCK
try:
audio = ID3(fileName)
except ID3NoHeaderError:
print("Adding ID3 header")
audio = ID3()
audio['TRCK'] = TRCK(encoding=3, text=5)
但我不明白我怎么能创建一个函数来修改传递的标签,比如:
def writeTag(filename, tagName, newValue):
from mutagen.mp3 import MP3
audio = MP3(fileName)
... ???
writeTag('/myDir/myFile.mp3', 'TRCK', 5)
如果您想直接编辑 ID3 标签,请使用 ID3 模块。
from mutagen.id3 import ID3, TIT2
path = 'example.mp3'
tags = ID3(path)
print(tags.pprint())
tags.add(TIT2(encoding=3, text="new_title"))
tags.save()
**供参考:标签 ID 总结在官方文档中 link.
使用 pprint() 方法显示 ID3 标签可能更容易:
song titles (TIT2)
Album name (TALB)
[编辑] 如下所示,所有具有特定功能的标签 ID 的示例:
from mutagen.id3 import ID3NoHeaderError
from mutagen.id3 import ID3, TIT2, TALB, TPE1, TPE2, COMM, TCOM, TCON, TDRC, TRCK
def writeTag(filename, tagName, newValue):
#title
if (tagName == 'TIT2'):
tags["TIT2"] = TIT2(encoding=3, text=u''+newValue+'')
#mutagen Album Name
elif (tagName == 'TALB'):
tags["TALB"] = TALB(encoding=3, text= u''+newValue+'')
#mutagen Band
elif (tagName == 'TPE2'):
tags["TPE2"] = TPE2(encoding=3, text= u''+newValue+'')
#mutagen comment
elif (tagName == 'COMM'):
tags["COMM"] = COMM(encoding=3, lang=u'eng', desc='desc', text=u''+newValue+'')
#mutagen Artist
elif (tagName == 'TPE1'):
tags["TPE1"] = TPE1(encoding=3, text=u''+newValue+'')
#mutagen Compose
elif (tagName == 'TCOM'):
tags["TCOM"] = TCOM(encoding=3, text=u''+newValue+'')
#mutagen Genre
elif (tagName == 'TCON'):
tags["TCON"] = TCON(encoding=3, text=u''+newValue+'')
#mutagen Genre date
elif (tagName == 'TDRC'):
tags["TDRC"] = TDRC(encoding=3, text=u''+newValue+'')
#track_number
elif (tagName == 'TRCK'):
tags["TRCK"] = TRCK(encoding=3, text=u''+newValue+'')
path = 'example.mp3'
tags = ID3(path)
writeTag(path,"TIT2","NewValue")
tags.save(path)
为了用 mutagen 修改 id3 标签值 - 让我们说曲目号 (TRCK) - 我发现了这个:
filename = '/myDir/myFile.mp3'
from mutagen.mp3 import MP3
audio = MP3(fileName)
from mutagen.id3 import ID3NoHeaderError, ID3, TRCK
try:
audio = ID3(fileName)
except ID3NoHeaderError:
print("Adding ID3 header")
audio = ID3()
audio['TRCK'] = TRCK(encoding=3, text=5)
但我不明白我怎么能创建一个函数来修改传递的标签,比如:
def writeTag(filename, tagName, newValue):
from mutagen.mp3 import MP3
audio = MP3(fileName)
... ???
writeTag('/myDir/myFile.mp3', 'TRCK', 5)
如果您想直接编辑 ID3 标签,请使用 ID3 模块。
from mutagen.id3 import ID3, TIT2
path = 'example.mp3'
tags = ID3(path)
print(tags.pprint())
tags.add(TIT2(encoding=3, text="new_title"))
tags.save()
**供参考:标签 ID 总结在官方文档中 link.
使用 pprint() 方法显示 ID3 标签可能更容易:
song titles (TIT2)
Album name (TALB)
[编辑] 如下所示,所有具有特定功能的标签 ID 的示例:
from mutagen.id3 import ID3NoHeaderError
from mutagen.id3 import ID3, TIT2, TALB, TPE1, TPE2, COMM, TCOM, TCON, TDRC, TRCK
def writeTag(filename, tagName, newValue):
#title
if (tagName == 'TIT2'):
tags["TIT2"] = TIT2(encoding=3, text=u''+newValue+'')
#mutagen Album Name
elif (tagName == 'TALB'):
tags["TALB"] = TALB(encoding=3, text= u''+newValue+'')
#mutagen Band
elif (tagName == 'TPE2'):
tags["TPE2"] = TPE2(encoding=3, text= u''+newValue+'')
#mutagen comment
elif (tagName == 'COMM'):
tags["COMM"] = COMM(encoding=3, lang=u'eng', desc='desc', text=u''+newValue+'')
#mutagen Artist
elif (tagName == 'TPE1'):
tags["TPE1"] = TPE1(encoding=3, text=u''+newValue+'')
#mutagen Compose
elif (tagName == 'TCOM'):
tags["TCOM"] = TCOM(encoding=3, text=u''+newValue+'')
#mutagen Genre
elif (tagName == 'TCON'):
tags["TCON"] = TCON(encoding=3, text=u''+newValue+'')
#mutagen Genre date
elif (tagName == 'TDRC'):
tags["TDRC"] = TDRC(encoding=3, text=u''+newValue+'')
#track_number
elif (tagName == 'TRCK'):
tags["TRCK"] = TRCK(encoding=3, text=u''+newValue+'')
path = 'example.mp3'
tags = ID3(path)
writeTag(path,"TIT2","NewValue")
tags.save(path)