python音频降采样
时间: 2023-09-12 19:09:09 浏览: 71
要在Python中进行音频降采样,您可以使用一些常用的音频处理库,例如`librosa`或`pydub`。下面是使用这些库进行音频降采样的示例代码:
使用librosa库:
```python
import librosa
# 读取音频文件
audio_file = "path/to/audio.wav"
audio, sr = librosa.load(audio_file)
# 设置目标采样率
target_sr = 16000
# 对音频进行降采样
audio_resampled = librosa.resample(audio, sr, target_sr)
# 保存降采样后的音频
output_file = "path/to/output.wav"
librosa.output.write_wav(output_file, audio_resampled, target_sr)
```
使用pydub库:
```python
from pydub import AudioSegment
# 读取音频文件
audio_file = "path/to/audio.wav"
audio = AudioSegment.from_file(audio_file)
# 设置目标采样率
target_sr = 16000
# 对音频进行降采样
audio_resampled = audio.set_frame_rate(target_sr)
# 保存降采样后的音频
output_file = "path/to/output.wav"
audio_resampled.export(output_file, format="wav")
```
以上代码示例将音频文件降采样到目标采样率16kHz,并将结果保存为新的音频文件。您可以根据实际需求调整目标采样率和文件路径。