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

3

这是我的代码

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'

在Visual Studio中显示了属性,但运行时出现错误。

当我写print(audiofile)时,它可以工作。我不知道为什么。 附注:Python 3.4。


2
显然,audiofile.tagNone... - jonrsharpe
在 Visual Studio 中显示了属性,但运行时出现了错误。 - Chanon Deeprasertkul
1
期望得到一些对象,但却收到了None- 阅读文档,找出原因。 - jonrsharpe
5个回答

7

试试这段代码,对我有用

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

5
我认为问题出在这个模块内。
我使用以下代码进行了一些调试:
from eyed3 import id3

tag = id3.Tag()
tag.parse("myfile.mp3")
print(tag.artist)

在解析函数中,文件被打开并传递给_loadV2Tag(fileobject)。然后,该模块读取文件头的前几行,并检查它是否以ID3开头。
if f.read(3) != "ID3":
    return False

这里返回 false,我认为错误就在这里,因为如果我自己尝试读取头部信息,它肯定是 ID3。
>>> f = open("myfile.mp3", "rb")
>>> print(f.read(3))
b'ID3'

“但是,根据https://bitbucket.org/nicfit/eyed3/issues/25/python-3-compatibilty,完全支持Python 3要等到0.8版本,这个版本可以在https://bitbucket.org/nicfit/eyed3/branch/py3上找到。”

1

试试这个:

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

1

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

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

错误仍然存在... Traceback (most recent call last): File "C:\Python34\testmp3.py", line 1, in <module> import eyeD3 ImportError: 没有名为 'eyeD3' 的模块 - Chanon Deeprasertkul
1
请尝试导入eyed3库,所有字母都要小写。 - Igor
3
属性错误:'module'对象没有'tag'属性。 - Chanon Deeprasertkul
@ChanonDeeprasertkul:看起来Python 3使用“eyed3”作为模块名称,但在Python 2中它被称为“eyeD3”。但是,在两个版本中,“Tag”类都是用大写的“T”编写的。因此,您可能需要执行import eyed3``tag = eyed3.Tag()。等号左侧的“tag”是“eyed3.Tag”类的一个实例,因此拼写为小写的“t”。 - PM 2Ring
1
我已经尝试过了。但仍然出现错误..... AttributeError: 'module'对象没有属性'Tag'。 - Chanon Deeprasertkul
3
我现在也遇到了同样的问题……最新版本的eyed3没有Tag模块,而audiofile.tag返回的是NoneType对象。希望有人能解决这个问题。 - user2738844

0

针对Python 3,有些事情发生了变化,但我的代码可以在Mac上运行。

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

复制并粘贴,只需调整路径以满足您的需求,并可在文件路径循环中使用。

"""PlaceHolder."""
import re

from os import path as ospath

from eyed3 import id3

current_home = ospath.expanduser('~')
file_path = ospath.join(current_home,
                        'Music',
                        'iTunes',
                        'iTunes Media',
                        'Music',
                        'Aerosmith',
                        'Big Ones',
                        '01 Walk On Water.mp3',
                        )


def read_id3_artist(audio_file):
    """Module to read MP3 Meta Tags.

    Accepts Path like object only.
    """
    filename = audio_file
    tag = id3.Tag()
    tag.parse(filename)
    # =========================================================================
    # Set Variables
    # =========================================================================
    artist = tag.artist
    title = tag.title
    track_path = tag.file_info.name
    # =========================================================================
    # Check Variables Values & Encode Them and substitute back-ticks
    # =========================================================================
    if artist is not None:
        artist.encode()
        artistz = re.sub(u'`', u"'", artist)
    else:
        artistz = 'Not Listed'
    if title is not None:
        title.encode()
        titlez = re.sub(u'`', u"'", title)
    else:
        titlez = 'Not Listed'
    if track_path is not None:
        track_path.encode()
        track_pathz = re.sub(u'`', u"'", track_path)
    else:
        track_pathz = ('Not Listed, and you have an the worst luck, '
                       'because this is/should not possible.')
    # =========================================================================
    # print them out
    # =========================================================================
    try:
        if artist is not None and title is not None and track_path is not None:
            print('Artist: "{}"'.format(artistz))
            print('Track : "{}"'.format(titlez))
            print('Path  : "{}"'.format(track_pathz))
    except Exception as e:
        raise e


read_id3_artist(file_path)

# Show Case:
# Artist: "Aerosmith"
# Track : "Walk On Water"
# Path  : "/Users/MyUserName/Music/iTunes/iTunes Media/Music/Aerosmith/Big Ones/01 Walk On Water.mp3"  # noqa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接