凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

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

2019-08-30 python 994

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

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()



文章评论

0条评论