【发布时间】:2019-01-20 09:40:32
【问题描述】:
我正在从 mp3 语音文件中提取 MFCC 功能,但我确实希望保持源文件不可更改且不添加任何新文件。我的处理包括以下步骤:
- 使用
pydub加载.mp3文件,消除静音,生成.wav数据 - 使用
scipy.io.wavfile.read()读取音频数据和速率 - 使用
python_speech_features提取特征
但是,eliminate_silence() 返回一个AudioSegment对象,而scipy.io.wavfile.read() 接受一个.wav 文件名,因此我不得不将数据临时保存/导出为波形以确保两者之间的转换。这一步是内存和耗时的,所以我的问题是:如何避免导出波形文件步骤?还是有解决方法?
这是我的代码。
import os
from pydub import AudioSegment
from scipy.io.wavfile import read
from sklearn import preprocessing
from python_speech_features import mfcc
from pydub.silence import split_on_silence
def eliminate_silence(input_path):
""" Eliminate silent chunks from original call recording """
# Import input wave file
sound = AudioSegment.from_mp3(input_path)
chunks = split_on_silence(sound,
# split on silences longer than 1000ms (1 sec)
min_silence_len=500,
# anything under -16 dBFS is considered silence
silence_thresh=-30,
# keep 200 ms of leading/trailing silence
keep_silence=100)
output_chunks = AudioSegment.empty()
for chunk in chunks: output_chunks += chunk
return output_chunks
silence_clear_data = eliminate_silence("file.mp3")
silence_clear_data.export("temp.wav", format="wav")
rate, audio = read("temp.wav")
os.remove("temp.wav")
# Extract MFCCs
mfcc_feature = mfcc(audio, rate, winlen = 0.025, winstep = 0.01, numcep = 15,
nfilt = 35, nfft = 512, appendEnergy = True)
mfcc_feature = preprocessing.scale(mfcc_feature)
【问题讨论】:
标签: python scipy scikit-learn mfcc pydub