获取mp3部分信息的python代码

import os,base64,struct
#这是python代码,用于获取mp3部分信息。
#编写者:SDY

#判断IDV2是否存在
def IDV2_exsist(mp3_file):
    b_data=mp3_file.read(3)
    a_data=b_data.decode('ASCII')
    if a_data=='ID3':
        return True
    else:        
        return False

#获取IDV2大小
def get_IDV_Size(mp3_file):
    mp3_file.seek(6)
    data=mp3_file.read(4)
    int_data=struct.unpack('>BBBB',data)
    all_size=int_data[0]*2*16**5+int_data[1]*4*4*16**2+int_data[2]*8*16+int_data[3]+10
    return all_size

#判断信息是否有价值
def judge_value(mp3_file,N):
    mp3_file.seek(N)
    value=mp3_file.read(4)
    if value==b'\x00\x00\x00\x00':
        return True
    else:
        return False

#获取数据种类
def get_type(mp3_file,N):
    mp3_file.seek(N)
    b_type=mp3_file.read(4)
    a_type=b_type.decode('ASCII')
    return a_type

#获取数据帧大小
def get_size(mp3_file,N):
    mp3_file.seek(N+4)
    b_size=mp3_file.read(4)
    number=struct.unpack('>BBBB',b_size)
    size=number[0]*16**8+number[1]*16**4+number[2]*16**2+number[3]+10
    return size

#获取数据编码方式
def get_code_style(mp3_file,N):
    mp3_file.seek(N+10)
    temp=mp3_file.read(1)
    temp=struct.unpack('>B',temp)
    if temp[0]==0:
        code_style='ISO-8859-1'
    elif temp[0]==1:
        code_style='UTF-16'
    elif temp[0]==2:
        code_style='UTF-16BE'
    elif temp[0]==3:
        code_style='UTF-8'
    return code_style

#获取具体信息
def get_message(mp3_file,code_style,N,size):
    mp3_file.seek(N+11)
    eb_data=mp3_file.read(size-11)
    ea_data=eb_data.decode(code_style)
    return ea_data

#获取mp3信息
def IDV2_GET_MESSAGE(mp3_file,file_name):
    ms_dic={'TIT2':'未知','TPE1':'未知','TALB':'未知','TYER':'未知'}
    global B
    B=0
    lis=['TIT2','TPE1','TALB','TYER']
    N,size=10,0
    if not IDV2_exsist(mp3_file):
        print('error')
        global A
        A=file_name
        B=1
    else:
        all_size=get_IDV_Size(mp3_file)
        while True:
            N=size+N
            if judge_value(mp3_file,N):
                break
            kind=get_type(mp3_file,N)
            size=get_size(mp3_file,N)
            if kind in lis:                
                code_style=get_code_style(mp3_file,N)
                message=get_message(mp3_file,code_style,N,size)
                ms_dic[kind]=message
    return ms_dic

#错误文件处理
def error_delete(error_list1,error_list2,delete_X,path):
    if delete_X=='Y':
        for file in os.listdir(path):
            if file in error_list1:
                file_path=path+'\\'+file
                print(f"delete {file} succeed.")
                os.remove(file_path)
            if file in error_list2:
                file_path=path+'\\'+file
                print(f"{file} delete successfully.")
                os.remove(file_path)

#主函数
print('TLT2:标题,TPE1:作者,TALB:专辑,TYER:年代')
n,m,mn=0,0,0
error_list1=[]
error_list2=[]
#文件路径获取
path='F:\\Temp'
for file in os.listdir(path):
    address=path+'\\'+file 
    mp3_file=open(address,'rb')
    try:        
        dic=IDV2_GET_MESSAGE(mp3_file,file)
    except:
        error_list2.append(file)
    else:
            a1=dic['TIT2']
            a2=dic['TPE1']
            a3=dic['TALB']
            a4=dic['TYER']
            value=a1+','+a2+','+a3+','+a4+','+address
            print(value)
            n+=1
    m+=1
    if B==1:
        error_list1.append(A)
        B=0
        mn+=1
    mp3_file.close()    
print(f'成功录入 {n} 首;共 {m} 首 ')
if error_list1:
    print('以下歌曲不存在IDV2标签:')
    print(error_list1)
if error_list2:
    print('以下歌曲解析失败:')
    print(error_list2)
if error_list1 or error_list2:
    delete_X=input('如果你想删除有问题的歌曲,请输入 Y :')
    error_delete(error_list1,error_list2,delete_X,path)

os.system('pause')

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值