python小應用---修改MP3

轉自:http://agilejava.blogbus.com/logs/1068361.html

上週買了一個MP3播放器,挺好的。昨天下了一些Queen的MP3,拷貝到了MP3裏面,在路上一聽,才發現格式 錯誤。回到家裏到電腦上放,可以的啊。仔細的檢查了一下文件,發現這次下的MP3裏都有一張Queen專緝的圖片,這就導致了我的MP3無法播放這些 MP3了。

今天到公司,正好上網找了一下MP3的格式,順便用python寫了一個程序,修改MP3,把那些信息都刪除,MP3就可以正常播放了,程序見下面:

# -*- coding: cp936 -*-
"""
將MP3文件中的ID3V2.3部分去掉,以便在MP3機上播放
用法:mp3lcear [源mp3目錄] [生成的mp3目錄]
"""
import sys
import os
import string
import shutil
import struct
import thread
import threading
import time

mp3suffix = 'mp3'

class Process(threading.Thread):
"""
簡單地在運行的過程中顯示進度
"""
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=''):
"""
程序的使用方法
"""
print >> sys.stderr, __doc__
if msg:
print >> sys.stderr, msg
sys.exit(code)

def checkDir(argDir,create=False):
"""
檢查目錄是否存在,如果create爲Ture,則新建一個目錄
"""
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,"目錄"+argDir+"不存在")
else:
tempDir = os.path.abspath(argDir)
return tempDir

def clearMp3(srcFile,destFile):
"""
修改mp3文件,並將其創建到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+'文件格式錯誤'
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(1024)
while (data!= ''):
destfp.write(data)
data=srcfp.read(1024)
except Exception,e:
print '創建文件'+destFile+'錯誤',e
try:
if (destfp != None):
destfp.close
except Exception,de:
print de
else:
print srcFile+'不需要修改 拷貝',
try:
shutil.copyfile(srcFile,destFile)
except Exception, ce:
print ce
except Exception,oe:
print '修改中出錯',oe
try:
if (srcfp != None):
srcfp.close()
except 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源目錄',sourceDir
print 'Mp3目的目錄',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 '開始處理 '+filename,
tempfilename = filename.lower()
if(not tempfilename.endswith(mp3suffix)):
print filename+'不是一個mp3文件/n'
else:
pause.set()
clearMp3(srcPath,destPath)
pause.clear()
print '結束 /n'
pause.set()
process.running = False
sys.exit(0)

用這個程序修改後的MP3比原來要小一些了,因爲一張圖片被刪除了,起到了給MP3"瘦身"的作用。在那些mp3中,每個都有一張400多K的圖片,10幾個MP3,就相當一個普通MP3文件的大小了。python寫一些小工具很是方便:)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章