Python 写入数据到MP3文件中,pythonmp3,昨天说了,通过Mp3的I


昨天说了,通过Mp3的Id3V1数据段的数据来修正Mp3文件的正确名字,但是,有时候这个数据断中的数据是空的,所以今天再写一个修改Id3V1数据段的数据的函数,同样是练习。使用方法:

writeMp3Header[ SongName] = '测试歌曲名称'

writeMp3Header[ SongPeople] = '不得闲'

writeMp3Header[ ZhuanJi] = '专辑'

writeMp3Header[ Year] = '2009'

writeMp3Header[ Bak] = '备注测试'

setMp3Header(r'E:\test.mp3')

writeMp3Header = {     "SongName":"",     "SongPeople":"",     "ZhuanJi":"",     "Year":"",     "Bak":""     } def setMp3Header(mp3file):     mp3Id3V1 = {                 "SongName":-125,         "SongPeople":-95,         "ZhuanJi":-65,         "Year":-35,         "Bak":-31         }     tags = ['SongName','SongPeople','ZhuanJi','Bak']     f = open(mp3file,'r+')     try:         f.seek(-128,2)         try:             tempstr = f.read(3)             if tempstr == 'TAG':                 for tag,startPos in mp3Id3V1.items():                     if writeMp3Header[tag] != '':                         f.seek(startPos,2)                         if tag in tags:                             if len(writeMp3Header[tag]) > 30:                                  f.write(writeMp3Header[tag][:30])                             else:                                 f.write(writeMp3Header[tag])                         print startPos,tag,writeMp3Header[tag]             else:                 print 'is not a mp3file'         except IOError:             print 'read error'     finally:         f.close()#该片段来自于http://byrx.net

评论关闭