pythonmutagen

Python (Mutagen) - Can't get artist from MP4/MP3 file


These are my first experiences with Python and external APIs; I'm trying to get the artist name from a MP3 / MP4 file but I'm not succeeding. In the first case (MP3) I get the following error:

HeaderNotFoundError: can't sync to an MPEG frame

In the latter, my code prints nothing.

These are my TEST functions that process my files:

1) MP3

from mutagen.mp3 import MP3    
def mp3_reader(path):
   track = MP3(path)       
   try:
       print track['artist']
   except SystemError:
       print ("SYSTEM ERROR!")

2) MP4

from mutagen.mp4 import MP4

def mp4_reader(path):

    track = MP4(path)

    try:
        print track.tags['artist']
    except SystemError:
        print ("SYSTEM ERROR!")

Another, Python related question: how do I effectively use the try-catch expressions? I know that those do not work...

Thank you very much!!

EDITED CODE:

def mp3_reader(path):

track = MP3(path)

try:
    print ("Trying to print MP3 infos.")
    track.pprint()
except Exception as err:
    print (err)

APP OUTPUT AFTER A MP3 IS PROCESSED:

{'TMED': TMED(encoding=1, text=[u'CD']), u'TXXX:SCRIPT': TXXX(encoding=1, desc=u'SCRIPT', text=[u'Latn']), u'TXXX:MusicBrainz Album Type': TXXX(encoding=1, desc=u'MusicBrainz Album Type', text=[u'album']), u'TXXX:MusicBrainz Album Artist Id': TXXX(encoding=1, desc=u'MusicBrainz Album Artist Id', text=[u'122d63fc-8671-43e4-9752-34e846d62a9c']), u'TXXX:MusicBrainz Artist Id': TXXX(encoding=1, desc=u'MusicBrainz Artist Id', text=[u'122d63fc-8671-43e4-9752-34e846d62a9c']), u'TXXX:BARCODE': TXXX(encoding=1, desc=u'BARCODE', text=[u'5099964783024']), 'TDOR': TDOR(encoding=0, text=[u'2010']), 'TDRC': TDRC(encoding=0, text=[u'2010-08-27']), 'TSO2': TSO2(encoding=1, text=[u'Perry, Katy']), 'TPE2': TPE2(encoding=1, text=[u'Katy Perry']), 'TPE1': TPE1(encoding=1, text=[u'Katy Perry']), 'TALB': TALB(encoding=1, text=[u'Teenage Dream']), u"COMM:iTunNORM:'eng'": COMM(encoding=0, lang='eng', desc=u'iTunNORM', text=[u' 000016A6 00001768 0000BFFB 0000BE99 00032378 00032378 00009227 000093AF 0001FCAC 00034AC1']), 'TCMP': TCMP(encoding=1, text=[u'1']), u'TXXX:CATALOGNUMBER': TXXX(encoding=1, desc=u'CATALOGNUMBER', text=[u'509996 47830 2 4']), u'UFID:http://musicbrainz.org': UFID(owner=u'http://musicbrainz.org', data='8cf81f4a-05fd-4688-bb8c-eb59df2026a0'), u'TXXX:MusicBrainz Release Group Id': TXXX(encoding=1, desc=u'MusicBrainz Release Group Id', text=[u'e6f683c9-fc85-412c-a352-d6e411fc2603']), 'TSOP': TSOP(encoding=1, text=[u'Perry, Katy']), 'TRCK': TRCK(encoding=0, text=[u'1/12']), u"COMM:iTunPGAP:'eng'": COMM(encoding=0, lang='eng', desc=u'iTunPGAP', text=[u'0/']), 'TIT2': TIT2(encoding=1, text=[u'Teenage Dream']), 'TSRC': TSRC(encoding=1, text=[u'USCA21001255']), 'TCON': TCON(encoding=0, text=[u'Pop']), 'TENC': TENC(encoding=0, text=[u'iTunes 10.0.0.68']), u'TXXX:MusicBrainz Album Id': TXXX(encoding=1, desc=u'MusicBrainz Album Id', text=[u'8551cd49-7da6-3139-809d-e48f6f3453e8']), 'TPUB': TPUB(encoding=1, text=[u'Capitol Records']), 'TPOS': TPOS(encoding=0, text=[u'1/1']), u"COMM:iTunSMPB:'eng'": COMM(encoding=0, lang='eng', desc=u'iTunSMPB', text=[u' 00000000 00000210 00000B41 00000000009943AF 00000000 003792F1 00000000 00000000 00000000 00000000 00000000 00000000']), u'TXXX:MusicBrainz Album Release Country': TXXX(encoding=1, desc=u'MusicBrainz Album Release Country', text=[u'DE']), u'TXXX:MusicBrainz Album Status': TXXX(encoding=1, desc=u'MusicBrainz Album Status', text=[u'official'])}

Solution

  • well, for your MP3 file, did you use an actual MP3 as path?

    It looks like you're not, or at least the id3 tags headers are missing. But more likely this is not a MP3 file, or it wouldn't tell you in the exception can't sync to an MPEG frame.

    For your second question:

    how do I effectively use the try-catch expressions? I know that those do not work...

    they indeed do not work, because you're catching SystemError, which is not thrown by mutagen. If you want to catch any exception you should instead use:

    except Exception as err:
        print(err)
    

    if you want to get only tag related errors:

    except HeaderNotFoundError as err:
        print(err)
    

    HTH