Python修改MP3文件的方法
本文實(shí)例講述了Python修改MP3文件的方法。分享給大家供大家參考。具體如下:
用這個(gè)程序修改后的MP3比原來(lái)要小一些了,因?yàn)橐粡垐D片被刪除了,起到了給MP3"瘦身"的作用。在一些mp3中,每個(gè)都有一張400多K的圖片,10幾個(gè)MP3,就相當(dāng)一個(gè)普通MP3文件的大小了。
# -*- coding: cp936 -*-
"""
將MP3文件中的ID3V2.3部分去掉,以便在MP3機(jī)上播放
用法:mp3lcear [源mp3目錄](méi) [生成的mp3目錄](méi)
"""
import sys
import os
import string
import shutil
import struct
import thread
import threading
import time
mp3suffix = 'mp3'
class Process(threading.Thread):
"""
簡(jiǎn)單地在運(yùn)行的過(guò)程中顯示進(jìn)度
"""
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為T(mén)ure,則新建一個(gè)目錄
"""
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文件,并將其創(chuàng)建到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+'文件格式錯(cuò)誤'
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 '創(chuàng)建文件'+destFile+'錯(cuò)誤',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 '修改中出錯(cuò)',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 '開(kāi)始處理 '+filename,
tempfilename = filename.lower()
if(not tempfilename.endswith(mp3suffix)):
print filename+'不是一個(gè)mp3文件\n'
else:
pause.set()
clearMp3(srcPath,destPath)
pause.clear()
print '結(jié)束 \n'
pause.set()
process.running = False
sys.exit(0)
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- Python批量修改文本文件內(nèi)容的方法
- 簡(jiǎn)單文件操作python 修改文件指定行的方法
- python獲得文件創(chuàng)建時(shí)間和修改時(shí)間的方法
- 使用python的chardet庫(kù)獲得文件編碼并修改編碼
- python實(shí)現(xiàn)批量修改文件名代碼
- python調(diào)用java模塊SmartXLS和jpype修改excel文件的方法
- 利用python批量修改word文件名的方法示例
- Python讀寫(xiě)txt文本文件的操作方法全解析
- Python open讀寫(xiě)文件實(shí)現(xiàn)腳本
- python 讀寫(xiě)、創(chuàng)建 文件的方法(必看)
- python讀寫(xiě)二進(jìn)制文件的方法
- Python實(shí)現(xiàn)修改文件內(nèi)容的方法分析
相關(guān)文章
Python 創(chuàng)建TCP服務(wù)器的方法
這篇文章主要介紹了Python 創(chuàng)建TCP服務(wù)器的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
Pandas數(shù)據(jù)分析固定時(shí)間點(diǎn)和時(shí)間差
這篇文章主要介紹了Pandas數(shù)據(jù)分析固定時(shí)間點(diǎn)和時(shí)間差,文章未日澳主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
Python?threading中l(wèi)ock的使用詳解
Lock類(lèi)是threading中用于鎖定當(dāng)前線程的鎖定類(lèi),本文給大家介紹了Python?threading中l(wèi)ock的使用,需要的朋友可以參考下2022-11-11
Python讀取實(shí)時(shí)數(shù)據(jù)流示例
今天小編就為大家分享一篇Python讀取實(shí)時(shí)數(shù)據(jù)流示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Pytorch中實(shí)現(xiàn)只導(dǎo)入部分模型參數(shù)的方式
今天小編就為大家分享一篇Pytorch中實(shí)現(xiàn)只導(dǎo)入部分模型參數(shù)的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
Jetson?NX?配置?pytorch的問(wèn)題及解決方法
這篇文章主要介紹了Jetson?NX?配置?pytorch的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03

