-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine_srt_files.py
42 lines (32 loc) · 974 Bytes
/
combine_srt_files.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
import datetime
import sys
import itertools
import heapq
class Line(object):
def __init__(self, when, text):
self.when = when
self.text = text
def __lt__(self, other):
return self.when < other.when
def __repr__(self):
return self.text
def srt_file_producer(file_name):
with open(file_name, 'r') as f:
while True:
if not f.readline():
break
txt = ''
s = f.readline()
when = s.split(' ')[0] # 00:00:01,648 --> 00:00:05,594
when = datetime.datetime.strptime(when, '%H:%M:%S,%f')
while s and s != '\n':
txt += s
s = f.readline()
yield Line(when, txt)
def combine_srt_files(file_names):
srt_files = map(srt_file_producer, file_names)
return heapq.merge(*srt_files)
ind = itertools.count(1)
for line in combine_srt_files(sys.argv[1:]):
print(next(ind))
print(line)