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)