使用 Python 访问 MP3元数据

是否有一个维护的包,我可以使用 Python 检索和设置 MP3 ID3元数据?

149083 次浏览

这本书中的一个简单例子 Dive Into Python works for me,这个是下载链接,例子是 fileinfo.py。不知道它是不是最好的,但它可以做基本的工作。

整本书可在线购买。

除了读取元数据之外,它还取决于您想要做什么。如果只需要比特率/名称等,没有其他需要,那么轻量级的可能是最好的。

如果您正在操作 mp3,那么 PyMedia 可能是合适的。

有相当多的,无论你得到什么,确保和测试它在大量的样品介质。特别是 ID3标签有几个不同的版本,所以要确保它不会过时。

就我个人而言,我很幸运地使用了这个小的 MP3Info 类。

Http://www.omniscia.org/~vivake/python/mp3info.py

你要找的是 表格3模块。这很简单,而且会给你你需要的东西。只要将 ID3.py 文件复制到 site-package 目录中,就可以执行以下操作:

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

我以前使用过 诱变剂来编辑媒体文件中的标记。诱变剂的好处是它可以处理其他格式,如 mp4,FLAC 等。我已经使用这个 API 编写了几个脚本,并取得了很大的成功。

前几天我用了 眼睛 D3,很成功。我发现它可以添加艺术品的 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()

我查看了上面的答案,发现它们对我的项目不利,因为 GPL 的许可问题。

我发现: PyID3Lib,虽然特定的 蟒蛇绑定发布日期是旧的,它使用的是 ID3Lib,它本身是最新的。

值得一提的是,两者都是 LGPL,并且都可以使用。

看看这个:

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()

给你们提供一些额外的信息:

看看 巨蟒音乐页面中的“ MP3素材和元数据编辑器”部分。

经过一些初步的研究,我认为歌曲细节可能适合我的用例,但它不处理。M4b 文件。变异原可以。请注意,虽然有些人(合理地)对 Mutagen 的浮出水面的格式-本地键,不同的格式(TIT2为 mp3,Title 为 ogg,xa9nam 为 mp4,Title 为 WMA 等) ,诱变剂。File ()有一个(新的?)Easy = True 参数,该参数提供 EasyMP3/EasyID3标记,这些标记具有一致的(尽管有限)键集。到目前为止,我只做了有限的测试,但常见的键,如专辑,艺术家,专辑艺术家,流派,曲目号码,唱片号码,等都是现在和相同的。Mb4及。当使用 easy = True 时,使用 mp3文件,这对我的目的来说非常方便。

eyed3的一个问题是它会抛出 NotImplementedError("Unable to write ID3 v2.2")来获取常见的 MP3文件。

根据我的经验,mutagenEasyID3工作起来更可靠。例如:

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()

所有其他标记都可以通过这种方式访问和保存,这将满足大多数用途。更多信息可以在 变异原教程中找到。

最简单的方法是 歌曲细节

读取数据

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()

不要忘记在名字前面加上 ,直到你懂中文。

您可以使用 python globb 模块批量读取和编辑

前男友。

import glob
songs = glob.glob('*')   # script should be in directory of songs.
for song in songs:
# do the above work.

第一个使用 眼睛3的答案已经过时了,所以这里有一个更新的版本。

从 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 的导入错误有关,尽管它已经被安装了。要修复此问题,请从 给你安装魔术箱 whl

在尝试了这里推荐的 eyD3、 pytaglib 和 ID3模块的简单 pip install路由之后,我发现第四个选项是唯一可以工作的选项。其余的导入错误包括 C + + 中缺少依赖项,或者一些神奇的东西,或者 pip缺少的其他库。因此,使用这个来读取 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

正如广告宣传的那样,它很小而且自给自足。

我建议使用 翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳。最好的一点是它是在 麻省理工学院执照下发布的,支持所有需要的属性。

- 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文件的设置、获取、更新和删除属性。

使用 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

我用 Tinytag 1.3.1是因为

  1. 它得到了积极的支持:
1.3.0 (2020-03-09):
added option to ignore encoding errors ignore_errors #73
Improved text decoding for many malformed files
  1. 它支持主要格式:
MP3 (ID3 v1, v1.1, v2.2, v2.3+)
Wave/RIFF
OGG
OPUS
FLAC
WMA
MP4/M4A/M4B
  1. 代码在几分钟的开发中就工作了。
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:\1d\PodcastPjs\JoeTagPj>