ASR—音频数据断句切割

按语音停顿切分

直接用pydub库,实现拆分的核心就是:
silence_thresh是认定小于-50dBFS以下的为silence,发现小于-50dBFS部分超过 600毫秒,就进行拆分

#!/usr/bin/env python3
# encoding: utf-8
'''
@file: audio_breakage.py
@time: 2020/5/10 0010 15:18
@author: Jack
@contact: [email protected]
'''

from pydub import AudioSegment
from pydub.silence import split_on_silence
import os

init_id = 0
root = r'F:\GitHub\audio_split\data\wav05'
file_lst = []
audiopath_lst = []


## 加载数据
def search_audio(file_dir):
    """
    递归查找音频文件
    :param file_dir:
    :return:
    """
    items = os.listdir(file_dir)
    items = [os.path.join(file_dir, item) for item in items]
    for item in items:
        if os.path.isdir(item):
            search_audio(item)
        else:
            file_lst.append(item)


search_audio(root)

for file in file_lst:
    if len(file.split('.')) == 2:
        audiopath_lst.append(file)


def read_wave(path):
    format_type = path.split(".")[-1]
    if format_type in ["wav", "WAV"]:
        wav_audio = AudioSegment.from_file(path, format="wav")
    elif format_type == "mp3":
        wav_audio = AudioSegment.from_file(path, format="mp3")
    elif format_type == "m4a":
        wav_audio = AudioSegment.from_file(path, format="mp4")

    return wav_audio, format_type


for i, audiopath in enumerate(audiopath_lst):
    audiopath = os.path.join(root, audiopath)
    print(audiopath)

    ## 读入音频

    sound, audiotype = read_wave(audiopath)

    ## 切割
    print('开始切割')
    chunks = split_on_silence(sound, min_silence_len=600, silence_thresh=-50)
    filepath = os.path.split(audiopath)[0]
    chunks_path = filepath + '\\chunks\\'
    if not os.path.exists(chunks_path):
        os.mkdir(chunks_path)

    print('开始保存音频片段')
    for j in range(len(chunks)):
        new = chunks[j]
        save_name = chunks_path + '{}_{}.{}'.format(i + init_id, j, audiotype)
        new.export(save_name, format=audiotype)
        print('{}\t{}\t{}'.format(i + init_id, j, len(new)))

    print('保存完毕')

切割效果如下:
切割前:
在这里插入图片描述
切割后:
ASR—音频数据断句切割_第1张图片

你可能感兴趣的:(语音识别,python)