8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

如何通过 mutagen(Python)为 mp3 文件中的情绪添加价值?

Asaduzzaman Sohel 2月前

28 0

我找不到通过 mutagen(Python 库)将心情写入 mp3 文件的方法初始化:从 mutagen.mp3 导入 MP3从 mutagen.id3 导入 ID3、TIT2、TALB、TPE1、TPE2、TCON、TPUB、TENC、TIT3...

我找不到通过 mutagen (Python 库) 将心情写入 mp3 文件的方法

初始化:

from mutagen.mp3 import MP3
from mutagen.id3 import ID3, TIT2, TALB, TPE1, TPE2, TCON, TPUB, TENC, TIT3, APIC, WOAR, PRIV
audio = MP3(mp3_file, ID3=ID3)

我可以使用 audio['TIT3'] = TIT3(encoding=3, text=subtitle)

另外,我可以使用以下方式编写作者 URL audio['WOAR'] = WOAR(encoding=3, url=author_url)

但我找不到办法来表达心情

我在文件属性中手动输入了心情(悲伤),然后运行 mutagen.File("file.MP3").keys()

我得到了结果 dict_keys(['TIT2', 'TALB', 'TCON', 'TPE2', 'TPUB', 'TSSE', 'TENC', 'TIT3', "APIC:'Album Cover'", 'APIC:', 'WOAR:https://youtube.com', 'PRIV:WM/Mood:S\x00a\x00d\x00\x00\x00', 'TPE1'])

我尝试了很多方法但无法让 Mood 发挥作用。

我甚至尝试使用 TMOO,就像第一条评论中说的那样。但还是没用。

以下是完整的 Python 代码

import os
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, TIT2, TALB, TPE1, TPE2, TCON, TPUB, TENC, TIT3, APIC, WOAR, TMOO
from PIL import Image

def add_album_art(mp3_file, album_art_file):
    audio = MP3(mp3_file, ID3=ID3)
    if audio.tags is None:
        audio.add_tags()

    # Open album art image
    with open(album_art_file, 'rb') as img:
        audio.tags.add(
            APIC(
                encoding=3,  # 3 is for utf-8
                mime='image/jpeg',  # or image/png
                type=3,  # 3 is for the album front cover
                desc='Cover',
                data=img.read()
            )
        )
    
    audio.save()

def edit_mp3_metadata(mp3_file, title, subtitle, contributing_artists, album_artist, album, genre, publisher, encoded_by, author_url, mood):
    audio = MP3(mp3_file, ID3=ID3)

    if title:
        audio['TIT2'] = TIT2(encoding=3, text=title)  # Title
    if subtitle:
        audio['TIT3'] = TIT3(encoding=3, text=subtitle) # Subtitle
    if contributing_artists:
        audio['TPE1'] = TPE1(encoding=3, text=contributing_artists)  # Contributing artists
    audio['TPE2'] = TPE2(encoding=3, text=album_artist)  # Album artist
    audio['TALB'] = TALB(encoding=3, text=album)  # Album
    if genre:
        audio['TCON'] = TCON(encoding=3, text=genre)  # Genre
    audio['TPUB'] = TPUB(encoding=3, text=publisher)  # Publisher
    audio['TENC'] = TENC(encoding=3, text=encoded_by)  # Encoded by
    audio['WOAR'] = WOAR(encoding=3, url=author_url)  # Author URL
    if mood:
        audio['TMOO'] = TMOO(encoding=3, text=mood)  # Mood
    audio.save()

def main():
    mp3_file = input("Enter the path to the MP3 file: ")
    if not os.path.isfile(mp3_file):
        print(f"Error: The file '{mp3_file}' does not exist.")
        return
    album_art_file = input("\n(Enter to skip from now)\n\nEnter the path to the album art image: ")
    if album_art_file:
        if not os.path.isfile(album_art_file):
            print(f"Error: The file '{album_art_file}' does not exist.")
            return
    
    title = str(input("Enter the title: "))
    subtitle = str(input("Enter the subtitle (Title + Version/Type): "))
    contributing_artists = str(input("Enter the contributing artists (A semicolon and a space at the end of each artist): "))
    album_artist = str(input("Enter the album artist: "))
    album = str(input("Enter the album:  "))
    genre = str(input("Enter the genre: "))
    publisher = str(input("Enter the publisher: "))
    encoded_by = str(input("Enter the encoder: "))
    author_url = str(input("Enter the author URL: "))
    mood = str(input("Enter the mood: "))

    if album_art_file:
        add_album_art(mp3_file, album_art_file)
    edit_mp3_metadata(mp3_file, title, subtitle, contributing_artists, album_artist, album, genre, publisher, encoded_by, author_url, mood)
    print("MP3 file metadata and album art updated successfully.")

if __name__ == "__main__":
    main()

Windows 版本信息:版本:Windows 10 Pro版本:22H2安装于:21-‎02-‎24OS 版本:19045.4651体验:Windows 功能体验包 1000.19060.1000.0

帖子版权声明 1、本帖标题:如何通过 mutagen(Python)为 mp3 文件中的情绪添加价值?
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Asaduzzaman Sohel在本站《python-3.x》版块原创发布, 转载请注明出处!
最新回复 (0)
  • @AmigoJack 我在资源管理器中手动输入了它,方法是转到文件属性,而不是使用任何程序。我也尝试过 TMOO

  • \'Explorer\' 在每个 Windows 版本中都有很大不同(您没有提到),并且多年来一直存在元数据支持不佳的问题。我指的是

返回
作者最近主题: