Python修改MP3文件的方法

php中文网
发布: 2016-06-10 15:10:32
原创
2073人浏览过

本文实例讲述了python修改mp3文件的方法。分享给大家供大家参考。具体如下:

用这个程序修改后的MP3比原来要小一些了,因为一张图片被删除了,起到了给MP3"瘦身"的作用。在一些mp3中,每个都有一张400多K的图片,10几个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)

登录后复制

希望本文所述对大家的Python程序设计有所帮助。

使用新浪微博账号登录ecshop插件
使用新浪微博账号登录ecshop插件

新浪微博登录ecshop这类的功能就显得很有必要了把login整个文件夹传到服务器上ecshop安装所在的目录,如果路径不对可以会导致应用失败。 需要修改的文件:config.php callback.php可以修改第27行的邮箱域名为你的网站域名。 别的不用改,否则会导致无法使用。

使用新浪微博账号登录ecshop插件 0
查看详情 使用新浪微博账号登录ecshop插件
相关标签:
python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号