2020-10-29 10:45:07 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from vosk import Model, KaldiRecognizer, SetLogLevel
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import wave
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
SetLogLevel(0)
|
|
|
|
|
2020-11-12 08:54:00 +00:00
|
|
|
modelBase = os.path.join(os.getenv('XDG_DATA_HOME', os.environ['HOME']), 'vosk/models')
|
2020-10-29 10:45:07 +00:00
|
|
|
modelPath = os.path.join(modelBase, sys.argv[2] if len(sys.argv) > 2 else 'daanzu')
|
|
|
|
if not os.path.exists(modelPath):
|
|
|
|
print (f"Please download a model from https://alphacephei.com/vosk/models and unpack it to {modelPath}.", file=sys.stderr)
|
|
|
|
exit (1)
|
|
|
|
|
|
|
|
sample_rate=16000
|
|
|
|
model = Model(modelPath)
|
|
|
|
rec = KaldiRecognizer(model, sample_rate)
|
|
|
|
|
|
|
|
process = subprocess.Popen(['ffmpeg', '-loglevel', 'quiet', '-i',
|
|
|
|
sys.argv[1],
|
|
|
|
'-ar', str(sample_rate) , '-ac', '1', '-f', 's16le', '-'],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
|
2020-11-12 08:54:00 +00:00
|
|
|
def getText(result):
|
|
|
|
res = json.loads(result)
|
|
|
|
if not 'result' in res:
|
|
|
|
return res['text']
|
|
|
|
sec = res['result'][0]['start']
|
|
|
|
time = "%02d:%02d" % (sec // 60, sec % 60)
|
|
|
|
return "%s %s" % (time, res['text'])
|
|
|
|
|
2020-10-29 10:45:07 +00:00
|
|
|
while True:
|
|
|
|
data = process.stdout.read(4000)
|
|
|
|
if len(data) == 0:
|
|
|
|
break
|
|
|
|
if rec.AcceptWaveform(data):
|
2020-11-12 08:54:00 +00:00
|
|
|
print(getText(rec.Result()))
|
2020-10-29 10:45:07 +00:00
|
|
|
|
2020-11-12 08:54:00 +00:00
|
|
|
print(getText(rec.FinalResult()))
|