Python 修改MP3
用这个程序修改后的MP3比原来要小一些了,因为一张图片被删除了,起到了给MP3"瘦身"的作用。在一些mp3中,每个都有一张400多K的图片,10几个MP3,就相当一个普通MP3文件的大小了。
[代码] [Python]代码
017 |
class Process(threading.Thread): |
021 |
def __init__( self ,msg,sleepTime): |
022 |
threading.Thread.__init__( self ) |
025 |
self .sleepTime = sleepTime |
026 |
def setPause( self ,pause): |
028 |
def setRunning( self ,running): |
029 |
self .running = running |
034 |
time.sleep( self .sleepTime) |
036 |
def usage(code, msg = ''): |
040 |
print >> sys.stderr, __doc__ |
042 |
print >> sys.stderr, msg |
045 |
def checkDir(argDir,create = False ): |
050 |
if ( not os.path.isdir(argDir)): |
051 |
currentDir = os.path.abspath(os.curdir) |
052 |
tempDir = os.path.join(currentDir,argDir) |
053 |
if ( not os.path.isdir(tempDir) and create): |
056 |
usage( 1 , "目录" + argDir + "不存在" ) |
058 |
tempDir = os.path.abspath(argDir) |
061 |
def clearMp3(srcFile,destFile): |
067 |
filesize = os.path.getsize(srcFile) |
069 |
srcfp = open (srcFile, 'rb' ) |
074 |
if ( not len (size) = = 4 ): |
075 |
print srcFile + '文件格式错误' |
077 |
size0 = struct.unpack( 'b' ,size[ 0 ])[ 0 ] |
078 |
size1 = struct.unpack( 'b' ,size[ 1 ])[ 0 ] |
079 |
size2 = struct.unpack( 'b' ,size[ 2 ])[ 0 ] |
080 |
size3 = struct.unpack( 'b' ,size[ 3 ])[ 0 ] |
081 |
headSize = (((size0& 0x7f )<< 21 ) | ((size1& 0x7f )<< 14 ) | ((size2& 0x7f )<< 7 ) | (size3& 0x7f )) |
082 |
filesize = filesize - headSize |
086 |
destfp = open (destFile, 'wb' ) |
087 |
srcfp.seek(headSize, 1 ) |
088 |
data = srcfp.read( 1024 ) |
091 |
data = srcfp.read( 1024 ) |
093 |
print '创建文件' + destFile + '错误' ,e |
100 |
print srcFile + '不需要修改 拷贝' , |
102 |
shutil.copyfile(srcFile,destFile) |
103 |
except Exception, ce: |
115 |
if __name__ = = "__main__" : |
120 |
sourceDir = checkDir(sys.argv[ 1 ]) |
121 |
destDir = checkDir(sys.argv[ 2 ], True ) |
123 |
print 'Mp3源目录' ,sourceDir |
124 |
print 'Mp3目的目录' ,destDir |
126 |
process = Process( '...' , 1 ) |
127 |
pause = threading.Event() |
128 |
process.setPause(pause) |
132 |
for filename in os.listdir(sourceDir): |
133 |
srcPath = os.path.join(sourceDir, filename) |
134 |
destPath = os.path.join(destDir, filename) |
135 |
if os.path.isfile(srcPath): |
136 |
print '开始处理 ' + filename, |
137 |
tempfilename = filename.lower() |
138 |
if ( not tempfilename.endswith(mp3suffix)): |
139 |
print filename + '不是一个mp3文件\n' |
142 |
clearMp3(srcPath,destPath) |
146 |
process.running = False |
|