准备:

python2.7/3.+   webrtcvad

import collections
import contextlib
import sys
import wave
import os
import webrtcvaddef read_wave(path):"""Reads a .wav file.Takes the path, and returns (PCM audio data, sample rate)."""with contextlib.closing(wave.open(path, 'rb')) as wf:num_channels = wf.getnchannels()assert num_channels == 1sample_width = wf.getsampwidth()assert sample_width == 2sample_rate = wf.getframerate()assert sample_rate in (8000, 16000, 32000)pcm_data = wf.readframes(wf.getnframes())return pcm_data, sample_ratedef write_wave(path, audio, sample_rate):"""Writes a .wav file.Takes path, PCM audio data, and sample rate."""with contextlib.closing(wave.open(path, 'wb')) as wf:wf.setnchannels(1)wf.setsampwidth(2)wf.setframerate(sample_rate)wf.writeframes(audio)class Frame(object):"""Represents a "frame" of audio data."""def __init__(self, bytes, timestamp, duration):self.bytes = bytesself.timestamp = timestampself.duration = durationdef frame_generator(frame_duration_ms, audio, sample_rate):"""Generates audio frames from PCM audio data.Takes the desired frame duration in milliseconds, the PCM data, andthe sample rate.Yields Frames of the requested duration."""n = int(sample_rate * (frame_duration_ms / 1000.0) * 2)offset = 0timestamp = 0.0duration = (float(n) / sample_rate) / 2.0while offset + n < len(audio):yield Frame(audio[offset:offset + n], timestamp, duration)timestamp += durationoffset += ndef vad_collector(sample_rate, frame_duration_ms,padding_duration_ms, vad, frames):"""Filters out non-voiced audio frames.Given a webrtcvad.Vad and a source of audio frames, yields onlythe voiced audio.Uses a padded, sliding window algorithm over the audio frames.When more than 90% of the frames in the window are voiced (asreported by the VAD), the collector triggers and begins yieldingaudio frames. Then the collector waits until 90% of the frames inthe window are unvoiced to detrigger.The window is padded at the front and back to provide a smallamount of silence or the beginnings/endings of speech around thevoiced frames.Arguments:sample_rate - The audio sample rate, in Hz.frame_duration_ms - The frame duration in milliseconds.padding_duration_ms - The amount to pad the window, in milliseconds.vad - An instance of webrtcvad.Vad.frames - a source of audio frames (sequence or generator).Returns: A generator that yields PCM audio data."""num_padding_frames = int(padding_duration_ms / frame_duration_ms)# We use a deque for our sliding window/ring buffer.ring_buffer = collections.deque(maxlen=num_padding_frames)# We have two states: TRIGGERED and NOTTRIGGERED. We start in the# NOTTRIGGERED state.triggered = Falsevoiced_frames = []for frame in frames:is_speech = vad.is_speech(frame.bytes, sample_rate)sys.stdout.write('1' if is_speech else '0')if not triggered:ring_buffer.append((frame, is_speech))num_voiced = len([f for f, speech in ring_buffer if speech])# If we're NOTTRIGGERED and more than 90% of the frames in# the ring buffer are voiced frames, then enter the# TRIGGERED state.if num_voiced > 0.9 * ring_buffer.maxlen:triggered = Truesys.stdout.write('+(%s)' % (ring_buffer[0][0].timestamp,))# We want to yield all the audio we see from now until# we are NOTTRIGGERED, but we have to start with the# audio that's already in the ring buffer.for f, s in ring_buffer:voiced_frames.append(f)ring_buffer.clear()else:# We're in the TRIGGERED state, so collect the audio data# and add it to the ring buffer.voiced_frames.append(frame)ring_buffer.append((frame, is_speech))num_unvoiced = len([f for f, speech in ring_buffer if not speech])# If more than 90% of the frames in the ring buffer are# unvoiced, then enter NOTTRIGGERED and yield whatever# audio we've collected.if num_unvoiced > 0.9 * ring_buffer.maxlen:sys.stdout.write('-(%s)' % (frame.timestamp + frame.duration))triggered = Falseyield b''.join([f.bytes for f in voiced_frames])ring_buffer.clear()voiced_frames = []if triggered:sys.stdout.write('-(%s)' % (frame.timestamp + frame.duration))sys.stdout.write('\n')# If we have any leftover voiced audio when we run out of input,# yield it.if voiced_frames:yield b''.join([f.bytes for f in voiced_frames])def main(args):path = r"D:\Workspace\dejavu27\test"# path = r"D:\workspace\test wer\ali_test_set\wav_total"files = os.listdir(path)files = [path + "\\" + f for f in files if f.endswith('.wav')]for i in xrange(len(files)):args=[3,files[i]]if len(args) != 2:sys.stderr.write('Usage: example.py <aggressiveness> <path to wav file>\n')sys.exit(1)audio, sample_rate = read_wave(args[1])vad = webrtcvad.Vad(int(args[0]))frames = frame_generator(30, audio, sample_rate)frames = list(frames)segments = vad_collector(sample_rate, 30, 300, vad, frames)for j, segment in enumerate(segments):path = './data/chunks/'+files[i][-17:-4]+'chunk-%002d.wav' % (j+1,)print(' Writing %s' % (path,))write_wave(path, segment, sample_rate)if __name__ == '__main__':main(sys.argv[1:])

