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

发布于 2024-09-29 06:45:11 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

め七分饶幸 2024-10-06 06:45:11

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

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

相关部分是:

from pathlib import Path

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

# Read the ID3 tag or create one if not present
audio_path = Path('/path/to/Music/audio.mp3')

with audio_path.open(mode='rb') as fib:
    try:
        tags = ID3(fib)
        print(f'BEFORE:\n\n{tags.pprint()}')

    except (ID3NoHeaderError, HeaderNotFoundError):
        print("Adding ID3 header..")
        tags = ID3(audio_path)

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

    tags.save(audio_path)
    print(f'\n\nAFTER:\n\n{tags.pprint()}')

另请参阅:

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

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

The relevant part is:

from pathlib import Path

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

# Read the ID3 tag or create one if not present
audio_path = Path('/path/to/Music/audio.mp3')

with audio_path.open(mode='rb') as fib:
    try:
        tags = ID3(fib)
        print(f'BEFORE:\n\n{tags.pprint()}')

    except (ID3NoHeaderError, HeaderNotFoundError):
        print("Adding ID3 header..")
        tags = ID3(audio_path)

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

    tags.save(audio_path)
    print(f'\n\nAFTER:\n\n{tags.pprint()}')

See also:

眼趣 2024-10-06 06:45:11

您检查过网络上的示例吗?其中一些应该对您有帮助。

[编辑:]

Mutagen教程非常好,因此没有添加更多信息。
dir() 提供了大部分详细信息。

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

使用 mutagen 嵌入歌词

例子

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())

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.

For setting album cover to mp3 using mutagen

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())
冷月断魂刀 2024-10-06 06:45:11

一个简单的方法是:

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

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

audio.save(v2_version=3)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文