Python修改Mp3文件名称
2010-03-26 00:00:00 来源:WEB开发网但是上面这个代码只是针对,歌曲本身记录了mp3名字信息的才能够正确修改名字,但是某些时候文件中并没有记录这些信息,一般的这些数据信息都记录在MP3的的Id3V1数据段中,所以这里再给一个直接写入信息到这个数据段中。代码:
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()
使用方式:
writeMp3Header[SongName] = '测试歌曲名称'
writeMp3Header[SongPeople] = '不得闲'
writeMp3Header[ZhuanJi] = '专辑'
writeMp3Header[Year] = '2009'
writeMp3Header[Bak] = '备注测试'
setMp3Header(r'E:\test.mp3')
更多精彩
赞助商链接