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)它的工作。我不知道为什么ps。蟒蛇 3.4。

4

5 回答 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()
于 2017-07-10T10:53:46.267 回答
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提供的版本 0.8 之前,预计不会提供完整的 python3 支持:https ://bitbucket.org/nicfit/ eyed3/分支/py3

于 2015-11-16T14:16:48.650 回答
1

Title 和 Artists 可通过Tag()返回值的访问器函数获得。下面的示例显示了如何使用getArtist()getTitle()方法获取它们。

 import eyed3
 tag = eyed3.Tag()
 tag.link("/some/file.mp3")
 print tag.getArtist()
 print tag.getTitle()
于 2015-04-17T14:37:28.860 回答
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")
于 2015-10-13T11:55:30.590 回答
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
于 2019-09-17T22:19:49.560 回答