Python3批量修改MP3文件的标签
程序员文章站
2022-06-21 15:06:38
...
环境
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
pip3 --version
pip 19.2.3
pip3 install eyed3
借助eyed3模块还实现,执行前需要安装libmagic的DLL.。pip3 install python-magic-bin==0.4.14
eyed3读取文件的时候,发现中文文件名报错,但是改成数字或者英文名正常,所以有个替换文件名的操作。
# -*- coding: utf-8 -*-
import eyed3
import os
import operator
prefix=r".mp3"
path=r"C:\Users\f\Desktop\神奇\后续的"
for eachfile in os.listdir(path):
length=len(eachfile)
if operator.eq(prefix,eachfile[length-4:length])==True:
title=u""+eachfile[0:length-4]
print(title)
print(eachfile)
track_num=eachfile[0:eachfile.find('.')]
print(track_num)
newname=str(track_num)+".mp3"
os.rename(os.path.join(path,eachfile), os.path.join(path, newname))
audiofile=eyed3.load(newname)
audiofile.initTag()
audiofile.tag.album = u"神奇"
audiofile.tag.track_num =int(track_num)
audiofile.tag.title =title
audiofile.tag.album_artist = u"故事"
audiofile.tag.save()
os.rename(os.path.join(path,newname), os.path.join(path, eachfile))