-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmicrodvd2srt.py
executable file
·61 lines (43 loc) · 1.44 KB
/
microdvd2srt.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import argparse
LINE = re.compile(r"^{(?P<start>\d+)}{(?P<stop>\d+)}(?P<text>.*)")
TPL = """%d
%s --> %s
%s
"""
def get_time(frame, fps):
hour = 0
minute = 0
second = 0
milisecond = 0
val = frame/fps
if val > 3600:
hour = int(val/3600)
val = val - (hour * 3600)
if val > 60:
minute = int(val/60)
val = val - (minute * 60)
if val > 0:
second = int(val)
milisecond = (val - second) * 1000
return "%02d:%02d:%02d,%03.3d" % (hour, minute, second, milisecond)
def get_line(lno, line, fps):
start, stop, text = LINE.match(line).groups()
start = get_time(int(start), fps)
stop = get_time(int(stop), fps)
return TPL % (lno, start, stop, "\n".join(text.split('|')))
def main():
parser = argparse.ArgumentParser(description='Convert MicroDVD subtitles '
'to SRT')
parser.add_argument('-f', '--fps', help='Frames per second. The '
'destination movie file FPS. Default 25 frames per '
'second.', default=25, type=float)
parser.add_argument('filename', help='Subtitle in MicroDVD format.')
args = parser.parse_args()
with open(args.filename) as fob:
for index, line in enumerate(fob.readlines(), 1):
print(get_line(index, line, float(args.fps)))
if __name__ == "__main__":
main()