-
Notifications
You must be signed in to change notification settings - Fork 0
/
srtImp.py
68 lines (53 loc) · 1.81 KB
/
srtImp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from vosk import Model, KaldiRecognizer, SetLogLevel
import sys
import os
import wave
import subprocess
import srt
import json
import datetime
import pysrt
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(input_file,wp):
WORDS_PER_LINE = wp
#process = subprocess.Popen(['ffmpeg', '-loglevel', 'quiet', '-i',input_file,'-ar', str(sample_rate) , '-ac', '1', '-f', 's16le', '-'],stdout=subprocess.PIPE)
results = []
subs = []
input_file.read(44)
while True:
data = input_file.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)
if not 'result' in jres:
continue
words = jres['result']
for j in range(0, len(words), WORDS_PER_LINE):
line = words[j : j + WORDS_PER_LINE]
s = srt.Subtitle(index=len(subs),
content=" ".join([l['word'] for l in line]),
start=datetime.timedelta(seconds=line[0]['start']),
end=datetime.timedelta(seconds=line[-1]['end']))
subs.append(s)
return subs
def createText():
srtfile = pysrt.open("texts_generated/file.srt")
txtfile = open("texts_generated/text.txt","w")
strtn = ""
for sub in srtfile:
tx = sub.text
txtfile.write(tx+" ")
strtn+=tx+"\n"
#srtfile.close()
txtfile.close()
return strtn