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

How to get detail (Title,Artist) from .mp3 files in python using 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)
AttributeError: 'NoneType' object has no attribute 'artist'

Visual Studio中显示了属性。但是当我 运行 it.an 发生错误时

当我写 print(audiofile) 时它起作用了。我不知道为什么 ps。 Python3.4.

标题和艺术家可通过 Tag() return 值的访问器函数获得。下面的示例显示了如何使用 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)

在解析函数中,文件被打开,然后传递给_loadV2Tag(fileobject)。然后,模块读取文件的前几行 header 并检查它是否以 ID3 开头。

if f.read(3) != "ID3":
    return False

这里是 returns false,我认为这就是错误所在,因为如果我尝试自己读取 header,那肯定是 ID3。

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

但根据 https://bitbucket.org/nicfit/eyed3/issues/25/python-3-compatibilty which is available here: https://bitbucket.org/nicfit/eyed3/branch/py3

,在 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 是正确的,因为你应该检查不存在的值,这是我的例子:

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

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