The example in this article describes how Python writes data to a MP3 file. Share to everyone for your reference. The specific analysis is as follows:
The correct name of the Mp3 file is corrected by Mp3 the data in the ID3v1 data section, but sometimes the data in the data break is empty, so writing a function that modifies the data of the ID3v1 data segment is also an exercise.
How to use:
writemp3header[Songname] = ' Test song name ' writemp3header[songpeople] = ' no idle ' writemp3header[Zhuanji] = ' album ' writemp3header[Y Ear] = ' writemp3header[Bak] = ' Memo 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]) >: 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 ()
Hopefully this article will help you with Python programming.