-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchap2vtt
executable file
·41 lines (35 loc) · 1003 Bytes
/
chap2vtt
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
#!/usr/bin/env python3
def format_time(time):
if '.' in time:
return time
else:
return time + '.000'
def main(timing_file):
outfile = timing_file.replace('chap','vtt')
with open(outfile, 'w') as tf:
print("WEBVTT\n\n", file=tf)
markers = []
with open(timing_file, 'r') as chap:
next(chap) # skip header row
for line in chap:
parts = line.strip().split(' ', 1)
markers.append({'time': parts[0], 'title':parts[1]})
# ~ markers.append({'time': get_length(video_file), 'title':'End'})
with open(outfile, 'w') as vtt:
print('WEBVTT\n\n', file=vtt)
_len = len(markers)
for i, item in enumerate(markers):
if i == 0:
print(f"""{i}
00:00:00.000 --> {format_time(item['time'])}
- Beginning
""", file=vtt)
print(f"""{i+1}
{format_time(item['time'])} --> {format_time(markers[i+1]['time'])}
- {item['title']}
""", file=vtt)
if i == _len-2:
break
if __name__=='__main__':
import plac
plac.call(main)