5

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'])}
peperunas
  • 428
  • 1
  • 6
  • 17

2 Answers2

5

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

zmo
  • 24,463
  • 4
  • 54
  • 90
  • Thank you! The path is correct though. I process it earlier in my code. The song can be listened on any player and I can read its tags... [MP3 case]. What about the MP4 songs? – peperunas Feb 28 '14 at 11:15
  • it's likely the value of `artist` in the header exists but is empty, otherwise it should have raised an exception. About the MP3, there's definitely something wrong with mutagen accessing the file. You should try `track.pprint()`, to show if mutagen reads the file successfully and if it has any data. – zmo Feb 28 '14 at 11:18
  • Tried to do that and... it only prints "Trying to print MP3 infos.". No errors, nothing. How can I throw mutagen exceptions? Maybe it isn't a "real" mp3 file? How can I check if it's a real mp3 audio file? Thank you very much for your patience! – peperunas Feb 28 '14 at 11:27
  • you can use a commandline tool such as id3tool or a mp3 player with that file. The path to the file in the variable may not be useable directly by mutagen… You don't need to throw mutagen exceptions, you need to catch them. – zmo Feb 28 '14 at 11:31
  • I did some step further! (I was doing a wrong thing before in my code) Now I can get some infos but not the artist name! The output is in the main question. I tried to use print(track['artist']) but that didn't work. How can I catch mutagen exceptions? – peperunas Feb 28 '14 at 11:37
  • Maybe I did it: instead of using the MP3 class from mutagen.mp3 I tried to use the EasyID3 class. – peperunas Feb 28 '14 at 11:42
  • when you get an exception, you have a string like: `HeaderNotFoundError: can't sync to an MPEG frame`, the name of the exception is the part before `:`. Then you can use that name in the `except` part of the `try/except` construct. (be sure to import it from mutagen before). – zmo Feb 28 '14 at 12:00
  • I tried to import it but I don't know WHAT to import... mutagen.? I can't find it in the documentation either.. – peperunas Feb 28 '14 at 12:18
  • `print( [err_class for err_class in dir(mutagen.mp3) if err_class )` will give you all the elements in the mp3 module, where you'll find the `HeaderNotFoundError`. You'll find as well `InvalidMPEGHeader`. It looks like `mutagen` error declaration is a bit drafty, but you'll find the exception class easily if you look at [the source](http://pydoc.net/Python/mutagen/1.20/mutagen.mp3/). You'll find out that all exception thrown inherits from `mutagen.module.error` which inherits from `IOError` which inherits from `Exception`. So to catch any exception, you can catch on of those. – zmo Feb 28 '14 at 12:39
0

Check if all the audio files are supported and not corrupted using the code written as exception by @zmo. Also check if all audios are in .mp3 format nly. The unsupported MP3 files cannot be accepted by mutagen MP3 function.