这些是我对 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 后的应用程序输出:
{'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'])}