MP3文件结构大体分为三部分:TAG_V2(ID3V2),Frame, TAG_V1(ID3V1)
一般情况下, 专辑名是在文件尾部的ID3V1中
取法:
#coding=utf-8
import os
thepath=os.path.join(os.getcwd(),"序章·小桑村之贰 - Visionaries.mp3")
def getID3(filename):
fp = open(filename, 'rb')
fp.seek(-128, 2)
fp.read(3)
title = fp.read(30)
artist = fp.read(30)
album = fp.read(30)
anno = fp.read(4)
comment = fp.read(28)
fp.close()
return {'title':title, 'artist':artist, 'album':album, 'anno':anno,'comment':comment}
print(getID3(thepath))
特殊情况, 有些MP3文件的专辑名在头部的ID3V2中
取法:
#coding=utf-8
import os
import binascii
import re
thepath=os.path.join(os.getcwd(),"序章·小桑村之贰 - Visionaries.mp3")
exampleframeids=['TALB','TBPM','TCOM','TCON','TCOP','TDAT','TDLY','TENC','TEXT','TFLT','TIME','TIT1','TIT2','TIT3','TKEY','TLAN','TLEN','TMED','TOAL','TOFN','TOLY','TOPE','TORY','TOWN','TPE1','TPE2','TPE3','TPE4','TPOS','TPUB','TRCK','TRDA','TRSN','TRSO','TSIZ','TSRC','TSSE','TYER','TXXX']
#---http://id3.org/id3v2.3.0---4. Declared ID3v2 frames---see frame id meaning here
def converttohex(theid):
return str(binascii.hexlify(theid.encode("utf-8")))[2:-1]
def dectostr(thehex):
x2=bytearray.fromhex(thehex)
return x2.decode(encoding='utf-16')
def getID3(filename,*frameids):
fp = open(filename, 'rb')
fp.seek(0, 0)
test1=fp.read(750)
test1=str(binascii.hexlify(test1)) #print(test1)
results1=[]
for i in [converttohex(x) for x in frameids]:
pattern1=i+"(.{8})"
re1=re.search(pattern1, test1) #print(re1.group(),re1.groups()[0])
if re1!=None:
thelen=int(re1.groups()[0],16)
pattern2=i+".{14}"+"(.{"+str(thelen*2-2)+"})"
re2=re.search(pattern2, test1)
results1.append(re2.groups()[0]) #print(re2.group(),re2.groups()[0])
else:
results1.append("") #search fail return ""
return {x:dectostr(y) for x ,y in zip(frameids,results1)}
test=getID3(thepath,*['TALB','TIT2']) #这里TALB就是专辑名
print(test)