python实现语音文件的格式转换

python调用 ffmpeg 实现语音文件的格式转换,需要安装ffmpeg

# -*- coding: utf-8 -*-
# @Time    : 2021/11/22 18:31
# @Author  : hjcui
# @Site    : 
# @File    : multi_thread_exec.py
# @Software: PyCharm

import os,time
from multiprocessing import Process
import itertools

def exec_func(file_list,src,dest):
    #print("%s %s \n" %(src, dest))
    for i in file_list:
        src_file = os.path.join(src,i)
        if Get_Obj_attr(src) == 'D':
            dst_dir = dest + '\\' + i.split('\\')[-2]
            if not os.path.exists(dst_dir):
                os.makedirs(dst_dir)
            dst_file = os.path.join(dst_dir,os.path.basename(i)).split('.')[0] + '.wav'
        if Get_Obj_attr(src) == 'F':
            dst_file = os.path.join(dest,os.path.basename(i)).split('.')[0] + '.wav'
        print('Convert %s to %s' % (src_file, dst_file))
        cmd = 'ffmpeg.exe -i %s -vn -ar 8000 -ac 1 -ab 128k -y -f wav -loglevel 0 %s' % (i, dst_file)
        os.system(cmd)
        #os.remove(src_file)
        # count += 1
        
#判断源文件夹下面的对象属性
def Get_Obj_attr(src):
    for i in os.listdir(src):
        full_path = os.path.join(src,i)
        if os.path.isdir(full_path):
            return 'D'
        if os.path.isfile(full_path):
            return 'F'

if __name__ == '__main__':
    src = r'D:\解码工具\src'
    dst = r'D:\解码工具\dst'
    threadNum = 20
    files = []
    for root, dirs, fileS in os.walk(src):
        for file in fileS:
            files.append(os.path.join(root,file))
    file_size = len(files)
    loc = int(file_size/threadNum)

    start = time.time()

    for i in range(threadNum):
        if i !=threadNum-1:
            p = Process(target=exec_func,args=(files[i*loc:loc + i*loc],src,dst,))
            p.start()
        else:
            p = Process(target=exec_func,args=(files[i*loc:file_size],src,dst,))
            p.start()
    p.join()
    end = time.time()
    print('run time: ',end-start)

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值