音频文件重采样 - python 实现

在处理音频文件的时候,经常会将原音频进行统一的重采样处理,设置为相同的采样率,本示例,就是将44100采样率的音频,重采样为16000.

安装对应的python 库:librosa 和 soundfile.

python 复制代码
pip install soundfile
pip install librosa

示例中用到的音频文件下载地址:【免费】音频文件重采样-python实现-示例音频资源-CSDN文库

具体代码实现如下:

python 复制代码
# -*- encoding: utf-8 -*-
import librosa
import soundfile as sf

# 音频重采样
def resample_audio(input_path, output_path, new_sample_rate):
    # 读取:音频文件
    signal, sr = librosa.load(input_path, sr=None)
    print("原采样率 sr:{}".format(sr))
    print("目标采样率 sr:{}".format(new_sample_rate))
    # 重新采样
    new_signal = librosa.resample(signal, orig_sr = sr, target_sr = new_sample_rate)
    #保存为新的音频文件
    sf.write(output_path, new_signal, new_sample_rate)
if __name__ == '__main__':
    # input_path = 'test_44100.mp3'
    input_path = 'test_44100.wav'

    new_sample_rate = 16000 # 16000
    output_path = 'out_{}.wav'.format(new_sample_rate)
    print("原文件:{}".format(input_path))
    resample_audio(input_path, output_path, new_sample_rate)
    print('文件已保存到 {}'.format(output_path))

log 如下:

python 复制代码
原文件: test_44100.wav
原采样率 sr: 44100
目标采样率 sr: 16000
文件已保存到 out_16000.wav

​

助力快速掌握数据集的信息和使用方式。

相关推荐
DeepVisionary7 小时前
为拿并网许可,OpenAI在佐治亚新园区同意高峰期断电三成
python·自动化
Martina_03217 小时前
AI生成3D模型一合并网格,材质就串色?用6步检查材质槽、UV与图集重映射
图像处理·人工智能·3d·材质·uv·游戏策划·1024程序员节
蓝鲨硬科技7 小时前
Token之后,物理AI需要什么样的度量衡?
大数据·人工智能
人工智能AI技术7 小时前
Agent架构新思路:多Agent正在过时,单循环Main Loop才是新方向
人工智能
GlobalInfo7 小时前
管道检测机器人行业调查、市场规模、排名分析报告2026-2032
大数据·人工智能·机器人
小郎君6587 小时前
Vue 多标签页的加载态设计:为什么我没有用 Suspense 实现 PageLoading
vue.js·算法
johnsong7 小时前
AI前沿日报 | 2026-11-03 — 基因突变:当AI开始重写神经网络
人工智能·深度学习·神经网络
AICoder码农王7 小时前
AgentScope Java v2 第9课,Harness 总架构:13 个中间件从哪来
人工智能
FPGA小迷弟7 小时前
FPGA 千兆以太网帧格式全解:从 IEEE 802.3 帧结构到 Verilog 收发实现
人工智能·5g·fpga开发·架构·硬件架构