Add speech_only.py script
Browse files- scripts/speech_only.py +60 -0
scripts/speech_only.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from silero_vad import load_silero_vad
|
| 3 |
+
from silero_vad import read_audio , get_speech_timestamps , save_audio
|
| 4 |
+
from silero_vad import VADIterator , collect_chunks
|
| 5 |
+
import torch
|
| 6 |
+
import json
|
| 7 |
+
import os
|
| 8 |
+
import argparse
|
| 9 |
+
import logging
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
logging.basicConfig(
|
| 13 |
+
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
|
| 14 |
+
datefmt="%Y-%m-%d %H:%M:%S",
|
| 15 |
+
level=os.environ.get("LOG_LEVEL", "INFO").upper())
|
| 16 |
+
LOGGER = logging.getLogger(__name__)
|
| 17 |
+
|
| 18 |
+
def main(args):
|
| 19 |
+
parser = argparse.ArgumentParser(args)
|
| 20 |
+
parser.add_argument("--folder", type=str, required=True, help="folder" )
|
| 21 |
+
parser.add_argument("--sfx", type=str, required=False, default="wav",help="audio suffix" )
|
| 22 |
+
parser.add_argument("--sr", type=int, required=False, default="16000",help="sampling rate" )
|
| 23 |
+
parser.add_argument("--out_folder", type=str, required=False, default=None,help="output folder" )
|
| 24 |
+
parser.add_argument("--out_file", type=str, required=True, help="json output" )
|
| 25 |
+
parser.add_argument("--reverse", action="store_true", help="reverse processing order in folder")
|
| 26 |
+
args = parser.parse_args()
|
| 27 |
+
|
| 28 |
+
model = load_silero_vad(onnx=True)
|
| 29 |
+
sr=args.sr
|
| 30 |
+
out_folder=args.out_folder
|
| 31 |
+
if out_folder:
|
| 32 |
+
Path(out_folder).mkdir(parents=True, exist_ok=True)
|
| 33 |
+
save_wav=True
|
| 34 |
+
audio_list = sorted(os.listdir(args.folder),reverse=args.reverse)
|
| 35 |
+
with open(args.out_file, "w") as outfile:
|
| 36 |
+
for audiofile in audio_list:
|
| 37 |
+
if audiofile.endswith(args.sfx):
|
| 38 |
+
audiopath = os.path.join(args.folder, audiofile)
|
| 39 |
+
audiofile = audiofile.replace(args.sfx,"wav")
|
| 40 |
+
audio_id = Path(audiofile).stem
|
| 41 |
+
out_audio=f"{out_folder}/{audiofile}"
|
| 42 |
+
if os.path.isfile(out_audio):
|
| 43 |
+
LOGGER.info(f"skipping {audiopath}")
|
| 44 |
+
continue
|
| 45 |
+
LOGGER.info(f"processing {audiopath}")
|
| 46 |
+
wav = read_audio(audiopath, sampling_rate=sr)
|
| 47 |
+
speech_timestamps = get_speech_timestamps(wav, model, sampling_rate=sr)
|
| 48 |
+
|
| 49 |
+
if speech_timestamps:
|
| 50 |
+
LOGGER.info(f"vad-processed {audiofile}: {len(speech_timestamps)} chunks")
|
| 51 |
+
for turn in speech_timestamps:
|
| 52 |
+
entry = {"audio_id": audio_id, "offset": turn['start']/sr, "duration": (turn['end']-turn['start'])/sr}
|
| 53 |
+
json.dump(entry, outfile)
|
| 54 |
+
outfile.write('\n')
|
| 55 |
+
if save_wav:
|
| 56 |
+
save_audio(out_audio,collect_chunks(speech_timestamps, wav), sampling_rate=sr)
|
| 57 |
+
LOGGER.info(f"vad-processed {audiofile} written to {out_audio}")
|
| 58 |
+
|
| 59 |
+
if __name__ == '__main__':
|
| 60 |
+
main(sys.argv)
|