使用ffmpeg对wav文件进行降采样
Feb. 26, 2020, 5:14 p.m.
read: 1830
依赖pysoundfile,ffmpeg
把采样率大于16k的wav文件降采样为16k
速度比使用librosa快很多,适合批量处理的情况
不适合在Windows环境下运行
更快读取sr的优化可以参考这个文章
/articles/100.html
import os, soundfile as sf, shutil
root_dir = '/home/zhangpeng/mydisk'
data_names = ['ffsvc2020/dev', 'ffsvc2020/train', 'nihaomiya_all/train', 'nihaomiya_all/dev']
def get_paths(work_dir, ext='.wav'):
l = []
for parent, dirnames, filenames in os.walk(work_dir):
for filename in filenames:
if filename.lower().endswith(ext.lower()):
l.append(os.path.join(parent, filename))
return l
for data_name in data_names:
data_dir = os.path.join(root_dir, data_name)
dest_dir = data_dir + '_16k'
wav_file_paths = get_paths(data_dir)
for wav_file_path in wav_file_paths:
dest_wav_file_path = wav_file_path.replace(data_dir+'/', dest_dir+'/')
dest_file_dir, _ = os.path.split(dest_wav_file_path)
if not os.path.exists(dest_file_dir):
os.makedirs(dest_file_dir)
sig, sr = sf.read(wav_file_path)
if sr == 16000:
shutil.copy(wav_file_path, dest_wav_file_path)
else:
cmd = 'ffmpeg -i %s -ar 16000 %s > /dev/null 2>&1' % (wav_file_path, dest_wav_file_path)
os.system(cmd)