这是我第一次使用Python和外部API;我试图从MP3 / MP4文件中获取艺术家名称,但我没有成功。 在第一种情况(MP3)中,我收到以下错误:
HeaderNotFoundError: can't sync to an MPEG frame
在后者中,我的代码什么都不打印。
这些是处理我的文件的 TEST 函数:
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!")
另一个与Python相关的问题:如何有效地使用try-catch表达式?我知道那些不起作用......
非常感谢!!
编辑代码:
def mp3_reader(path):
track = MP3(path)
try:
print ("Trying to print MP3 infos.")
track.pprint()
except Exception as err:
print (err)
MP3处理后的APP输出:
{'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'])}
答案 0 :(得分:1)
嗯,对于你的MP3文件,你是否使用实际的MP3作为path
?
看起来你不是,或者至少缺少id3标签。但更有可能这不是MP3文件,或者它不会在例外can't sync to an MPEG frame
中告诉你。
关于你的第二个问题:
如何有效地使用try-catch表达式?我知道那些不起作用......
它们确实不起作用,因为你正在捕捉SystemError
,这不是诱变剂引发的。如果你想要捕获任何异常,你应该使用:
except Exception as err:
print(err)
如果您只想获得与标签相关的错误:
except HeaderNotFoundError as err:
print(err)
HTH