Python writes data to MP3 files,
This example describes how to write Python data to an MP3 file. Share it with you for your reference. The specific analysis is as follows:
The correct name of the Mp3 file is corrected through the data in the Id3V1 data segment of the Mp3 file. However, sometimes the data in this data segment is empty. Therefore, write a function to modify the data in the Id3V1 data segment, it is also an exercise.
Usage:
WriteMp3Header [SongName] = 'test song name' writeMp3Header [SongPeople] = 'not idle 'writeMp3Header [ZhuanJi] = 'album 'writemp3header [Year] = '20140901' writeMp3Header [Bak] = 'note test 'setmp3header (r'e: \ test.mp3 ')
The Python code is as follows:
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()
I hope this article will help you with Python programming.