-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtt_to_srt.py
237 lines (185 loc) · 6.62 KB
/
vtt_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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/python
# Jansen A. Simanullang / Jeison Cardoso
"""Convert of vtt to srt format"""
import os
import re
import sys
from string import Template
from stat import S_ISDIR, ST_MODE, S_ISREG
def convert_header(contents):
"""Convert of vtt header to srt format
Keyword arguments:
contents
"""
replacement = re.sub(r"WEBVTT\n", "", contents)
replacement = re.sub(r"Kind:[ \-\w]+\n", "", replacement)
replacement = re.sub(r"Language:[ \-\w]+\n", "", replacement)
return replacement
def padding_timestamp(contents):
"""Add 00 to padding timestamp of to srt format
Keyword arguments:
contents
"""
find_srt = Template(r'$a,$b --> $a,$b(?:[ \-\w]+:[\w\%\d:,.]+)*\n')
minute = r"((?:\d\d:){1}\d\d)"
second = r"((?:\d\d:){0}\d\d)"
padding_minute = find_srt.substitute(a=minute, b=r"(\d{0,3})")
padding_second = find_srt.substitute(a=second, b=r"(\d{0,3})")
replacement = re.sub(padding_minute, r"00:\1,\2 --> 00:\3,\4\n", contents)
return re.sub(padding_second, r"00:00:\1,\2 --> 00:00:\3,\4\n", replacement)
def convert_timestamp(contents):
"""Convert timestamp of vtt file to srt format
Keyword arguments:
contents
"""
find_vtt = Template(r'$a.$b --> $a.$b(?:[ \-\w]+:[\w\%\d:,.]+)*\n')
all_timestamp = find_vtt.substitute(a=r"((?:\d\d:){0,2}\d\d)", b=r"(\d{0,3})")
return padding_timestamp(re.sub(all_timestamp, r"\1,\2 --> \3,\4\n", contents))
def convert_content(contents):
"""Convert content of vtt file to srt format
Keyword arguments:
contents
"""
replacement = convert_timestamp(contents)
replacement = convert_header(replacement)
replacement = re.sub(r"<c[.\w\d]*>", "", replacement)
replacement = re.sub(r"</c>", "", replacement)
replacement = re.sub(r"<\d\d:\d\d:\d\d.\d\d\d>", "", replacement)
replacement = re.sub(r"::[\-\w]+\([\-.\w\d]+\)[ ]*{[.,:;\(\) \-\w\d]+\n }\n", "", replacement)
replacement = re.sub(r"Style:\n##\n", "", replacement)
replacement = add_sequence_numbers(replacement)
return replacement
def timestamp_line(content):
"""Check if line is a timestamp srt format
Keyword arguments:
contents
"""
return re.match(r"((\d\d:){2}\d\d),(\d{3}) --> ((\d\d:){2}\d\d),(\d{3})", content) is not None
def add_sequence_numbers(contents):
"""Adds sequence numbers to subtitle contents and returns new subtitle contents
Keyword arguments:
contents
"""
output = ''
lines = contents.split(os.linesep)
i = 1
for line in lines:
if timestamp_line(line):
output += str(i) + os.linesep
i += 1
output += line + os.linesep
return output
def file_create(str_name_file: str, str_data):
"""Create a file with some data
Keyword arguments:
str_name_file -- filename pat
str_data -- dat to write
"""
try:
with open(str_name_file, "w", encoding='utf-8') as file:
file.writelines(str(str_data))
except IOError:
str_name_file = str_name_file.split(os.sep)[-1]
with open(str_name_file, "w") as file:
file.writelines(str(str_data))
print("file created: " + str_name_file + "\n")
def read_text_file(str_name_file: str):
"""Read a file text
Keyword arguments:
str_name_file -- filename pat
"""
content: str = ''
with open(str_name_file, mode="r", encoding='utf-8') as file:
print("file being read: " + str_name_file + "\n")
content = file.read()
return content
def vtt_to_srt(str_name_file: str):
"""Convert vtt file to a srt file
Keyword arguments:
str_name_file -- filename path
"""
file_contents: str = read_text_file(str_name_file)
str_data: str = ""
str_data = str_data + convert_content(file_contents)
str_name_file: str = str_name_file.replace(".vtt", ".srt")
print(str_name_file)
file_create(str_name_file, str_data)
def str_vtt_to_srt(str_vtt: str):
"""Convert vtt file to a srt file
Keyword arguments:
str_name_file -- filename path
"""
file_contents: str = str_vtt
str_data: str = ""
str_data = str_data + convert_content(file_contents)
return str_data
def walk_tree(top_most_path, callback):
"""Recursively descend the directory tree rooted at top_most_path,
calling the callback function for each regular file
Keyword arguments:
top_most_path -- parent directory
callback -- function to call
"""
for file in os.listdir(top_most_path):
pathname = os.path.join(top_most_path, file)
mode = os.stat(pathname)[ST_MODE]
if S_ISDIR(mode):
# It's a directory, recurse into it
walk_tree(pathname, callback)
elif S_ISREG(mode):
# It's a file, call the callback function
callback(pathname)
else:
# Unknown file type, print a message
print("Skipping %s" % pathname)
def walk_dir(top_most_path, callback):
"""Walk a directory
Keyword arguments:
top_most_path -- parent directory
callback -- function to call
"""
for file in os.listdir(top_most_path):
pathname = os.path.join(top_most_path, file)
if not os.path.isdir(pathname):
# It"s a file, call the callback function
callback(pathname)
def convert_vtt_to_str(file):
"""Convert vtt file to string
Keyword arguments:
f -- file to convert
"""
if ".vtt" in file:
vtt_to_srt(file)
def vtts_to_srt(directory, rec = False):
"""Walk down directory seaching for vtt files
Keyword arguments:
directory -- path to search
rec -- enable recursive
"""
top_most_path = directory
if rec:
walk_tree(top_most_path, convert_vtt_to_str)
else:
walk_dir(top_most_path, convert_vtt_to_str)
def print_usage():
"""Show a info message about the usage"""
print("\nUsage:\tvtt_to_srt pathname [-r]\n")
print("\tpathname\t- a file or directory with files to be converted")
print("\t-r\t\t- walk path recursively\n")
def main():
"""main
Keyword arguments:
pathname - a file or directory with files to be converted
-r walk path recursively
"""
if len(sys.argv) < 2 or sys.argv[1] == "--help" or not os.path.exists(sys.argv[1]):
print_usage()
sys.exit()
path = sys.argv[1]
rec = bool(len(sys.argv) > 2 and sys.argv[2] == "-r")
if os.path.isdir(path):
vtts_to_srt(path, rec)
else:
vtt_to_srt(path)
if __name__ == "__main__":
main()