Python 实用宝典
  • 首页
  • 文章
    • Python 量化投资
    • python 智能家居系列教程
    • Python 爬虫
    • Python 绘图
  • 系列教程
    • Python 教程
    • Python 机器学习教程
    • Celery 教程
  • 数据/源代码下载
  • 联系我们

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

2021-08-17 知识问答 0 0 1.2K

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

已关闭。这个问题需要更加集中。它当前不接受答案。

想改善这个问题吗?更新问题,使其仅通过编辑此帖子来关注一个问题。

上个月关闭。

改善这个问题

如何在Python中检索mp3元数据?

点击查看英文原文
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 5 months ago.

Improve this question

How can I retrieve mp3 metadata in Python?


回答 0

前几天,我使用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()
点击查看英文原文

I used eyeD3 the other day with a lot of success. I found that it could add artwork to the ID3 tag which the other modules I looked at couldn’t. You’ll have to install using pip or download the tar and execute python setup.py install from the source folder.

Relevant examples from the website are below.

Reading the contents of an mp3 file containing either v1 or v2 tag info:

 import eyeD3
 tag = eyeD3.Tag()
 tag.link("/some/file.mp3")
 print tag.getArtist()
 print tag.getAlbum()
 print tag.getTitle()

Read an mp3 file (track length, bitrate, etc.) and access it’s tag:

if eyeD3.isMp3File(f):
     audioFile = eyeD3.Mp3AudioFile(f)
     tag = audioFile.getTag()

Specific tag versions can be selected:

 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.

Or you can iterate over the raw frames:

 tag = eyeD3.Tag()
 tag.link("/some/file.mp3")
 for frame in tag.frames:
    print frame

Once a tag is linked to a file it can be modified and saved:

 tag.setArtist(u"Cro-Mags")
 tag.setAlbum(u"Age of Quarrel")
 tag.update()

If the tag linked in was v2 and you’d like to save it as v1:

 tag.update(eyeD3.ID3_V1_1)

Read in a tag and remove it from the file:

 tag.link("/some/file.mp3")
 tag.remove()
 tag.update()

Add a new tag:

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

回答 1

我以前使用过诱变剂来编辑媒体文件中的标签。mutagen的优点在于它可以处理其他格式,例如mp4,FLAC等。我已经使用此API编写了许多脚本,并取得了很多成功。

点击查看英文原文

I’ve used mutagen to edit tags in media files before. The nice thing about mutagen is that it can handle other formats, such as mp4, FLAC etc. I’ve written several scripts with a lot of success using this API.


回答 2

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

可以通过这种方式访问​​和保存所有其他标签,这将达到大多数目的。可以在《诱变剂指南》中找到更多信息。

点击查看英文原文

A problem with eyed3 is that it will throw NotImplementedError("Unable to write ID3 v2.2") for common MP3 files.

In my experience, the mutagen class EasyID3 works more reliably. Example:

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

All other tags can be accessed this way and saved, which will serve most purposes. More information can be found in the Mutagen Tutorial.


回答 3

您所追求的是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
点击查看英文原文

What you’re after is the ID3 module. It’s very simple and will give you exactly what you need. Just copy the ID3.py file into your site-packages directory and you’ll be able to do something like the following:

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

回答 4

检查这一:

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()
点击查看英文原文

check this one out:

https://github.com/Ciantic/songdetails

Usage example:

>>> import songdetails
>>> song = songdetails.scan("data/song.mp3")
>>> print song.duration
0:03:12

Saving changes:

>>> import songdetails
>>> song = songdetails.scan("data/commit.mp3")
>>> song.artist = "Great artist"
>>> song.save()

回答 5

只是给您的其他信息:

请参阅PythonInMusic页面中的“ MP3内容和元数据编辑器”部分。

点击查看英文原文

Just additional information to you guys:

take a look at the section “MP3 stuff and Metadata editors” in the page of PythonInMusic.


回答 6

《 Dive Into Python》一书中的一个简单示例对我来说很好,这是下载链接,示例是fileinfo.py。不知道它是否是最好的,但它可以完成基本工作。

整本书可在此处在线获得。

