音频文件重采样 - 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

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

相关推荐
wearegogog1237 小时前
基于 MATLAB 的卡尔曼滤波器实现,用于消除噪声并估算信号
前端·算法·matlab
一只小小汤圆7 小时前
几何算法库
算法
Evand J7 小时前
【2026课题推荐】DOA定位——MUSIC算法进行多传感器协同目标定位。附MATLAB例程运行结果
开发语言·算法·matlab
小二·7 小时前
Python Web 开发进阶实战:性能压测与调优 —— Locust + Prometheus + Grafana 构建高并发可观测系统
前端·python·prometheus
leo__5208 小时前
基于MATLAB的交互式多模型跟踪算法(IMM)实现
人工智能·算法·matlab
忆锦紫8 小时前
图像增强算法:Gamma映射算法及MATLAB实现
开发语言·算法·matlab
脑极体8 小时前
云厂商的AI决战
人工智能
t198751288 小时前
基于自适应Chirplet变换的雷达回波微多普勒特征提取
算法
guygg888 小时前
采用PSO算法优化PID参数,通过调用Simulink和PSO使得ITAE标准最小化
算法
njsgcs8 小时前
NVIDIA NitroGen 是强化学习还是llm
人工智能