按TextGrid来切割音频

from pydub import AudioSegment
import re
f=open("0.TextGrid","r",encoding='utf-8')
#w=open('label','w')
file_name = "0.wav"
sound = AudioSegment.from_mp3(file_name)
f.readline()
time_start=0
time_end=0
wav_id=0
for line in f:
    line=line.strip()
    if(re.match("xmin = (.*)", line) != None):
        start0bj=re.match("xmin = (.*)", line)
        time_start=float(start0bj.group(1))*1000
    if(re.match("xmax = (.*)", line) != None):
        end0bj=re.match("xmax = (.*)", line)
        time_end=float(end0bj.group(1))*1000
    if(re.match("text = (.*)", line) != None):
        text0bj=re.match("text = (.*)", line)
        text=text0bj.group(1) 
        if(len(text)>2):
            save_name =str(wav_id)+".wav"
            wav_id+=1
            #print(time_start,"-",time_end,":",text,save_name)
            #w.write(text+'('+save_name+")\n")
            word=sound[time_start:time_end]
            word.export(save_name, format="wav")

 

 按顺序切并给音频按0-N.wav这种编名 .  去掉#号就是提取相应wav的label

 

你可能感兴趣的:(音频处理)