Skip to content

Commit

Permalink
Add srt example
Browse files Browse the repository at this point in the history
  • Loading branch information
nshmyrev committed Oct 7, 2020
1 parent d666876 commit 7af3e9a
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions python/example/test_srt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3

from vosk import Model, KaldiRecognizer, SetLogLevel
import sys
import os
import wave
import subprocess
import srt
import json
import datetime

SetLogLevel(-1)

if not os.path.exists("model"):
print ("Please download the model from https://alphacephei.com/vosk/models and unpack as 'model' in the current folder.")
exit (1)

sample_rate=16000
model = Model("model")
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)


def transcribe():
results = []
subs = []
while True:
data = process.stdout.read(4000)
if len(data) == 0:
break
if rec.AcceptWaveform(data):
results.append(rec.Result())
results.append(rec.FinalResult())

for i, res in enumerate(results):
jres = json.loads(res)
s = srt.Subtitle(index=i,
content=jres['text'],
start=datetime.timedelta(seconds=jres['result'][0]['start']),
end=datetime.timedelta(seconds=jres['result'][-1]['end']))
subs.append(s)
return subs

print (srt.compose(transcribe()))

0 comments on commit 7af3e9a

Please sign in to comment.