gpt4 book ai didi

Python (Mutagen) - 无法从 MP4/MP3 文件中获取艺术家

转载 作者:太空宇宙 更新时间:2023-11-03 18:33:03 26 4
gpt4 key购买 nike

这是我第一次使用 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'])}

最佳答案

那么,对于您的 MP3 文件,您是否使用实际的 MP3 作为路径

看来您没有,或者至少缺少 id3 标签 header 。但更有可能这不是 MP3 文件,或者它不会在异常中告诉您无法同步到 MPEG 帧

对于你的第二个问题:

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

它们确实不起作用,因为您捕获的是SystemError,这不是诱变剂引发的。如果您想捕获任何异常,您应该使用:

except Exception as err:
print(err)

如果您只想获取与标签相关的错误:

except HeaderNotFoundError as err:
print(err)

HTH

关于Python (Mutagen) - 无法从 MP4/MP3 文件中获取艺术家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22093456/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com