注意:

VAD mode可选0-3  0是过滤掉非语音最不积极的,3是最具侵略性的,其中可调试的参数有:Channels、sample_with、sample_rate、vadmode、frame_duration(10,20,30)、 padding_duration_ms;

Python对批量文件进行VAD检测和分段相关推荐

  1. 用Python进行批量文件整理

    如何使用Python进行批量文件整理 " 引言 " 批量文件整理一直是日常工作中令人头疼的事,使用 Python 进行大批量文件整理,可以大大提升工作效率.下面来介绍几种批量文件整 ...

  2. 如何使用Python进行批量文件整理

    一.准备工作 为了用于实验,我们使用代码生成 200 个 txt 文件,代码如下. for i in range(0, 200):file_name = f'file_{i}.txt'f = open ...

  3. python工具 - 批量文件重命名

    日常工作中经常会遇到这样的情况,需要将某个文件夹下的文件按着一定的规则进行重命名,当文件数量及其庞大的时候手工一个一个的去修需要耗费大量的时间,以下python工具可以协助批量修改文件名. 场景:某文 ...

  4. 手把手教你用Python实现批量文件的压缩处理

    点击上方"Python爬虫与数据挖掘",进行关注 回复"书籍"即可获赠Python从入门到进阶共10本电子书 今 日 鸡 汤 近乡情更怯,不敢问来人. 一.前言 ...

  5. python watchdog 同时检测到多个事件_python中watchdog文件监控与检测上传功能

    引言 上一篇介绍完了观察者模式的原理,本篇想就此再介绍一个小应用,虽然我也就玩了一下午,是当时看observer正好找到的,以及还有Django-observer,但Django很久没用了,所以提下这 ...

  6. Python OpenCV 批量修改文件夹内所有图片的尺寸

    Python OpenCV 批量修改文件夹内所有图片的尺寸 简述 插件 遍历所有文件(包括子文件) 缩放图片尺寸并保存图片 遍历并修改图片 简述 遍历文件夹下的所有子文件(包括自文件夹内的)的图片批量 ...

  7. 生成Yolox检测负样本-对应空文件夹txt、批量文件重命名、批量转化三通道去除小图

    1.生成Yolox检测负样本-对应空文件夹txt import os.path import cv2 from tqdm import tqdmpath = r"G:\pachong\fuy ...

  8. python重命名文件pycharm_Python中批量修改变量名太费劲?Pycharm中使用重命名一次搞定...

    标签:rename   current   变量   阅读   tor   小伙伴   search   其他   就是如果程序中有一个变量被用得比较多,但名字起得不是很好,导致其他阅读程序的人搞不清 ...

  9. python批量下载文件-Python实现批量下载文件

    Python实现批量下载文件 #!/usr/bin/env python # -*- coding:utf-8 -*- from gevent import monkey monkey.patch_a ...

最新文章

  1. com.android.tools.build:gradle:2.0.0-alpha3 build errors
  2. shell脚本详解(七)——正则表达式、sort、uniq、tr
  3. Spring Cloud Alibaba 简介
  4. android手机可以设置屏幕锁定,安卓手机屏幕锁设置方法(九个点图案)
  5. java 租车管理系统_jsp+servlet+jdbc实现的java web共享租车信息管理系统,包括登陆注册,页面框架Easy UI...
  6. Mysql数据库drop表不用跑路,表空间传输助你恢复数据
  7. 80年代的我们对儿时零食的回忆
  8. 用python自动办公 麻瓜_(视频教程)下载:麻瓜编程Python商业爬虫学徒计划python自动化办公麻瓜麻瓜编程...
  9. pdf去除密码 html,pdf密码移除工具
  10. VMware虚拟机内Ubuntu系统安装教程
  11. Git—— 1.安装
  12. 关于nvme固态硬盘安装系统的一点经验
  13. python 顺序读取文件夹下面的文件(自定义排序方式)
  14. php im即时消息,im即时通讯php
  15. A component required a bean of type ‘com.yida.data.religion.common.dao.AddressInfoMapper‘ that could
  16. 使用递归函数计算1到n之和
  17. Anaconda3安装与配置教程(2022.11)
  18. 对于阿里云手机 OS 大家都怎么看?
  19. csp计算机认证考试题,CCF数图 | 第22次CSP认证题目精讲已收录
  20. Codeforces 102202D-A Plus Equals B【思维】 难度:**

热门文章

  1. 看板娘全是php的怎么办,【教程】给网站添加互动二次元看板娘老婆
  2. (困难)SQL练习25:获取员工其当前的薪水比其manager当前薪水还高的相关信息
  3. 神经网络阈值是什么意思,神经网络阈值怎么设置
  4. 通达信资金净流入公式_通达信成交额资金净流入指标公式
  5. 第七史诗无限显示服务器连接中,第七史诗神器满破是什么意思?神器满破攻略...
  6. java能做什么(java能做什么项目)
  7. 更换新电脑,如何将旧电脑数据/文件传输到新电脑?
  8. Python模块字典
  9. 解除Word文档保护方法汇总
  10. python学习——HTMLParser