-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson_to_srt.py
31 lines (26 loc) · 1.17 KB
/
json_to_srt.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
from pysrt import SubRipFile, SubRipItem
def json_to_srt(deepspeech_json, max_word_time=10, min_sub_time=1.5, max_sub_time=3):
index = 0
subtitle = ""
start_time = 0
end_time = 0
subtitles = SubRipFile()
for word in deepspeech_json["words"]:
word["end_time"] = word["start_time"] + word["duration"]
if word["duration"] < max_word_time:
if start_time + max_sub_time >= word["end_time"] and subtitle:
subtitle += " "
subtitle += word["word"]
end_time = max(word["end_time"], start_time + min_sub_time)
elif subtitle:
# Convert to milliseconds
subtitles.append(
SubRipItem(index=++index, start=int(start_time*1000), end=int(end_time*1000), text=subtitle))
subtitle = ""
if not subtitle:
start_time = word["start_time"]
subtitle += word["word"]
end_time = max(word["end_time"], start_time + min_sub_time)
if subtitle:
subtitles.append(SubRipItem(index=++index, start=int(start_time*1000), end=int(end_time*1000), text=subtitle))
return subtitles