网站开发有哪些常用工具,用云怎么做网站,网站建设如何增加流量,医学ppt模板免费下载本文实例讲述了Python修改MP3文件的方法。分享给大家供大家参考。具体如下#xff1a;用这个程序修改后的MP3比原来要小一些了#xff0c;因为一张图片被删除了#xff0c;起到了给MP3瘦身的作用。在一些mp3中#xff0c;每个都有一张400多K的图片#xff0c;…本文实例讲述了Python修改MP3文件的方法。分享给大家供大家参考。具体如下用这个程序修改后的MP3比原来要小一些了因为一张图片被删除了起到了给MP3瘦身的作用。在一些mp3中每个都有一张400多K的图片10几个MP3就相当一个普通MP3文件的大小了。# -*- coding: cp936 -*-将MP3文件中的ID3V2.3部分去掉,以便在MP3机上播放用法mp3lcear [源mp3目录] [生成的mp3目录]import sysimport osimport stringimport shutilimport structimport threadimport threadingimport timemp3suffix mp3class Process(threading.Thread):简单地在运行的过程中显示进度def __init__(self,msg,sleepTime):threading.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):程序的使用方法print sys.stderr, __doc__if msg:print sys.stderr, msgsys.exit(code)def checkDir(argDir,createFalse):检查目录是否存在,如果create为Ture,则新建一个目录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,目录argDir不存在)else:tempDir os.path.abspath(argDir)return tempDirdef clearMp3(srcFile,destFile):修改mp3文件并将其创建到destFile所指定的地址global processsrcfp Nonefilesize os.path.getsize(srcFile)try:srcfp open(srcFile,rb)head srcfp.read(3)if(headID3):srcfp.seek(3,1)size srcfp.read(4)if(not len(size)4):print srcFile文件格式错误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 (((size00x7f)21) | ((size10x7f)14) | ((size20x7f)7) | (size30x7f))filesize filesize - headSizedestfp Nonetry:dataLen 0destfp open(destFile,wb)srcfp.seek(headSize,1)datasrcfp.read(1024)while (data! ):destfp.write(data)datasrcfp.read(1024)except Exception,e:print 创建文件destFile错误,etry:if (destfp ! None):destfp.closeexcept Exception,de:print deelse:print srcFile不需要修改 拷贝,try:shutil.copyfile(srcFile,destFile)except Exception, ce:print ceexcept Exception,oe:print 修改中出错,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源目录,sourceDirprint Mp3目的目录,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 开始处理 filename,tempfilename filename.lower()if(not tempfilename.endswith(mp3suffix)):print filename不是一个mp3文件\nelse:pause.set()clearMp3(srcPath,destPath)pause.clear()print 结束 \npause.set()process.running Falsesys.exit(0)希望本文所述对大家的Python程序设计有所帮助。本文标题: Python修改MP3文件的方法本文地址: http://www.cppcns.com/jiaoben/python/126497.html