使用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)




python 读取wav的采样率

使用标准库读取# librosa import librosa y, sr = librosa(filename, sr=None)。 # pysoundfile import soundfile…

python 读写 pickle文件

文章标题:python 读写 pickle文件文章内容:以python3为例注意:python3 与 python2 默认生成的pickle文件不通用,你得加上参数, 即协议版本, 以下样例不加协议…

此站点由 ASP.NETIIS 驱动 | © 2018-2023 hupeng.me. All Rights Reserved.