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

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

相关推荐
fanstuck41 分钟前
从知识图谱到精准决策:基于MCP的招投标货物比对溯源系统实践
人工智能·知识图谱
dqsh061 小时前
树莓派5+Ubuntu24.04 LTS串口通信 保姆级教程
人工智能·python·物联网·ubuntu·机器人
Kidddddult1 小时前
力扣刷题Day 43:矩阵置零(73)
算法·leetcode·力扣
打小就很皮...2 小时前
编写大模型Prompt提示词方法
人工智能·语言模型·prompt
Aliano2172 小时前
Prompt(提示词)工程师,“跟AI聊天”
人工智能·prompt
weixin_445238122 小时前
第R8周:RNN实现阿尔兹海默病诊断(pytorch)
人工智能·pytorch·rnn
KingDol_MIni2 小时前
ResNet残差神经网络的模型结构定义(pytorch实现)
人工智能·pytorch·神经网络
sunshineine2 小时前
jupyter notebook运行简单程序
linux·windows·python
方博士AI机器人2 小时前
Python 3.x 内置装饰器 (4) - @dataclass
开发语言·python
万能程序员-传康Kk3 小时前
中国邮政物流管理系统(Django+mysql)
python·mysql·django