如何使用eyed3从python中的.mp3文件获取细节(标题,艺术家)

时间:2015-04-17 14:35:24

标签: 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)
AttributeError: 'NoneType' object has no attribute 'artist'

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

当我写print(audiofile)时,它有效。我不知道为什么 PS。 Python 3.4。

5 个答案:

答案 0 :(得分:4)

我认为问题出在模块内。

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

id_stock_available

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

from eyed3 import id3

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

这里它返回false,我认为这就是错误所在,因为如果我自己尝试读取标题,那么它绝对是ID3。

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

但是根据https://bitbucket.org/nicfit/eyed3/issues/25/python-3-compatibilty的版本0.8,不能期望完整的python3支持,这可以在这里找到:https://bitbucket.org/nicfit/eyed3/branch/py3

答案 1 :(得分:3)

试试此代码,它对我有用

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

答案 2 :(得分:0)

标题和艺术家可通过Tag()返回值的访问者功能获得。以下示例显示了如何使用getArtist()getTitle()方法获取它们。

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

答案 3 :(得分:0)

试试这个:

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

答案 4 :(得分: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