-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgcloudReadJSON.py
More file actions
executable file
·52 lines (47 loc) · 1.31 KB
/
gcloudReadJSON.py
File metadata and controls
executable file
·52 lines (47 loc) · 1.31 KB
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
import json
import sys
data = {}
building_block = []
sentence = ''
time_sector = 1
current_time = 0.0
TIMING = 1
timing_block = []
captions = []
with open('out','r') as f:
data = json.load(f)
for block in data['response']['results']:
for tiny_block in block['alternatives']:
for word in tiny_block['words']:
building_block.append(word['word'])
if 'endTime' in word:
end_time = word['endTime']
end_time = end_time[:-1]
end_time = float(end_time)
if end_time > time_sector * TIMING:
sentence = ' '.join(building_block)
building_block = []
output = (current_time, end_time, sentence)
captions.append(output)
current_time = end_time
time_sector = end_time/1 + 1
sentence = ' '.join(building_block)
output = (current_time, end_time, sentence)
captions.append(output)
def formatTiming(x):
time_str = str(x)
time_str = time_str[:-1]
time_str = time_str.split('.')
minute,sec = divmod(int(time_str[0]),60)
hour, minute = divmod(minute, 60)
time_str[0] = sec
time_str.insert(0, minute)
time_str.insert(0, hour)
str_format = '{:02}:{:02}:{:02},{:0<3}'.format(time_str[0], time_str[1], time_str[2], time_str[3])
return str_format
for x,y in enumerate(captions):
print(x+1)
str_format = '{} --> {}'.format(formatTiming(y[0]), formatTiming(y[1]))
print (str_format)
print (y[2])
print('')