点击查看英文原文

A simple example from the book Dive Into Python works ok for me, this is the download link, the example is fileinfo.py. Don’t know if it’s the best, but it can do the basic job.

The entire book is available online here.


回答 7

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

而且我发现了这一点:PyID3Lib,尽管该特定的python绑定发布日期较旧,但它使用的ID3Lib本身就是最新的。

值得一提的是,两者都是LGPL,而且都很好。

点击查看英文原文

I looked the above answers and found out that they are not good for my project because of licensing problems with GPL.

And I found out this: PyID3Lib, while that particular python binding release date is old, it uses the ID3Lib, which itself is up to date.

Notable to mention is that both are LGPL, and are good to go.


回答 8

最简单的方法是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.
点击查看英文原文

easiest method is songdetails..

for read data

import songdetails
song = songdetails.scan("blah.mp3")
if song is not None:
    print song.artist

similarly for edit

import songdetails
song = songdetails.scan("blah.mp3")
if song is not None:
    song.artist = u"The Great Blah"
    song.save()

Don’t forget to add u before name until you know chinese language.

u can read and edit in bulk using python glob module

ex.

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

回答 9

在尝试了pip install此处推荐的eyeD3,pytaglib和ID3模块的简单路由后,我发现第四个选项是唯一可用的选项。其余的有导入错误,缺少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

如广告所示,它很小且设备齐全。

点击查看英文原文

After trying the simple pip install route for eyeD3, pytaglib, and ID3 modules recommended here, I found this fourth option was the only one to work. The rest had import errors with missing dependencies in C++ or something magic or some other library that pip missed. So go with this one for basic reading of ID3 tags (all versions):

https://pypi.python.org/pypi/tinytag/0.18.0

from tinytag import TinyTag
tag = TinyTag.get('/some/music.mp3')

List of possible attributes you can get with 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

It was tiny and self-contained, as advertised.


回答 10

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

点击查看英文原文

The first answer that uses eyed3 is outdated so here is an updated version of it.

Reading tags from an mp3 file:

 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)

An example from the website to modify tags:

 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

An issue I encountered while trying to use eyed3 for the first time had to do with an import error of libmagic even though it was installed. To fix this install the magic-bin whl from here


回答 11

我建议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文件的属性。

点击查看英文原文

I would suggest mp3-tagger. Best thing about this is it is distributed under MIT License and supports all the required attributes.

- artist;
- album;
- song;
- track;
- comment;
- year;
- genre;
- band;
- composer;
- copyright;
- url;
- publisher.

Example:

from mp3_tagger import MP3File

# Create MP3File instance.
mp3 = MP3File('File_Name.mp3')

# Get all tags.
tags = mp3.get_tags()
print(tags)

It supports set, get, update and delete attributes of mp3 files.


回答 12

除了读取元数据外,它还可能完全取决于您要执行的操作。如果仅是您需要的比特率/名称等,而没有别的,那么最好是轻量级的。

如果您要操作的是mp3,则建议使用PyMedia。

无论您得到什么,都有很多东西,请确保在大量示例媒体上进行测试。特别是ID3标签有几种不同的版本,因此请确保它不是过时的。

我个人很幸运地使用了这个小的MP3Info类。虽然已经很老了。

http://www.omniscia.org/~vivake/python/MP3Info.py

点击查看英文原文

It can depend on exactly what you want to do in addition to reading the metadata. If it is just simply the bitrate / name etc. that you need, and nothing else, something lightweight is probably best.

If you’re manipulating the mp3 past that PyMedia may be suitable.

There are quite a few, whatever you do get, make sure and test it out on plenty of sample media. There are a few different versions of ID3 tags in particular, so make sure it’s not too out of date.

Personally I’ve used this small MP3Info class with luck. It is quite old though.

http://www.omniscia.org/~vivake/python/MP3Info.py


回答 13

