如何使用eyed3从python中的.mp3文件中获取详细信息(标题、艺术家)

如何使用eyed3从python中的.mp3文件中获取详细信息(标题、艺术家),python,mp3,eyed3,Python,Mp3,Eyed3,这是我的密码 import eyed3 audiofile = eyed3.load("19 Calvin Harris - Summer.mp3") print(audiofile.tag.artist) 这是一个错误 Traceback (most recent call last): File "C:\Python34\testmp3.py", line 5, in <module> print(audiofile.tag.artist) AttributeE

这是我的密码

import eyed3

audiofile = eyed3.load("19 Calvin Harris - Summer.mp3")

print(audiofile.tag.artist)
这是一个错误

Traceback (most recent call last):
  File "C:\Python34\testmp3.py", line 5, in <module>
    print(audiofile.tag.artist)
AttributeError: 'NoneType' object has no attribute 'artist'
回溯(最近一次呼叫最后一次):
文件“C:\Python34\testmp3.py”,第5行,在
打印(audiofile.tag.artist)
AttributeError:“非类型”对象没有属性“艺术家”

VisualStudio中显示了一些属性。但是当我运行它时,发生了一个错误

当我写
打印(音频文件)
时,它会工作。我不知道为什么
ps.Python3.4.

标题和艺术家可通过
标记()的访问器函数
返回值获得。下面的示例显示了如何使用
getArtist()
getTitle()
方法获取它们

 import eyed3
 tag = eyed3.Tag()
 tag.link("/some/file.mp3")
 print tag.getArtist()
 print tag.getTitle()
试试这个:

if audiofile.tag is None:
            audiofile.tag = eyed3.id3.Tag()
            audiofile.tag.file_info = eyed3.id3.FileInfo("foo.id3")
    audiofile.tag.artist=unicode(artist, "utf-8")

我认为问题出在模块内部

我使用以下代码进行了一些调试:

from eyed3 import id3

tag = id3.Tag()
tag.parse("myfile.mp3")
print(tag.artist)
在parse函数中,打开文件,然后将其传递给_loadV2Tag(fileobject)。然后,模块读取文件头的前几行并检查它是否以ID3开头

if f.read(3) != "ID3":
    return False
这里它返回false,我想这就是错误所在,因为如果我自己尝试读取标题,它肯定是ID3

>>> f = open("myfile.mp3", "rb")
>>> print(f.read(3))
b'ID3'

但是在0.8版本之前,python3的全面支持是不可能实现的,根据这里提供的版本:

试试这段代码,它对我有用

import eyed3

def show_info():
    audio = eyed3.load("[PATH_TO_MP3]")
    print audio.tag.artist
    print audio.tag.album
    print audio.tag.title

show_info()

对于Python,3件事情已经改变,我的代码在Mac上运行

@yask是正确的,因为您应该检查不存在的值,这是我的示例:

复制、粘贴并根据需要调整路径&可以在文件路径循环中使用

“占位符”
进口稀土
从os导入路径作为ospath
从eyed3导入id3
当前_home=ospath.expanduser(“~”)
文件路径=ospath.join(当前路径),
"音乐",,
“iTunes”,
“iTunes媒体”,
"音乐",,
“史密斯航空公司”,
“大人物”,
“01水上行走。mp3”,
)
def read_id3_艺术家(音频文件):
“”“用于读取MP3元标记的模块。”。
仅接受类似路径的对象。
"""
文件名=音频文件
tag=id3.tag()
解析(文件名)
# =========================================================================
#设置变量
# =========================================================================
艺术家
title=tag.title
track\u path=tag.file\u info.name
# =========================================================================
#检查变量值并对其进行编码,然后替换回刻度
# =========================================================================
如果艺术家不是无:
艺术家编码()
artistz=re.sub(u'',u'',艺术家)
其他:
artistz='未列出'
如果标题不是“无”:
title.encode()
titlez=re.sub(u'`',u''',标题)
其他:
titlez='未列出'
如果轨迹路径不是无:
track_path.encode()
track_pathz=re.sub(u'`',u''',track_path)
其他:
track_pathz=('未列出,您的运气最差,'
“因为这是/不应该是可能的。”)
# =========================================================================
#打印出来
# =========================================================================
尝试:
如果艺术家不是无且标题不是无且轨迹路径不是无:
打印('艺术家:“{}'.格式(artistz))
打印('Track:'{}'.格式(titlez))
打印('路径:“{}'。格式(轨迹路径Z))
例外情况除外,如e:
提高e
读取\u id3\u艺术家(文件路径)
#展示案例:
#艺术家:“航空史密斯”
#曲目:“水上行走”
#路径:“/Users/MyUserName/Music/iTunes/iTunes-Media/Music/Aerosmith/Big-Ones/01 Water-Walk-On-Water.mp3”#noqa

仍然发生错误。。。。回溯(最后一次调用):文件“C:\Python34\testmp3.py”,第1行,在import eyeD3 ImportError中:没有名为“eyeD3”的模块尝试导入eyeD3,所有字母smallAttributeError:“module”对象没有属性“tag”,我已经尝试过了。但还是有错误。。。。。AttributeError:'module'对象没有属性'Tag'我现在也遇到了同样的问题。。。eyed3在最新版本中没有模块标记,audiofile.Tag返回非类型对象。希望有人能解决这个问题。显然,
audiofile.tag
None
…Visual Studio中显示了一些属性。但是当我运行它时,出现了一个错误。编辑需要一些对象,但是得到了
None
-阅读文档,找出原因