How Python modifies the MP3 file

Source: Internet
Author: User
Tags unpack
The example in this article describes how Python modifies the MP3 file. Share to everyone for your reference. Specific as follows:

With this program modified MP3 than the original is smaller, because a picture was deleted, played to MP3 "thin body" role. In some MP3, each has a picture of more than 400 K, 10 a few MP3, quite a common MP3 file size.

#-*-coding:cp936-*-"" "Removes the id3v2.3 portion of the MP3 file for playback on the MP3 machine usage: mp3lcear [source mp3 directory] [generated MP3 directory]" "Import sysimport Osimport Stringimport shutilimport structimport threadimport threadingimport timemp3suffix = ' mp3 ' Class Process (threading. Thread): "" Simply displays the progress "" "Def __init__ (self,msg,sleeptime): threading in the process of running. Thread.__init__ (self) self.msg = msgself.running = Trueself.sleeptime = Sleeptimedef setpause (self,pause): 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 ture, create a new 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+ "not Present") Else:tempdir = os.path.abspAth (argdir) return tempdirdef clearMp3 (srcfile,destfile): "" "Modify the MP3 file and create it to destfile the address specified by" "" Global PROCESSSRCFP = Nonefilesize = 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):p rint 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 = 0DESTFP = Open (DestFile, ' WB ') Srcfp.seek (headsize,1) data= Srcfp.read (1024x768) while (data!= "):d estfp.write (data) Data=srcfp.read (1024x768) Except Exception,e:print ' Create file ' +destfile+ ' Error ', etry:if (destfp! = None):d estfp.closeexcept exception,de:print deelse:print srcfile+ ' No need to modify copy ', try: Shutil.copyfile (srcfile,destfile) except Exception, Ce:print ceexcept exception,oe:print ' Error in modification ', oetry: if (SRCFP! = None): Srcfp.close () except 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):p rint ' start processing ' +filename,tempfilename = Filename.lower () if (not Tempfilename.endswith (mp3suffix)):p rint filename+ ' is not a mp3 file \ n ' else:pause.set () clearMp3 ( Srcpath,destpath) pause.clear () print ' End \ n ' pause.set () process.running = falsesys.exit (0)

Hopefully this article will help you with Python programming.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.