python 使用eyed3在MP3文件上设置ID3标签

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用eyed3在MP3文件上设置ID3标签相关的知识,希望对你有一定的参考价值。

# I used this code within an IPython session to clean up all the missing ID3 tags from my MP3 collection.
# They had gone missing years ago when I down-sampled them to fit on an old phone, and then lost the originals.
# Fortunately I named the files themselves with the basic details (artist, date, title and so on) so it was possible to
# recover the tags... It sat on my to-do list for *years* but now I finally did it.

# I used the Python library "EyeD3" (get it?): https://pypi.python.org/pypi/eyeD3
# This requires Python 2.7, which has some interesting quirks for Unicode, a bit of a pain since I had named my MP3s
# with utf8 characters. What I've come up with *mostly* works.  When it doesn't I had to resort to manually editing (using 
# Clementine).


import eyed3
import os
import os.path
import glob

#In ipython:  cd ~/Music/some_artist
artist=u'Some Artist'

#note the glob to select folders for tagging MP3s
# This expects music tracks to be stored within album folders, and to be named like this:
#
#  Artist/YYYY - Album Name/nn - Song Title.mp3
#
# It will pick out the relevant parts of these names for use in the ID3 tags.
for folder in glob.glob("*"):
     (year,album)=unicode(os.path.split(os.path.abspath(folder))[-1],"utf8").split(" - ")
     print year, album
     year=eyed3.core.Date(int(year))
     for file in glob.glob(os.path.join(folder,"*.mp3")):
        (track_num,title)=unicode(os.path.splitext(os.path.basename(file))[0],"utf8").split(" - ")
        song=eyed3.load(file).tag
        song.track_num=int(track_num)
        song.title=title
        song.album=album
        song.artist=artist
        song.recording_date=year
        song.save()

# Sometimes my MP3s had no tags set *at all*.  There's probably a way to initialise tags with eyed3, but I found that
# the easiest was to load an album with Clementine and set a tag for all the files -- e.g. the Artist tag.  After that
# then the above code works.

# In some rare cases I had to do an album at a time:

#cd ~/Music/Funky_artist/1974 - Some Album That breaks above code
artist=u'Funky Artist'
year=eyed3.core.Date(1974)
album=u'Some Album That breaks above code'

for file in glob.glob("*.mp3"):
     song=eyed3.load(file).tag
     (track_num,title)=unicode(os.path.splitext(file)[0],"utf8").split(" - ")
     song.track_num=int(track_num)
     song.title=title
     song.album=album
     song.artist=artist
     song.recording_date=year
     song.save()
      
 # And that takes care of my missing ID3 tags.

以上是关于python 使用eyed3在MP3文件上设置ID3标签的主要内容,如果未能解决你的问题,请参考以下文章

[使用eyeD3从Python中的mp3文件检索歌词

使用 eyeD3 在 python 2.7 中标记 mp3 文件

在 Python 中通过 eyeD3 更新 mp3 标签

python的音频处理库

eyeD3 -- 递归标签删除

使用 Python 访问 MP3 元数据 [关闭]