经过一些初步研究,我认为songdetails可能适合我的用例,但它不能处理.m4b文件。诱变剂确实如此。请注意,尽管有些人(合理地)对Mutagen的格式本机键出现问题有所不同,但格式不同(TIT2表示mp3,ogg的标题,\ xa9nam表示mp4,WMA的标题等),mutagen.File( )有一个(new?)easy = True参数,该参数提供EasyMP3 / EasyID3标签,这些标签具有一致的(尽管数量有限)密钥集。到目前为止,我只进行了有限的测试,但是使用easy = True时,.mb4和.mp3文件的通用密钥(如专辑,艺术家,专辑艺术家,类型,音轨编号,光盘编号等)都存在并且相同。对我来说非常方便。

点击查看英文原文

After some initial research I thought songdetails might fit my use case, but it doesn’t handle .m4b files. Mutagen does. Note that while some have (reasonably) taken issue with Mutagen’s surfacing of format-native keys, that vary from format to format (TIT2 for mp3, title for ogg, \xa9nam for mp4, Title for WMA etc.), mutagen.File() has a (new?) easy=True parameter that provides EasyMP3/EasyID3 tags, which have a consistent, albeit limited, set of keys. I’ve only done limited testing so far, but the common keys, like album, artist, albumartist, genre, tracknumber, discnumber, etc. are all present and identical for .mb4 and .mp3 files when using easy=True, making it very convenient for my purposes.


回答 14

使用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
点击查看英文原文

using 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

回答 15

我使用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>
点击查看英文原文

I used tinytag 1.3.1 because

  1. It is actively supported:
1.3.0 (2020-03-09):
added option to ignore encoding errors ignore_errors #73
Improved text decoding for many malformed files
  1. It supports the major formats:
MP3 (ID3 v1, v1.1, v2.2, v2.3+)
Wave/RIFF
OGG
OPUS
FLAC
WMA
MP4/M4A/M4B
  1. The code WORKED in just a few minutes of development.
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)

  • RESULT
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>

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
metadata mp3 Python
Python实用宝典
分享 收藏 点赞(0)
上一篇
socket.shutdown与socket.close
下一篇
pytorch中的模型摘要

相关文章

知识问答

在Python中格式化多行字典的正确方法是什么?

在Python中,我想在代码中编写多行字典。有几种方法可以格式化它。我想到的是一...
4 年前 0 0 1.2K
知识问答

如何在python解释器中执行文件?

我正在尝试从解释器中使用python命令执行文件。 编辑:我正在尝试使用该文件中...
4 年前 0 0 1.1K
知识问答

Python等同于PHP的内爆吗?

有没有等同于PHP在Python中内爆的工具?我已经阅读并拆分了一组分隔的单词,...
4 年前 0 0 911
知识问答

用于将PDF转换为文本的Python模块

关闭。此问题不符合堆栈溢出准则。它当前不接受答案。 想改善这个问题吗? 更新问题...
4 年前 0 0 1.1K
排行榜展示
1

Python 情人节超强技能 导出微信聊天记录生成词云

2

你不得不知道的python超级文献批量搜索下载工具

3

Python 流程图 — 一键转化代码为流程图

4

7行代码 Python热力图可视化分析缺失数据处理

5

Python 优化—算出每条语句执行时间

6

你的10W块放哪里能赚最多钱?

文章展示

如何从Tkinter文本小部件获取输入?

查找Python解释器的完整路径?

如何记录带有调试信息的Python错误?

使用Python将JSON字符串转换为dict

python中的一线ftp服务器

快速计数正整数中的非零位的方法

Python 实用宝典

Python 实用宝典,专注于Python教程、深度学习教程、机器学习教程、智能家居教程、数据分析教程、自动化、可视化、数据下载、训练数据下载、模型下载等领域。

快速导航

  • 个人中心
  • 标签云
  • 网址导航

关于本站

  • VIP介绍
  • 客服咨询
  • 推广计划

联系我们

如有BUG或建议可与我们在线联系或登录本站账号进入个人中心提交工单或邮件联系 admin@pythondict.com。
Copyright © 2023 Python 实用宝典 - All rights reserved
  • 首页
  • 用户中心
  • 会员介绍
  • QQ客服
  • 首页
  • 分类
  • 会员
  • 我的
Python 实用宝典
微信支付
请使用 微信 扫码支付