-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
205 lines (180 loc) · 6.38 KB
/
main.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
from deep_translator import GoogleTranslator
import argparse
import json
import subprocess
import os
import whisper
import t5
language_map = {
"ja": "Japanese",
"en": "English",
"nl": "Dutch",
}
def argparser():
parser = argparse.ArgumentParser(description="Transcribe and translate videos using MLX whisper and T5.")
parser.add_argument(
"--w-model",
default="whisper-q-mlx-large",
type=str,
help="Path the the Whisper mlx model weights",
)
parser.add_argument(
"--t-model",
default="t5-large",
type=str,
help="Path the the FLAN-T5 mlx model weights",
)
parser.add_argument(
"--file",
required=True,
type=str,
help="The file to generate subtitles on",
)
parser.add_argument(
"--input-language",
type=str,
default="en",
help="The language of the input video. 'ja' for Japanese, 'en' for English, 'nl' for Dutch.",
)
parser.add_argument(
"--output-language",
type=str,
default=None,
help="The language of the subtitles. None if the subtitles should not be translated",
)
# Note that some languages such as Japanese are not supported by FLAN-T5.
parser.add_argument(
"--local-translate",
action='store_true',
help="Translate locally using Google's FAN-T5",
)
parser.add_argument(
"--burn",
action='store_true',
help="Burn the subtitles in the video."
)
parser.add_argument(
"--out",
type=str,
default=None,
help="The name of the burned output video"
)
return parser
# Formats a floating point number into HH:MM:SS,MS format used for srt files.
def formatTime(time):
h = int(time // 3600)
m = int((time % 3600) // 60)
s = int(time % 60)
ms = int((time - int(time)) * 1000)
formatted = "{:02d}:{:02d}:{:02d},{:03d}".format(h, m, s, ms)
return formatted
class Subber:
def __init__(self):
self.w_model = args.w_model
self.t5_model = args.t_model
self.inputFilePath = args.file
self.inputLanguage = args.input_language
self.outputLanguage = args.output_language
self.subtitlePath = f"{self.inputFilePath}_{self.outputLanguage}.srt"
self.transcriptPath = f"{self.inputFilePath}_transcription.txt"
def _transcribe(self):
if os.path.exists(self.transcriptPath):
print("Transcription file found.")
with open(self.transcriptPath, "r") as f:
self.transcription = json.load(f)
return
print("Transcribing.")
self.transcription = whisper.transcribe(self.inputFilePath, path_or_hf_repo=self.w_model)
with open(self.transcriptPath, "w+") as f:
json.dump(self.transcription, f)
def _translate(self):
# If only subbing and no translation is needed
if not self.outputLanguage:
self.translatedSubs = self.transcription["segments"]
self.subtitlePath = f"{self.inputFilePath}_{self.inputLanguage}.srt"
return
print("Translating.")
if args.local_translate:
self._translate_local()
else:
self._translate_google()
# Translating using Google Translate api.
def _translate_google(self):
self.translatedSubs = []
for part in self.transcription["segments"]:
translatedPart = {}
translatedPart["start"] = part["start"]
translatedPart["end"] = part["end"]
translatedPart["text"] = GoogleTranslator(source=self.inputLanguage, target=self.outputLanguage).translate(part["text"])
self.translatedSubs.append(translatedPart)
# Translating using a local FAN-T5 model.
def _translate_local(self):
self.translatedSubs = []
model, tokenizer = t5.load_model(self.t5_model, "bfloat16")
inp_lang = language_map[self.inputLanguage]
outp_lang = language_map[self.outputLanguage]
for part in self.transcription["segments"]:
text = part["text"]
# Run t5 inference.
prompt = f"translate from {inp_lang} to {outp_lang}: {text}"
print("\n", prompt)
tokens = []
for token, n_tokens in zip(
t5.generate(prompt, model, tokenizer, 0.0), range(200)
):
if token.item() == tokenizer.eos_id:
break
print(
tokenizer.decode([token.item()], with_sep=n_tokens > 0),
end="",
flush=True,
)
tokens.append(token.item())
translatedPart = {}
translatedPart["start"] = part["start"]
translatedPart["end"] = part["end"]
translatedPart["text"] = tokenizer.decode(tokens)
self.translatedSubs.append(translatedPart)
def _create_subtitles(self):
with open(self.subtitlePath, "w+") as f:
for i, part in enumerate(self.translatedSubs):
start = formatTime(part['start'])
end = formatTime(part['end'])
text = part['text']
f.write(f"{i}\n{start} --> {end}\n")
f.write(f"{text}\n\n")
def _burnSubtitles(self, output_video):
print("Burning subtitles.")
style = "Fontname=Roboto,OutlineColour=&H40000000,BorderStyle=3,ScaleY=0.87, ScaleX=0.87,Fontsize=15"
subprocess.run(
[
"ffmpeg",
"-i",
self.inputFilePath,
"-vf",
f"subtitles='{self.subtitlePath}':force_style='{style}'",
"-c:v",
"libx264",
"-crf",
"18",
"-c:a",
"copy",
output_video if output_video else f"{self.outputLanguage}_{self.inputFilePath}",
],
stdout = subprocess.DEVNULL,
stderr = subprocess.DEVNULL
)
def run(self):
if not os.path.exists(self.subtitlePath):
self._transcribe()
self._translate()
self._create_subtitles()
else:
print("Subtitle file found.")
if args.burn:
self._burnSubtitles(args.out)
parser = argparser()
args = parser.parse_args()
if __name__ == "__main__":
s = Subber()
s.run()