python中mp3转wav---安装、声音截取、音频分布

一、pydub和ffmpeg安装(失败)

1、直接pip安装

pip install pydub
pip install FFmpeg

运行下文程序时,报错:Couldn’t find ffmpeg or avconv - defaulting to ffmpeg, but may not work"
主要原因是没有找到 ffmpeg.exe。pip list查看安装版本为1.4。

二、单独下载ffmped,将bin文件并添加到环境变量(失败)

下载链接:https://ffmpeg.zeranoe.com/builds/
仍然报错:Couldn’t find ffmpeg or avconv - defaulting to ffmpeg, but may not work"

三、使用镜像进行安装(成功)

conda install -c https://conda.anaconda.org/menpo ffmpeg

conda-forge是编译的ffmpeg视频编解码库,它可直接对视频读存取

conda-forge是编译的ffmpeg视频编解码库,它可直接对视频读存取

四、mp3转wav代码

1、将.mp3转化为.wav格式

from scipy.io import wavfile
import wave
import numpy as np
import matplotlib.pyplot as plt
from pydub import AudioSegment
 #定义转化格式函数
def trans_mp3_to_wav(filepath):
    print(filepath)
    song = AudioSegment.from_mp3(filepath)
    song.export("now.wav", format="wav")
trans_mp3_to_wav(source_file_path)

2、录音详情

wave_path = "now.wav"
file = wave.open(wave_path)
# print('---------声音信息------------')
# for item in enumerate(file.getparams()):
#     print(item)
a = file.getparams().nframes  # 帧总数
f = file.getparams().framerate  # 采样频率
sample_time = 1 / f  # 采样点的时间间隔
time = a / f  # 声音信号的长度
sample_frequency, audio_sequence = wavfile.read(wave_path)
# print(audio_sequence)  # 声音信号每一帧的“大小”
x_seq = np.arange(0, time, sample_time)
 
plt.plot(x_seq, audio_sequence, 'blue')
plt.xlabel("time (s)")
plt.show()

python中mp3转wav---安装、声音截取、音频分布_第1张图片
3、截取录音片段

def get_part_wav(main_wav_path, start_time, end_time,part_wav_path):
    start_time = int(start_time)
    end_time = int(end_time)
    sound = AudioSegment.from_mp3(main_wav_path)
    word = sound[start_time:end_time]
    word.export(part_wav_path, format="wav")

main_wav_path = 'index.mp3'
part_wav_path = 'part.wav'
start_time = 155800
end_time = 160020
get_part_wav(main_wav_path, start_time, end_time,part_wav_path)

参考文献:
1、python 安装 ffmpeg包–简便方法
2、python处理wav音频文件:音频信息,读取内容,获取时长,切割音频,pcm与wav互转

你可能感兴趣的:(Python)