#-*-Coding: cp936 -*- """ Remove ID3V2.3 from the MP3 file for playing on the MP3 host. Usage: mp3lcear [Source mp3 Directory] [generated mp3 Directory] """ Import sys Import OS Import string Import shutil Import struct Import thread Import threading Import time Mp3suffix = 'mp3' Class Process (threading. Thread ): """ Simply display the progress during running """ Def _ init _ (self, msg, sleepTime ): Threading. Thread. _ init _ (self) Self. msg = msg Self. running = True Self. sleepTime = sleepTime Def setPause (self, pause ): Self. pause = pause Def setRunning (self, running ): Self. running = running Def run (self ): While (self. running ): Self. pause. wait () Print self. msg, Time. sleep (self. sleepTime) Def usage (code, msg = ''): """ Procedure """ Print> sys. stderr, _ doc __ If msg: Print> sys. stderr, msg Sys. exit (code) Def checkDir (argDir, create = False ): """ Check whether the directory exists. If create is true, a new directory is created. """ TempDir = None If (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 tempDir Def clearMp3 (srcFile, destFile ): """ Modify the mp3 file and create it to the address specified by destFile. """ Global process Srcfp = None Filesize = OS. path. getsize (srcFile) Try: Srcfp = open (srcFile, 'rb ') Head = srcfp. read (3) If (head = 'id3 '): 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-headSize Destfp = None Try: DataLen = 0 Destfp = open (destFile, 'wb ') Srcfp. seek (headSize, 1) Data = srcfp. read (1, 1024) While (data! = ''): Destfp. write (data) Data = srcfp. read (1, 1024) Except t Exception, e: Print 'create file' + destFile + 'error', e Try: If (destfp! = None ): Destfp. close Except t Exception, de: Print de Else: Print srcFile + 'do not need to modify copy ', Try: Shutil. copyfile (srcFile, destFile) Except t Exception, ce: Print ce Failed t Exception, oe: Print 'modification error ', oe Try: If (srcfp! = None ): Srcfp. close () Failed t Exception, se: Print de If _ name _ = "_ main __": If (len (sys. argv) <3 ): Usage (1) Global process SourceDir = checkDir (sys. argv [1]) DestDir = checkDir (sys. argv [2], True) Print 'mp3 source directory', sourceDir Print 'mp3 destination directory', destDir Process = 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 processing '+ filename, Tempfilename = filename. lower () If (not tempfilename. endswith (mp3suffix )): Print filename + 'not an mp3 file \ N' Else: Pause. set () ClearMp3 (srcPath, destPath) Pause. clear () Print 'end \ N' Pause. set () Process. running = False Sys. exit (0) |