Python: how to modify MP3 files,
This example describes how to modify an MP3 file in Python. Share it with you for your reference. The details are as follows:
The MP3 File Modified by this program is smaller than the original one. Because an image is deleted, it plays a role of "slimming" the MP3 file. In some mp3 files, each of which has an image of more than 400 K and a dozen MP3 files is equivalent to the size of an ordinary MP3 file.
#-*-Coding: cp936-*-"Remove ID3V2.3 from the MP3 file for playing on the MP3 machine: mp3lcear [Source mp3 Directory] [generated mp3 Directory] "import sysimport osimport stringimport shutilimport structimport threadimport threadingimport timemp3suffix = 'mp3' class Process (threading. thread): "simple display progress during running" def _ init _ (self, msg, sleepTime): threading. thread. _ init _ (self) self. msg = msgself. running = Trueself. sleepTime = sleepTimedef setPause (self, pau Se): self. pause = pausedef setRunning (self, running): self. running = runningdef run (self): while (self. running): self. pause. wait () print self. msg, time. sleep (self. sleepTime) def usage (code, msg = ''):" program usage "" print> sys. stderr, _ doc _ if msg: print> sys. stderr, msgsys. exit (code) def checkDir (argDir, create = False): "check whether the directory exists. If create is true, create a directory "tempDir = Noneif (not OS. path. isdir (argDir): currentDir = OS. path. abspath (OS. curdir) tempDir = OS. path. join (currentDir, argDir) if (not OS. path. isdir (tempDir) and create): OS. mkdir (tempDir) else: usage (1, "directory" + argDir + "nonexistent") else: tempDir = OS. path. abspath (argDir) return tempDirdef clearMp3 (srcFile, destFile): "Modify an mp3 file and create it to the address specified by destFile" "global processsrcfp = Nonefilesize = OS. path. getsize (srcFile) try: srcfp = open (srcFile, 'rb') head = srcfp. read (3) if (head = 'id 3 '): srcfp. seek (3, 1) size = srcfp. read (4) if (not len (size) = 4): print srcFile + 'file format error 'else: size0 = struct. unpack ('B', size [0]) [0] size1 = struct. unpack ('B', size [1]) [0] size2 = struct. unpack ('B', size [2]) [0] size3 = struct. unpack ('B', size [3]) [0] headSize = (size0 & 0x7f) <21) | (size1 & 0x7f) <14) | (size2 & 0x7f) <7) | (size3 & 0x7f) filesize = filesize-headSizedestfp = Nonetry: dataLen = 0 destfp = open (destFil E, 'wb ') srcfp. seek (headSize, 1) data = srcfp. read (1024) while (data! = ''): Destfp. write (data) data = srcfp. read (1024) failed t Exception, e: print 'create file' + destFile + 'error', etry: if (destfp! = None): destfp. closespont Exception, de: print deelse: print srcFile + 'do not need to modify copy', try: shutil. copyfile (srcFile, destFile) failed t Exception, ce: print CET t Exception, oe: print 'modification error ', oetry: if (srcfp! = None): srcfp. close () failed t Exception, se: print deif _ name _ = "_ main _": if (len (sys. argv) <3): usage (1) global processsourceDir = checkDir (sys. argv [1]) destDir = checkDir (sys. argv [2], True) print 'mp3 source directory', sourceDirprint 'mp3 destination directory', destDirprocess = Process ('... ', 1) pause = threading. event () process. setPause (pause) process. start () for filename in OS. listdir (sourceDir): srcPath = OS. path. join (sourceDir, filename) destPath = OS. path. join (destDir, filename) if OS. path. isfile (srcPath): print 'start to Process '+ filename, tempfilename = filename. lower () if (not tempfilename. endswith (mp3suffix): print filename + 'is not an mp3 file \ n' else: pause. set () clearMp3 (srcPath, destPath) pause. clear () print 'End \ n' pause. set () process. running = Falsesys. exit (0)
I hope this article will help you with Python programming.