Python 有没有人有使用诱变剂写入文件的好例子?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4040605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:57:29  来源:igfitidea点击:

Does anyone have good examples of using mutagen to write to files?

pythonmutagen

提问by YGA

Just as the title asks — does anyone have a good example of using the Mutagen Python ID3 libraryto write to .mp3files?

正如标题所问的那样 - 有没有人有使用Mutagen Python ID3 库写入.mp3文件的好例子?

I'm looking, in particular, to add disc/track number information, but examples editing the title and artist would be helpful as well.

我特别想添加光盘/曲目编号信息,但编辑标题和艺术家的示例也会有所帮助。

Cheers,
/YGA

干杯,
/YGA

回答by pyfunc

Did you check out the examples on the web. Some of these should help you.

您是否查看了网络上的示例。其中一些应该对您有所帮助。

[Edit:]

[编辑:]

Mutagen tutorial is pretty good, hence did not add more information. dir() provides most of the details.

Mutagen 教程很不错,因此没有添加更多信息。dir() 提供了大部分细节。

For setting album cover to mp3 using mutagen

使用 mutagen 将专辑封面设置为 mp3

Embedding lyrics using mutagen

使用诱变剂嵌入歌词

An example

一个例子

from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
import mutagen.id3

filename = 'xxx.mp3'

# Example which shows how to automatically add tags to an MP3 using EasyID3

mp3file = MP3(filename, ID3=EasyID3)

try:
    mp3file.add_tags(ID3=EasyID3)
except mutagen.id3.error:
    print("has tags")

mp3file['title'] = 'Newly tagged'
mp3file.save()
print(mp3file.pprint())

回答by ccpizza

Taken from a script I made a while ago for embedding lyrics into MP3 files:

摘自我前段时间制作的用于将歌词嵌入 MP3 文件的脚本:

http://code.activestate.com/recipes/577138-embed-lyrics-into-mp3-files-using-mutagen-uslt-tag/

http://code.activestate.com/recipes/577138-embed-lyrics-into-mp3-files-using-mutagen-uslt-tag/

The relevant part is:

相关部分是:

from mutagen.id3 import ID3NoHeaderError
from mutagen.id3 import ID3, TIT2, TALB, TPE1, TPE2, COMM, TCOM, TCON, TDRC

# Read ID3 tag or create it if not present
try: 
    tags = ID3(fname)
except ID3NoHeaderError:
    print("Adding ID3 header")
    tags = ID3()

tags["TIT2"] = TIT2(encoding=3, text=title)
tags["TALB"] = TALB(encoding=3, text=u'mutagen Album Name')
tags["TPE2"] = TPE2(encoding=3, text=u'mutagen Band')
tags["COMM"] = COMM(encoding=3, lang=u'eng', desc='desc', text=u'mutagen comment')
tags["TPE1"] = TPE1(encoding=3, text=u'mutagen Artist')
tags["TCOM"] = TCOM(encoding=3, text=u'mutagen Composer')
tags["TCON"] = TCON(encoding=3, text=u'mutagen Genre')
tags["TDRC"] = TDRC(encoding=3, text=u'2010')
tags["TRCK"] = TRCK(encoding=3, text=u'track_number')

tags.save(fname)

回答by MagTun

An easy way to do it:

一个简单的方法:

from mutagen.easyid3 import EasyID3
audio = EasyID3(mp3_filename_import)
audio['title'] = "Title"
audio['artist'] = "Artist"
audio['album'] = "Album"
audio['composer'] = "" # empty
audio.save()

If the tags don't appear, then change the last line to:

如果标签未出现,则将最后一行更改为:

audio.save(v2_version=3)