使用 Python 访问 MP3 元数据[关闭]
- 2025-03-05 09:14:00
- admin 原创
- 89
问题描述:
是否有一个维护的包可以用来使用 Python 检索和设置 MP3 ID3 元数据?
解决方案 1:
前几天我使用eyeD3取得了很大成功。我发现它可以将艺术作品添加到 ID3 标签中,而我看过的其他模块却做不到。您必须使用 pip 安装或下载 tar 并python setup.py install
从源文件夹执行。
以下是来自该网站的相关示例。
读取包含 v1 或 v2 标签信息的 mp3 文件的内容:
import eyeD3
tag = eyeD3.Tag()
tag.link("/some/file.mp3")
print tag.getArtist()
print tag.getAlbum()
print tag.getTitle()
读取 mp3 文件(曲目长度、比特率等)并访问其标签:
if eyeD3.isMp3File(f):
audioFile = eyeD3.Mp3AudioFile(f)
tag = audioFile.getTag()
可以选择特定的标签版本:
tag.link("/some/file.mp3", eyeD3.ID3_V2)
tag.link("/some/file.mp3", eyeD3.ID3_V1)
tag.link("/some/file.mp3", eyeD3.ID3_ANY_VERSION) # The default.
或者您可以迭代原始帧:
tag = eyeD3.Tag()
tag.link("/some/file.mp3")
for frame in tag.frames:
print frame
一旦标签链接到文件,就可以修改和保存:
tag.setArtist(u"Cro-Mags")
tag.setAlbum(u"Age of Quarrel")
tag.update()
如果链接的标签是 v2 并且您想将其保存为 v1:
tag.update(eyeD3.ID3_V1_1)
读入标签并将其从文件中删除:
tag.link("/some/file.mp3")
tag.remove()
tag.update()
添加新标签:
tag = eyeD3.Tag()
tag.link('/some/file.mp3') # no tag in this file, link returned False
tag.header.setVersion(eyeD3.ID3_V2_3)
tag.setArtist('Fugazi')
tag.update()
解决方案 2:
我以前曾使用mutagen编辑媒体文件中的标签。mutagen 的优点在于它可以处理其他格式,例如 mp4、FLAC 等。我使用此 API 编写了多个脚本,取得了很大成功。
解决方案 3:
问题是eyed3
它会对NotImplementedError("Unable to write ID3 v2.2")
常见的 MP3 文件抛出错误。
根据我的经验,该mutagen
课程EasyID3
更加可靠。例如:
from mutagen.easyid3 import EasyID3
audio = EasyID3("example.mp3")
audio['title'] = u"Example Title"
audio['artist'] = u"Me"
audio['album'] = u"My album"
audio['composer'] = u"" # clear
audio.save()
所有其他标签都可以通过这种方式访问和保存,这可以满足大多数用途。更多信息可以在Mutagen 教程中找到。
解决方案 4:
您需要的是ID3模块。它非常简单,可以满足您的所有需求。只需将 ID3.py 文件复制到您的 site-packages 目录中,您就可以执行以下操作:
from ID3 import *
try:
id3info = ID3('file.mp3')
print id3info
# Change the tags
id3info['TITLE'] = "Green Eggs and Ham"
id3info['ARTIST'] = "Dr. Seuss"
for k, v in id3info.items():
print k, ":", v
except InvalidTagError, message:
print "Invalid ID3 tag:", message
解决方案 5:
在尝试了这里推荐的 eyeD3、pytaglib 和 ID3 模块的简单pip install
路径后,我发现第四个选项是唯一可行的。其余的选项都存在导入错误,缺少 C++ 中的依赖项,或者pip
缺少 magic 或其他库。因此,请使用这个来基本读取 ID3 标签(所有版本):
https://pypi.python.org/pypi/tinytag/0.18.0
from tinytag import TinyTag
tag = TinyTag.get('/some/music.mp3')
使用 TinyTag 可以获得的属性列表:
tag.album # album as string
tag.albumartist # album artist as string
tag.artist # artist name as string
tag.audio_offset # number of bytes before audio data begins
tag.bitrate # bitrate in kBits/s
tag.disc # disc number
tag.disc_total # the total number of discs
tag.duration # duration of the song in seconds
tag.filesize # file size in bytes
tag.genre # genre as string
tag.samplerate # samples per second
tag.title # title of the song
tag.track # track number as string
tag.track_total # total number of tracks as string
tag.year # year or data as string
正如广告所说,它很小而且设施齐全。
解决方案 6:
看看这个:
https://github.com/Ciantic/songdetails
使用示例:
>>> import songdetails
>>> song = songdetails.scan("data/song.mp3")
>>> print song.duration
0:03:12
保存更改:
>>> import songdetails
>>> song = songdetails.scan("data/commit.mp3")
>>> song.artist = "Great artist"
>>> song.save()
解决方案 7:
最简单的方法是songdetails ..
用于读取数据
import songdetails
song = songdetails.scan("blah.mp3")
if song is not None:
print song.artist
同样对于编辑
import songdetails
song = songdetails.scan("blah.mp3")
if song is not None:
song.artist = u"The Great Blah"
song.save()
在您懂中文之前,请不要忘记在名字前添加u 。
您可以使用 python glob 模块批量读取和编辑
前任。
import glob
songs = glob.glob('*') # script should be in directory of songs.
for song in songs:
# do the above work.
解决方案 8:
只需向你们提供更多信息:
查看PythonInMusic页面中的“MP3 内容和元数据编辑器”部分。
解决方案 9:
我使用tinytag 1.3.1,因为
它得到积极支持:
1.3.0 (2020-03-09):
added option to ignore encoding errors ignore_errors #73
Improved text decoding for many malformed files
它支持主要格式:
MP3 (ID3 v1, v1.1, v2.2, v2.3+)
Wave/RIFF
OGG
OPUS
FLAC
WMA
MP4/M4A/M4B
仅需几分钟的开发时间,代码便可运行。
from tinytag import TinyTag
fileNameL ='''0bd1ab5f-e42c-4e48-a9e6-b485664594c1.mp3
0ea292c0-2c4b-42d4-a059-98192ac8f55c.mp3
1c49f6b7-6f94-47e1-a0ea-dd0265eb516c.mp3
5c706f3c-eea4-4882-887a-4ff71326d284.mp3
'''.split()
for fn in fileNameL:
fpath = './data/'+fn
tag = TinyTag.get(fpath)
print()
print('"artist": "%s",' % tag.artist)
print('"album": "%s",' % tag.album)
print('"title": "%s",' % tag.title)
print('"duration(secs)": "%s",' % tag.duration)
结果
JoeTagPj>python joeTagTest.py
"artist": "Conan O’Brien Needs A Friend",
"album": "Conan O’Brien Needs A Friend",
"title": "17. Thomas Middleditch and Ben Schwartz",
"duration(secs)": "3565.1829583532785",
"artist": "Conan O’Brien Needs A Friend",
"album": "Conan O’Brien Needs A Friend",
"title": "Are you ready to make friends?",
"duration(secs)": "417.71840447045264",
"artist": "Conan O’Brien Needs A Friend",
"album": "Conan O’Brien Needs A Friend",
"title": "Introducing Conan’s new podcast",
"duration(secs)": "327.22187551899646",
"artist": "Conan O’Brien Needs A Friend",
"album": "Conan O’Brien Needs A Friend",
"title": "19. Ray Romano",
"duration(secs)": "3484.1986772305863",
C:dPodcastPjsJoeTagPj>
解决方案 10:
我查看了上述答案并发现它们不适合我的项目,因为 GPL 存在许可问题。
我发现了这一点:PyID3Lib,虽然那个特定的python 绑定发布日期很旧,但它使用ID3Lib,而 ID3Lib 本身是最新的。
值得一提的是,两者都是LGPL,并且可以正常使用。
解决方案 11:
对我来说,《深入 Python》一书中的一个简单的示例就足够了,这是下载链接,示例是 fileinfo.py。不知道它是否是最好的,但它可以完成基本工作。
整本书可以在这里在线获取。
解决方案 12:
第一个使用eyed3的答案已经过时了,因此这里是它的更新版本。
从 mp3 文件中读取标签:
import eyed3
audiofile = eyed3.load("some/file.mp3")
print(audiofile.tag.artist)
print(audiofile.tag.album)
print(audiofile.tag.album_artist)
print(audiofile.tag.title)
print(audiofile.tag.track_num)
网站上修改标签的一个示例:
import eyed3
audiofile = eyed3.load("some/file.mp3")
audiofile.tag.artist = u"Integrity"
audiofile.tag.album = u"Humanity Is The Devil"
audiofile.tag.album_artist = u"Integrity"
audiofile.tag.title = u"Hollow"
audiofile.tag.track_num = 2
我第一次尝试使用 eyed3 时遇到的一个问题与 libmagic 的导入错误有关,尽管它已安装。要解决此问题,请从此处安装 magic-bin whl
解决方案 13:
我建议使用mp3-tagger。它最好的一点是它是在MIT 许可下发布的,并且支持所有必需的属性。
- artist;
- album;
- song;
- track;
- comment;
- year;
- genre;
- band;
- composer;
- copyright;
- url;
- publisher.
例子:
from mp3_tagger import MP3File
# Create MP3File instance.
mp3 = MP3File('File_Name.mp3')
# Get all tags.
tags = mp3.get_tags()
print(tags)
它支持设置、获取、更新和删除 mp3 文件的属性。
解决方案 14:
除了读取元数据之外,这还取决于您到底想做什么。如果您只需要比特率/名称等,而不需要其他内容,那么轻量级的东西可能是最好的。
如果您正在操作 mp3,那么 PyMedia 可能适合。
有相当多的标签,无论你得到什么,一定要在大量的样本媒体上进行测试。特别是 ID3 标签有几个不同的版本,所以要确保它不是太过时。
就我个人而言,我曾使用过这个小型 MP3Info 类,但效果不错。不过它已经很老了。
http://www.omniscia.org/~vivake/python/MP3Info.py
解决方案 15:
经过一些初步研究,我认为 songdetails 可能适合我的用例,但它不能处理 .m4b 文件。Mutagen 可以。请注意,虽然有些人(合理地)对 Mutagen 显示的格式本机键提出质疑,这些键因格式而异(mp3 为 TIT2,ogg 为 title,mp4 为 ©nam,WMA 为 Title 等),但 mutagen.File() 有一个(新的?)easy=True 参数,它提供 EasyMP3/EasyID3 标签,这些标签具有一组一致但有限的键。到目前为止,我只做了有限的测试,但当使用 easy=True 时,albumb4 和 .mp3 文件中的常用键(如专辑、艺术家、albumbartist、流派、曲目编号、唱片编号等)都存在且相同,这对我来说非常方便。
解决方案 16:
使用https://github.com/nicfit/eyeD3
import eyed3
import os
for root, dirs, files in os.walk(folderp):
for file in files:
try:
if file.find(".mp3") < 0:
continue
path = os.path.abspath(os.path.join(root , file))
t = eyed3.load(path)
print(t.tag.title , t.tag.artist)
#print(t.getArtist())
except Exception as e:
print(e)
continue
扫码咨询,免费领取项目管理大礼包!