Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python访问MP3元数据_Python_Mp3_Metadata - Fatal编程技术网

用Python访问MP3元数据

用Python访问MP3元数据,python,mp3,metadata,Python,Mp3,Metadata,如何在Python中检索mp3元数据?书中的一个简单示例Dive Into Python works对我来说很好,就是下载链接,示例是fileinfo.py。我不知道这是不是最好的,但它可以做基本的工作 整本书都可以在网上找到。除了阅读元数据外,它还取决于你想做什么。如果您只需要比特率/名称等,而不需要其他东西,那么轻量级的东西可能是最好的 如果您正在操作mp3,那么PyMedia可能是合适的 有相当多的,无论你得到什么,确保并测试了大量的样本媒体。特别是ID3标签有几个不同的版本,所以请确保它

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

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


整本书都可以在网上找到。

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

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

有相当多的,无论你得到什么,确保并测试了大量的样本媒体。特别是ID3标签有几个不同的版本,所以请确保它不是太过时

就我个人而言,我很幸运地使用了这个小型MP3Info类。不过它很老了

您想要的是模块。它非常简单,可以满足您的需求。只需将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
我以前在媒体文件中编辑过标签。诱变剂的好处是它可以处理其他格式,如mp4、FLAC等。我已经用这个API编写了几个脚本,并取得了很大成功。

前几天我用它取得了很大成功。我发现它可以在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的许可问题

我发现:,虽然特定的python绑定发布日期很旧,但它使用了,它本身是最新的

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

请查看以下内容:

用法示例:

>>> 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素材和元数据编辑器”部分。

经过一些初步研究,我认为songdetails可能适合我的用例,但它不处理.m4b文件。诱变剂确实如此。请注意,虽然有些人(合理地)对诱变剂的格式本机密钥表示异议,这些密钥因格式而异(mp3为TIT2,mp4为title,\xa9nam,WMA为title等),但诱变剂.File()有一个(新的?)easy=True参数,该参数提供EasyMP3/EasyID3标记,这些标记具有一致但有限的密钥集。到目前为止,我只做了有限的测试,但是当使用easy=True时,.mb4和.mp3文件的常用键,如album、artist、AlbumArtister、流派、tracknumber、discnumber等都存在并且相同,这使我的使用非常方便。

eyed3的一个问题是它会抛出
NotImplementedError(“无法写入常见MP3文件的ID3 v2.2”)

根据我的经验,
诱变剂
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()
所有其他标签都可以通过这种方式访问和保存,这将用于大多数目的。更多信息可以在中找到。

最简单的方法是

用于读取数据

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模块进行批量读取和编辑


使用的第一个答案已经过时,因此这里是它的更新版本

从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的导入错误有关,即使它已安装。要修复此安装,请在尝试此处推荐的eyed3、pytaglib和ID3模块的简单
pip安装
路径后,从安装magic bin whl,我发现第四个选项是其余的都有导入错误,在C++中有缺失的依赖项或魔术或其他库, PIP错过了。所以用这个来读ID3标签(所有版本):

使用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
正如广告所宣传的那样,它很小而且是独立的。

我建议。最好的是它是在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文件的属性。

使用

导入eyed3
导入操作系统
对于os.walk(folderp)中的根目录、目录和文件:
对于文件中的文件:
尝试:
如果file.find(“.mp3”)<0:
持续
path=os.path.abspath(os.path.join(根,文件))
t=eyed3.负载(路径)
印刷品(t.tag.title,t.tag.artist)
#打印(t.getArtist())
例外情况除外,如e:
打印(e)
持续
我之所以使用

  • 它得到积极支持:
  • 它支持以下主要格式:
  • 代码只需几分钟的开发就可以工作
  • 从tinytag导入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()
    对于fileNameL中的fn:
    fpath='./数据/'+fn
    tag=TinyTag.get(fpath)
    脉波重复间隔
    
    - 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)
    
    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
    
    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
    
    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>