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

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

相关推荐
u***324316 分钟前
使用python进行PostgreSQL 数据库连接
数据库·python·postgresql
mit6.8243 小时前
bfs|栈
算法
青瓷程序设计3 小时前
动物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
tobebetter95273 小时前
How to manage python versions on windows
开发语言·windows·python
F_D_Z3 小时前
数据集相关类代码回顾理解 | sns.distplot\%matplotlib inline\sns.scatterplot
python·深度学习·matplotlib
daidaidaiyu4 小时前
一文入门 LangGraph 开发
python·ai
CoderYanger4 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
金智维科技官方4 小时前
RPA财务机器人为企业高质量发展注入动能
人工智能·机器人·rpa·财务
沫儿笙4 小时前
安川机器人tag焊接怎么节省保护气
人工智能·物联网·机器人
2501_941147424 小时前
人工智能赋能智慧教育互联网应用:智能学习与教育管理优化实践探索》
人工智能