python操作wav文件有很多库:wave scipy.io.wavfile pydub等。
wav文件截取我用的是pydub库。
from pydub import AudioSegment
filePath = '.../'
# 操作函数
def get_wav_make(dataDir):
sound= AudioSegment.from_wav(dataDir)
duration = sound.duration_seconds * 1000 # 音频时长(ms)
begin = 0
end = int(duration/2)
cut_wav = sound[begin:end] #以毫秒为单位截取[begin, end]区间的音频
cut_wav.export(filePath+ 'test.wav', format='wav') #存储新的wav文件
ps: 获取wav文件时长,可用wave包操作,上代码:
import wave
f = wave.open(data_dir+ pdwav.iloc[i]['SID']+'.wav')
rate = f.getframerate()
frames = f.getnframes()
duration = frames/float(rate) #单位为s