3

Here is my code

import eyed3

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

print(audiofile.tag.artist)

This is an error

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'

There's attributes shown in Visual Studio. but when i run it.an error occurred

when i write print(audiofile) it works. i don't know why ps. Python 3.4.

Serenity
  • 35,289
  • 20
  • 120
  • 115

5 Answers5

7

Try this Code , it worked for me

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()
Yugansh Tyagi
  • 646
  • 10
  • 24
5

I think the problem lies within the module.

I did some debugging using this code:

from eyed3 import id3

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

In the parse function, the file is opened and then passed to _loadV2Tag(fileobject). Then, the module reads the first few lines of the file header and checks if it begins with ID3.

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

And here it returns false and I think this is where the error lies, because if I try to read the header myself, it is most definitely ID3.

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

But full python3 support is not to be expected until version 0.8 according to https://bitbucket.org/nicfit/eyed3/issues/25/python-3-compatibilty which is available here: https://bitbucket.org/nicfit/eyed3/branch/py3

Matthias Brandt
  • 342
  • 3
  • 11
1

Title and Artists are available through accessor functions of the Tag() return value. The example below shows how to get them using getArtist() and getTitle() methods.

 import eyed3
 tag = eyed3.Tag()
 tag.link("/some/file.mp3")
 print tag.getArtist()
 print tag.getTitle()
nsfyn55
  • 14,875
  • 8
  • 50
  • 77
EvenLisle
  • 4,672
  • 3
  • 24
  • 47
  • error still occured.... Traceback (most recent call last): File "C:\Python34\testmp3.py", line 1, in import eyeD3 ImportError: No module named 'eyeD3' – Chanon Deeprasertkul Apr 17 '15 at 14:39
  • 1
    Try to import eyed3, all letters small – Igor Apr 17 '15 at 14:41
  • 3
    AttributeError: 'module' object has no attribute 'tag' – Chanon Deeprasertkul Apr 17 '15 at 14:42
  • @ChanonDeeprasertkul: It appears that Python 3 uses "eyed3" for the module name, but in Python 2 it's called "eyeD3". However, in both versions the `Tag` class is written with an upper-case `T`. So you probably need to do `import eyed3` `tag = eyed3.Tag()`. The `tag` on the left side of the `=` sign is an instance of the `eyed3.Tag` class, so it's spelled with a lower-case `t`. – PM 2Ring Apr 17 '15 at 15:18
  • 1
    i've tried. But still error ..... AttributeError: 'module' object has no attribute 'Tag' – Chanon Deeprasertkul Apr 17 '15 at 15:47
  • 3
    I am struggling same problem now... eyed3 has no module Tag in latest version, and audiofile.tag returns NoneType object. Hopefully someone solves this problem. – user2738844 Oct 10 '15 at 14:22
1

Try this:

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")
yask
  • 3,918
  • 4
  • 20
  • 29
0

For Python 3 things have changed and my code runs on my Mac.

@yask was correct as you should be checking for non-existent values and this is my example:

Copy and paste and just adjust the path for your needs & can be used in a loop of file paths.

"""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
JayRizzo
  • 3,234
  • 3
  • 33
  • 49