import os
import librosa
import soundfile as sf
def resample4wavs(frompath, topath, resamplerate):
'''
:param frompath: 源文件所在目录
:param topath: 重置采样率文件存放目录
源文件目录和重置采样率目录可以是一个目录但是会覆盖原来的文件
:param resamplerate: 重置采样率
:return:
'''
fs = os.listdir(frompath)
for f in fs:
try:
fromfile = frompath + f
print(fromfile)
tofile = topath + f
y, sr = librosa.load(fromfile)
to_y = librosa.resample(y, orig_sr=sr, target_sr=resamplerate)
sf.write(tofile, to_y, resamplerate)
except Exception as e:
print('Error:', e)
if __name__ == '__main__':
# 需要重置采样率的文件所在的文件夹
path_1 = r'wav/'
# 重置采样率后的文件所在的文件夹
path_2 = r'wav2/'
# 48000是重置后的采样率,这些都按照自己需要修改
resample4wavs(path_1, path_2, 16000)
06-09
906
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)