使用python 3诱变剂写入id3标记的函数

42fyovps  于 2023-01-16  发布在  Python
关注(0)|答案(1)|浏览(103)

为了修改带有诱变剂的id 3标签值-假设为轨道编号(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)
ecr0jaav

ecr0jaav1#

如果要直接编辑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)

**[EDIT]**以下是所有具有特定功能的标签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)

相关问题