forked from Naki21/google-speech-to-text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_response.py
61 lines (47 loc) · 2.18 KB
/
format_response.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
import math
#Convert the raw transcription into proper .srt format
def format_transcript(results, audio_file):
def format_time(seconds, offset=0): #time conversion/formatting for timestamps
frac, whole = math.modf(seconds)
f = frac * 1000
m, s = divmod(whole, 60)
h, m = divmod(m, 60)
return "%d:%02d:%02d,%03d" % (h, m, s, (f + offset * 1000))
"""Used to break up large transcript sections to prevent multi-line subtitles"""
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]
file = open( audio_file + ".srt", "w")
counter = 0 # Used for numbering lines in file
for result in results:
print(result)
alternatives = result.alternatives
for alternative in alternatives:
print(alternative)
words = alternative.words
print(words)
if len(words) < 14:
transcript = alternative.transcript
start_time = words[0].start_time
end_time = words[-1].end_time
start_time_seconds = start_time.seconds + start_time.nanos * 1e-9
end_time_seconds = end_time.seconds + end_time.nanos * 1e-9
counter += 1
file.write(str(counter) + '\n')
file.write(format_time(start_time_seconds) + ' --> ' + format_time(end_time_seconds) + '\n')
file.write(transcript + "\n\n")
else:
chunk = list(chunks(words, 14))
for words in chunk:
start_time = words[0].start_time
end_time = words[-1].end_time
start_time_seconds = start_time.seconds + start_time.nanos * 1e-9
end_time_seconds = end_time.seconds + end_time.nanos * 1e-9
section = ''
for word_info in words:
section += word_info.word + " "
counter += 1
file.write(str(counter) + '\n')
file.write(format_time(start_time_seconds) + ' --> ' + format_time(end_time_seconds) + '\n')
file.write(section + "\n\n")
file.close()