forked from morevnaproject-org/subtitles-translation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsrt2srt.py
executable file
·89 lines (69 loc) · 2.48 KB
/
msrt2srt.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
# A script for converting the so-called multi-language srt files to regular srt files
# Currently licenced under the Modified BSD license
from gettext import gettext as _
import sys
from argparse import ArgumentParser
__version__ = '0.1'
def process_args():
parser = ArgumentParser(description=_("Convert msrt to srt files."))
parser.add_argument("input",
help=_("A path to the msrt file you want to convert."))
parser.add_argument("language",
help=_("The language to extract"))
parser.add_argument("--output", "-o",
action="store",
help=_("A path to the where you want the output srt file to be written to. If unspecified, output is written to the standard output."),
default="-")
parser.add_argument("--version", "-v", action='version', version=_("msrt2srt version %s") % __version__)
return parser.parse_args()
def parse_subgroup(subGroup, language):
if not subGroup:
return ""
groupLines = subGroup.split("\n")
subGroup = ""
index = int(groupLines[0])
startTime, endTime = groupLines[1].split("-->")
startTime = startTime.strip()
endTime = endTime.strip()
langText = ""
for l in groupLines[2:]:
if l and l.startswith("[" + language + "]"):
langText += l[2+len(language):].strip() + "\n"
if langText:
return (str(index) + "\n" +
startTime + " --> " + endTime + "\n" +
langText +
"\n")
def convert(input, output, language):
subGroup = ""
for line in input:
#print(line, end='')
if line.lstrip().startswith("[*]"):
# Ignore comments
#print("Ignore")
pass
elif line == "\n":
#print("Done")
# A blank line marks the end of a subgroup, time to parse!
output.write(parse_subgroup(subGroup, language))
subGroup = ""
else:
#print("Add")
# Line is part of the current subgroup
subGroup += line
output.write(parse_subgroup(subGroup, language))
def main():
args = process_args()
with open(args.input, 'r') as input:
try:
if args.output == "-":
output = sys.stdout
else:
output = open(args.output, 'w')
convert(input, output, args.language)
finally:
if output != sys.stdout:
output.close()
if __name__ == "__main__":
main()