-
Notifications
You must be signed in to change notification settings - Fork 0
/
captioning.py
399 lines (348 loc) · 9.97 KB
/
captioning.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import csv
import os
import random
from typing import List, TypedDict
from numpy.lib import math
import torch
from faster_whisper import WhisperModel
from moviepy import (
Clip,
CompositeVideoClip,
TextClip,
VideoFileClip,
vfx,
)
AVOID_LIST = {
"a",
"about",
"all",
"an",
"and",
"are",
"as",
"at",
"be",
"been",
"but",
"by",
"can",
"could",
"came",
"come",
"did",
"do",
"does",
"for",
"from",
"had",
"has",
"have",
"he",
"her",
"here",
"him",
"his",
"how",
"if",
"in",
"into",
"is",
"it",
"its",
"like",
"me",
"more",
"my",
"no",
"not",
"of",
"on",
"one",
"or",
"our",
"out",
"she",
"should",
"so",
"some",
"than",
"that",
"the",
"their",
"them",
"then",
"there",
"these",
"they",
"this",
"those",
"to",
"too",
"up",
"us",
"was",
"we",
"were",
"what",
"when",
"where",
"which",
"who",
"will",
"with",
"would",
"you",
"your",
"am",
"an",
"are",
"at",
"be",
"by",
"did",
"do",
"go",
"had",
"has",
"have",
"he",
"her",
"him",
"his",
"i",
"if",
"in",
"is",
"it",
"me",
"my",
"no",
"not",
"of",
"on",
"or",
"so",
"that",
"this",
"to",
"up",
"us",
"we",
"you",
}
class WordTranscript(TypedDict):
start: float
end: float
probability: float
word: str
# This is because whisper's start time is not reliable and is sometimes way too early
def _word_timing_adjusted(word: WordTranscript):
start = float(word["start"])
end = float(word["end"])
duration = end - start
max_duration = math.log(len(word["word"]), 8)
if duration > max_duration:
start = end - max_duration
return start, end
def _get_positon_param(pos_param: str):
coordinates = pos_param.split(",")
if len(coordinates) != 2:
raise Exception(
"invalid position parameter. Must be two values separated by a comma with the whole thing in quotes"
)
try:
x, y = int(coordinates[0]), int(coordinates[1])
except ValueError:
raise Exception("coordinates must be integers")
return (x, y)
def transcribe_file(file: str):
video = VideoFileClip(file)
video.audio.write_audiofile("temp/temp.wav") # pyright: ignore
device = "cuda" if torch.cuda.is_available() else "cpu"
model = WhisperModel("large-v3", device, compute_type="float32")
segments, _ = model.transcribe(
"temp/temp.wav",
suppress_tokens=[],
language="en",
# vad_filter=True,
condition_on_previous_text=False,
log_progress=True,
beam_size=10,
word_timestamps=True,
)
transcription: List[WordTranscript] = []
for segment in segments:
for word in segment.words or []:
transcription.append(
{
"start": word.start,
"end": word.end,
"probability": word.probability,
"word": word.word,
}
)
filename = os.path.basename(file)
with open(f"output/{filename}.csv", "w") as output:
writer = csv.DictWriter(
output, fieldnames=["start", "end", "probability", "word"]
)
writer.writeheader()
writer.writerows(transcription)
def caption_video(
file: str,
font: str,
font_size: int,
caption_position: tuple[int, int] | None,
caption_type: int = 1,
):
# ##### private functions #######
def _create_font_autoresize(size: int, word: str, max_width: int = 9999999):
clip = None
for i in range(10, -1, -1):
modifier = i / 10
clip = TextClip(
font,
text=word,
method="label",
stroke_color="black",
vertical_align="center",
stroke_width=5,
size=(None, size),
margin=(13, 2, 13, 2),
font_size=size * modifier,
color="white",
)
if clip.size[0] < max_width:
break
assert clip is not None
return clip
################################
with open(file + ".csv", mode="r") as csvfile:
reader = csv.DictReader(csvfile)
transcription: List[WordTranscript] = list(reader) # pyright: ignore
video = VideoFileClip(file)
text_clips: List[Clip.Clip] = []
if caption_type == 1:
if caption_position is None:
xpos, ypos = video.size[0] * 0.1, video.size[1] * 0.1
else:
xpos, ypos = caption_position
template_width = video.size[0] - xpos
current_line_clips: List[TextClip] = []
x = xpos
for n_word in transcription:
clip = _create_font_autoresize(font_size, n_word["word"])
clip = TextClip(
font,
text=n_word["word"],
method="label",
stroke_color="black",
stroke_width=5,
size=(None, font_size + 8),
margin=(0, 10),
font_size=font_size,
color="white",
)
start, _ = _word_timing_adjusted(n_word)
if x + clip.size[0] > template_width:
x = xpos
for textclip in current_line_clips:
text_clips.append(
textclip.with_duration(
start - textclip.start + 0.5
).with_effects(
[
vfx.CrossFadeIn(0.5),
vfx.CrossFadeOut(0.3),
]
)
)
current_line_clips = []
position = (x + 10, ypos)
clip: TextClip = clip.with_position(position).with_start(start)
clip.size = (clip.size[0], clip.size[1] * 2)
current_line_clips.append(clip)
x = x + clip.size[0]
if caption_type == 2:
words_to_delete = []
for i, word in enumerate(transcription):
if i == 0:
continue
p_word = transcription[i - 1]
if word["word"].startswith("-"):
p_word["word"] = p_word["word"] + word["word"]
p_word["end"] = word["end"]
words_to_delete.append(word)
for word in words_to_delete:
transcription.remove(word)
if video.size[0] > video.size[1]:
width, height = int(video.size[0] * 0.3), int(video.size[1] * 0.34)
else:
width, height = int(video.size[0] * 0.4), int(video.size[1] * 0.3)
font_max = font_size * 2
fonts = [int(font_max * 0.45), int(font_max * 0.65), font_max]
y = 0
x = 0
current_line: List[TextClip] = []
def _new_font_size(word: str):
rand = random.random()
if word.lower().replace(".", "").replace(",", "") in AVOID_LIST:
# print(f"avoided {text} since it is common")
return fonts[0] if rand < 0.6 else fonts[1]
else:
return fonts[0] if rand < 0.4 else fonts[1] if rand < 0.65 else fonts[2]
if caption_position is None or caption_position == "":
xpos, ypos = 220, 60
else:
xpos, ypos = caption_position
new_font_size = fonts[1]
def _place_current_line(line: List[TextClip], end_time: float):
for line_clip in line:
text_clips.append(
line_clip.with_duration(
end_time - line_clip.start + 0.5
).with_effects(
[
vfx.CrossFadeIn(0.3),
vfx.CrossFadeOut(0.3),
]
)
)
for word_index, n_word in enumerate(transcription):
p_word = transcription[word_index - 1] if word_index > 0 else None
text = n_word["word"].strip()
start, end = _word_timing_adjusted(n_word)
p_start, p_end = (
_word_timing_adjusted(p_word) if p_word is not None else (0, 0)
)
clip = _create_font_autoresize(new_font_size, text, width)
reset = False
if x > 0: # always print text if at the beginnig of line
if x + clip.size[0] > width:
y = y + clip.size[1] + 5
x = 0
new_font_size = _new_font_size(text)
clip = _create_font_autoresize(new_font_size, text, width)
if y + clip.size[1] > height:
reset = True
if len(current_line) > 0 and start - p_start > 1.0:
reset = True
if reset:
_place_current_line(current_line, p_end)
current_line = []
x, y = 0, 0
new_font_size = _new_font_size(text)
clip = _create_font_autoresize(new_font_size, text, width)
clip = clip.with_position((x + xpos, y + ypos)).with_start(start)
x = x + clip.size[0]
current_line.append(clip)
if word_index == len(transcription) - 1 and len(current_line) > 0:
print(f"caught {len(current_line)} elements left at the end")
_place_current_line(
current_line,
p_end + 1.0 if p_end + 1.0 < video.duration else video.duration,
)
else:
raise Exception("Not supported caption type")
video_with_text = CompositeVideoClip([video] + text_clips)
video_with_text.write_videofile("output/output_sentence.mp4")