python录音_python 使用录音功能总结,wav转MP3

在做这个程序的时候,遇到好多坑

1 使用 sounddevice时 python 无法导入  D:\Program Files (x86)\python\Lib\site-packages\_sounddevice_data

报 :Failed to execute script xxx错误

使用 pyinstaller -D xxx.py  进入debug模式查看错误 解决问题

2 ffmpeg 全局变量问题 因为使用python自带的组件 想要打包到自己的程序中 没办法直接设置要执行的文件

最后使用了os.environ["PATH"] = os.environ["PATH"] + ";" + r"D:\Program Files (x86)\avconv\usr\bin"

用这样的方式可以解决问题

3 ffmpeg 在使用的时候 导出的exe文件,只要执行就会弹出cmd窗口 基本上找不到解决的方法

-loglevel quiet 也只是不输出内容

4 最后 使用了avconv 实际上就是命令行执行avconv.exe -loglevel quiet -i C:/Users/Administrator/Desktop/system1.wav -b 320k -vol 256 -ac 1 -y C:/Users/Administrator/Desktop/system1.mp3

这样执行的就很好了

然后整理了一下 放到程序里面去

5 使用 subprocess.Popen执行 效率比ffmpeg 不是快了一点半点wav2mp3_cmd = [avconv_path,'-loglevel','quiet','-i', 'C:/Users/Administrator/Desktop/system1.wav','-b',bitrate,'-vol',str(volume), '-ac', str(channelsCount), '-y', mp3file]

print(" ".join(wav2mp3_cmd))

subprocess.Popen(wav2mp3_cmd,shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

附加:

解决pyinstaller丢失模块的问题自己写了个导入程序import os, shutil

import re

def convert_path(path):

seps = r'\/'

sep_other = seps.replace(os.sep, '')

return path.replace(sep_other, os.sep)

def list_dir(path):

dirs = []

for path, name, filenames in os.walk(path):

print(path)

dirs.append(path)

return dirs

def repairQt5(path):

bin_path = r'/PyQt5/Qt/bin'

dst = convert_path(path + bin_path)

for file in os.listdir(path):

try:

m = re.match("Qt5\w+\.dll", file)

if m:

f = os.path.join(path, file)

d = os.path.join(dst, file)

shutil.move(f, d)

print("move: %s -> %s" % (f, d))

except Exception as e:

print(str(e))

print("#====== 修复 pyinstaller 无法正确导入 Qt5 dll组件问题 ======")

def copyapp(conf_files, path, distpath):

if len(conf_files) > 0:

for file in conf_files:

f = os.path.join(path, file)

d = os.path.join(distpath, file)

if len(file.split(":\\")) > 1:

d = os.path.join(distpath, os.path.basename(file))

if os.path.exists(d) == False:

if os.path.isdir(f):

shutil.copytree(f, d)

else:

shutil.copyfile(f, d)

print("copy: %s -> %s" % (f, d))

print("#====== 导入配置文件 ======")

def main():

path = os.getcwd()

print(path)

distpath = os.path.join(path, "dist")

# 导入 设定的内容

conf_files = [

'trans.png',

'ffmpeg.exe',

'ffprobe.exe',

r'D:\Program Files (x86)\python\Lib\site-packages\_sounddevice_data',

]

for dir in os.listdir(distpath):

dtpath = os.path.join(distpath, dir)

if os.path.isdir(dtpath):

# 处理配置文件

copyapp(conf_files, path, dtpath)

# 修复 pyinstaller 无法正确导入 Qt5 dll组件问题

repairQt5(dtpath)

if __name__ == "__main__":

main()

如果本文对您有所帮助,请支持下本站哦!!!^_^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值