-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtt2str.py
executable file
·71 lines (51 loc) · 1.41 KB
/
vtt2str.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
#!/usr/bin/env python3
import argparse
import webvtt
import re
from pathlib import Path
def parse_args():
argparser = argparse.ArgumentParser(
description="WEBVTT to SRT subtitle converter.",
)
argparser.add_argument(
"path",
metavar="PATH",
help="A VTT file or directory with files to be converted.",
)
return argparser.parse_args()
def get_files(path: Path) -> list:
files = []
if path.is_dir():
files += path.glob("*.vtt")
else:
files.append(path)
return files
def convert_vtt_to_srt(vtt: Path) -> None:
print("\nInput: " + vtt.name)
srt = vtt.with_suffix(".srt")
print("Output: " + srt.name)
if vtt == srt:
raise (Exception("The input and output file is the same file."))
with srt.open(mode="w") as f:
i = 0
for caption in webvtt.read(vtt):
i += 1
print(i, file=f)
print(
re.sub(r"\.", ",", caption.start)
+ " --> "
+ re.sub(r"\.", ",", caption.end),
file=f,
)
print(caption.text + "\n", file=f)
def main():
args = parse_args()
files = get_files(Path(args.path))
total = len(files)
i = 0
for vtt in files:
i += 1
print("Process file %02d / %02d" % (i, total))
convert_vtt_to_srt(vtt)
if __name__ == "__main__":
main()