Skip to main content
 首页 » 编程设计

Python之如何添加初始 MP3 属性

2025年05月04日12lidabo

我曾经在文件名中获取带有艺术家和歌曲名称的歌曲(例如 - “布兰妮斯 PIL 斯 - 哎呀我又做了一次”。 我的脚本有两个目的: 1.将艺术家姓名和歌曲添加到他的 MP3 属性中(使用 eyed3)。 2.在我的主音乐文件夹中为艺术家创建一个新文件夹(如果我还没有)。

我的问题是,如果 MP3 文件没有属性,我就不能添加新的..

这是我的代码(这是我的第一个 :-))..谢谢!

#That's the 0.2 ver of my code 
import os 
import shutil 
import eyed3.id3 
 
songs_path = raw_input("Please insert the path of your Songs: ") 
music_path = raw_input("Please insert the path of your music folders location: ") 
 
#That's function supposed to present the files in a path 
def files_in_folder(m): 
    Files = os.listdir(m) 
    return Files 
 
mp3_files_list = files_in_folder(downloads_path) 
artist_list = files_in_folder(music_path) 
 
for i in mp3_files_list: 
    song_artist, song_title = i.split(' - ') 
    if not os.path.exists(music_path + '\\' + song_artist): 
        os.mkdir(music_path + '\\' + song_artist, 0777 ) 
    src_file = os.path.join(downloads_path, i) 
    dst_file = os.path.join(music_path + '\\' + song_artist + '\\' + song_title) 
    print src_file 
    print dst_file 
    shutil.move(src_file, dst_file) 
    track_mp3_file = eyed3.load(dst_file) 
    if track_mp3_file.tag is None: 
        track_mp3_file.tag = eyed3.id3.Tag() 
        track_mp3_file.tag.file_info = eyed3.id3.FileInfo(dst_file) 
    track_mp3_file.tag.artist = unicode(song_artist, "UTF-8") 
    print track_mp3_file.tag.artist 
    track_mp3_file.tag.title = unicode(song_title, "UTF-8") 
    track_mp3_file.tag.save() 

请您参考如下方法:

尝试用另一个 id3 版本保存标签:

track_mp3_file.tag.save(version=(2, 3, 0))

来自维基百科:

Windows Explorer and Windows Media Player cannot handle ID3v2.4 tags in any version, up to and including Windows 8 / Windows Media Player 12. Windows can understand ID3v2 up to and including version 2.3