递归处理文件夹内所有音频的范例

1、Python脚本功能:

另有介绍可以参考:https://rollingstarky.github.io/2018/12/18/processing-audio-with-sox/

该python脚本功能为递归处理文件夹下所有文件的,并递归输出到另一个文件夹,这里是格式转换,用sox把格式同样转换为单通道,8k16bit数据。

#!/usr/bin/python                                                                                                        

import os
import sys
import subprocess
if len(sys.argv) < 3 :
    print("Usage: exe inpath outpath")
    sys.exit()

for fpathe,dirs,fs in os.walk(sys.argv[1]):
    for f in fs:
        wav_ori= os.path.join(fpathe,f)
        wav_out= wav_ori.replace(sys.argv[1], sys.argv[2])

        if not os.path.exists(wav_out.split(f)[0]):
            os.makedirs(wav_out.split(f)[0])

        print(wav_ori)
        print(wav_out)
        subprocess.call(["sox " +  wav_ori + " -r 8k" + " -c 1 " + " -b 16 " +  wav_out], shell=True)

其中查看音频数的格式命令以及执行结果如下:(命令参考 该博客)

# sox -V meian_5874.wav -n
sox:      SoX v14.4.1
sox INFO formats: detected file format type `wav'

Input File     : 'meian_5874.wav'
Channels       : 1
Sample Rate    : 8000
Precision      : 16-bit
Duration       : 00:00:07.42 = 59385 samples ~ 556.734 CDDA sectors
File Size      : 119k
Bit Rate       : 128k
Sample Encoding: 16-bit Signed Integer PCM
Endian Type    : little
Reverse Nibbles: no
Reverse Bits   : no


Output File    : '' (null)
Channels       : 1
Sample Rate    : 8000
Precision      : 16-bit
Duration       : 00:00:07.42 = 59385 samples ~ 556.734 CDDA sectors

sox INFO sox: effects chain: input         8000Hz  1 channels
sox INFO sox: effects chain: output        8000Hz  1 channels
root@VM-0-4-ubuntu:/home/usftp/public/Project/Sunway/test_data

 

你可能感兴趣的:(语音算法)