修改MP3文件标题专辑等信息

第一版本(用eyeD3)

因为音乐APP导入音乐后,显示的并不是音乐的文件名而已MP3文件的title。所以为了方便我自己,我要给MP3的title添加一个序号前缀方便排序。
例如:需要把子文件夹的编号0001作为MP3的title前缀,文件名的最后两个字母dg作为title后缀。修改新的文件名和title一样。


操作要求
import eyed3
import os
import re

#过滤不能做文件名的字符
def validateTitle(title):
    rstr = r"[\/\\\:\*\?\"\<\>\|!]"  # '/ \ : * ? " < > |'
    new_title = re.sub(rstr, "_", title)  # 替换为下划线
    return new_title

'''
#遍历路径中的特定文件生成列表
def get_mp3(mp3path):
    mp3_list = []
    for dirs, dirnames, files in os.walk(mp3path):
        for file in files:
            if file.endswith('.mp3'):
                mp3_list.append(dirs + '\\'+ file)
    return mp3_list

'''

#遍历路径中的文件,然后生成列表
def get_mp3(mp3path):
    mp3_list = []
    for dirs, dirnames, files in os.walk(mp3path):  #os.walk出来是列表
        for file in files:  #取出原文件名
            if file.endswith('.mp3'): #过滤非mp3文件
                mp3_list.append(dirs + '\\' + file)
                mp3_fullpath = dirs + '\\' + file  #拼接文件全路径
                print('mp3_fullpath: '+str(mp3_fullpath))
                print('====================================')
                Tag = eyed3.load(mp3_fullpath).tag   #提取mp3文件tag信息
                mp3_title = str(Tag.title) + file[-6:-4]  #新title用原来title+文件名后2个字符
                print('mp3_old_title: '+str(mp3_title))
                print('====================================')
                
                #修改title
                new_title = str(dirs).split("\\")[-1] + mp3_title  #新title等于子目录(例001)+新title
                print('new_title: ' + str(new_title))
                print('====================================')
                Tag.title = new_title   #修改mp3文件title
                Tag.save()  #保存修改
                print('mp3_new_title: '+str(new_title))
                print('====================================')
                
                #重命名
                new_title = validateTitle(new_title)  #新的title过滤掉不能作为文件名的特殊字符
                os.rename(mp3_fullpath, dirs +'\\'+ new_title + '.mp3')   #重命名mp3文件
                print('new_fullpath: ' + dirs +'\\'+ new_title + '.mp3')
                print('====================================')

                
    return mp3_list

#MP3文件路径
mp3path = r'E:\BaiduNetdiskDownload\31-60'
#mp3path = r'C:\Users\Administrator\Desktop'
#print(get_mp3(mp3path))

get_mp3(mp3path)

第二版本(用mutagen)

在用第一版本用eyeD3库来处理,但是一些MP3的是ID3v2版本好像会出错。所以用mutagen库来处理。

from mutagen.id3 import ID3, APIC, TIT2, TPE1, TALB
import os
import re

#过滤不能做文件名的字符
def validateTitle(title):
    rstr = r"[\/\\\:\*\?\"\<\>\|!]"  # '/ \ : * ? " < > |'
    new_title = re.sub(rstr, "_", title)  # 替换为下划线
    return new_title


#遍历路径中的特定文件生成列表
def get_mp3_list(mp3path):
    mp3_list = []
    for dirs, dirnames, files in os.walk(mp3path):
        for file in files:
            if file.endswith('.mp3'):  #筛选mp3文件
                mp3_list.append(dirs + '\\'+ file)   #把路径+文件名加入列表,方便后面使用
                mp3_name = file.replace('.mp3', '')   #去除“.mp3”后缀,后面用于title
                print(mp3_name)
    return mp3_list

#修改mp3文件信息
def SetMp3Info(mp3file, title, artist, album):
    songFile = ID3(mp3file)
    songFile['TIT2'] = TIT2(encoding = 3, text = title) #编辑title
    songFile['TPE1'] = TPE1(encoding = 3, text = artist) #编辑艺术家
    songFile['TALB'] = TALB(encoding = 3, text = album) #编辑专辑
    songFile.save()
    print('修改后:')
    print(songFile['TIT2'])
    print(songFile['TPE1'])
    print(songFile['TALB'])

#读取MP3的元数据等信息
def readMp3(mp3file):
    songFile = ID3(mp3file)
    print('修改后:')
    print(songFile['TIT2'])
    print(songFile['TPE1'])
    print(songFile['TALB'])



#遍历路径中的文件,然后生成列表
def get_mp3(mp3path):
    mp3_list = []
    for dirs, dirnames, files in os.walk(mp3path):  #os.walk出来是列表
        for file in files:  #取出原文件名
            if file.endswith('.mp3'): #过滤非mp3文件
                mp3_list.append(dirs + '//' + file)
                mp3_fullpath = dirs + '//' + file  #拼接文件全路径
                print('mp3_fullpath: '+str(mp3_fullpath))
                print('====================================')

                mp3_title = file.replace('.mp3', '')
                # print('====================================')
                
                #修改title,作者,专辑
                SetMp3Info(mp3_fullpath, mp3_title, "赖世雄", "美语音标")

                #查看信息
                readMp3(mp3_fullpath)

                
    return mp3_list


mp3path = r'C:\Users\Administrator\Desktop\test'


get_mp3(mp3path)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容