-
Notifications
You must be signed in to change notification settings - Fork 538
/
convert_output.py
100 lines (75 loc) · 3.05 KB
/
convert_output.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
import argparse
import json
import os
class TxtFormatter:
@classmethod
def preamble(cls):
return ""
@classmethod
def format_chunk(cls, chunk, index):
text = chunk['text']
return f"{text}\n"
class SrtFormatter:
@classmethod
def preamble(cls):
return ""
@classmethod
def format_seconds(cls, seconds):
whole_seconds = int(seconds)
milliseconds = int((seconds - whole_seconds) * 1000)
hours = whole_seconds // 3600
minutes = (whole_seconds % 3600) // 60
seconds = whole_seconds % 60
return f"{hours:02d}:{minutes:02d}:{seconds:02d},{milliseconds:03d}"
@classmethod
def format_chunk(cls, chunk, index):
text = chunk['text']
start, end = chunk['timestamp'][0], chunk['timestamp'][1]
start_format, end_format = cls.format_seconds(start), cls.format_seconds(end)
return f"{index}\n{start_format} --> {end_format}\n{text}\n\n"
class VttFormatter:
@classmethod
def preamble(cls):
return "WEBVTT\n\n"
@classmethod
def format_seconds(cls, seconds):
whole_seconds = int(seconds)
milliseconds = int((seconds - whole_seconds) * 1000)
hours = whole_seconds // 3600
minutes = (whole_seconds % 3600) // 60
seconds = whole_seconds % 60
return f"{hours:02d}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}"
@classmethod
def format_chunk(cls, chunk, index):
text = chunk['text']
start, end = chunk['timestamp'][0], chunk['timestamp'][1]
start_format, end_format = cls.format_seconds(start), cls.format_seconds(end)
return f"{index}\n{start_format} --> {end_format}\n{text}\n\n"
def convert(input_path, output_format, output_dir, verbose):
with open(input_path, 'r') as file:
data = json.load(file)
formatter_class = {
'srt': SrtFormatter,
'vtt': VttFormatter,
'txt': TxtFormatter
}.get(output_format)
string = formatter_class.preamble()
for index, chunk in enumerate(data['chunks'], 1):
entry = formatter_class.format_chunk(chunk, index)
if verbose:
print(entry)
string += entry
with open(os.path.join(output_dir, f"output.{output_format}"), 'w', encoding='utf-8') as file:
file.write(string)
def main():
parser = argparse.ArgumentParser(description="Convert JSON to an output format.")
parser.add_argument("input_file", help="Input JSON file path")
parser.add_argument("-f", "--output_format", default="all", help="Format of the output file (default: srt)", choices=["txt", "vtt", "srt"])
parser.add_argument("-o", "--output_dir", default=".", help="Directory where the output file/s is/are saved")
parser.add_argument("--verbose", action="store_true", help="Print each VTT entry as it's added")
args = parser.parse_args()
convert(args.input_file, args.output_format, args.output_dir, args.verbose)
if __name__ == "__main__":
# Example Usage:
# python convert_output.py output.json -f vtt -o /tmp/my/output/dir
main()