How Python modifies the MP3 file

Source: Internet
Author: User
Tags file size join unpack

The example in this article describes how Python modifies MP3 files. Share to everyone for your reference. Specifically as follows:

Use this program to modify the MP3 is smaller than the original, 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, is quite a common MP3 file size.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132-133 134 #-*-coding:cp936-*-"" To remove the id3v2.3 part of the MP3 file for playback on the MP3 machine: mp3lcear [source mp3 directory] [generated MP3 directory] "" Import sys import OS Impor T string import shutil import struct import thread import threading import Time Mp3suffix = ' mp3 ' Class Process (threading. Thread): "" "" "Simply Displays Progress" "" Def __init__ (self,msg,sleeptime): threading in the process of running. 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 S Elf.msg, Time.sleep (self.sleeptime) def usage (code, msg= ' "):" "" "" "" "" "" "" "" Print >> Sys.stderr, __doc__ if MSG:PR int >> Sys.stderr, msg sys.exit (code) def checkdir (argdir,create=false): "" Check if the directory exists, and if Create is ture, create a new directory "" 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+ "does not exist") 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 Type 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=src Fp.read (1024) while (data!= '): Destfp.write (data) data=srcfp.read (1024) except ' exception,e:print ' Create file ' +destfile+ ' Error ', e try:if (DESTFP!= None): destfp.close except Exception,de:print de else:print srcfile+ ' do not need to modify copies ', Try:shutIl.copyfile (srcfile,destfile) except Exception, ce:print ce except exception,oe:print ' Error in modification ', OE try:if (SRCFP!= None) : Srcfp.close () except Exception,se:print de if __name__ = = "__main__": if (len (SYS.ARGV) <3): Usage (1) Global process S Ourcedir = 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 ' starts processing ' +filename, TempFileName = Filename.lower () if (not Tempfilename.endswith (mp3suffix)): Print filename+ ' is not a MP3 file n ' else:pause.set () clearMp3 ( Srcpath,destpath) pause.clear () print ' End N ' pause.set () process.running = False sys.exit (0)

        wants this article to help you with your Python programming.

Related Article

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.