-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_common.py
455 lines (387 loc) · 15.2 KB
/
video_common.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
"""
Common functionality for dealing with video files.
"""
import itertools
import pprint
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Callable, Iterator, List, NamedTuple, Optional, cast
import ffmpeg
import more_itertools
import numpy as np
from cv2 import cv2
from ffmpeg.nodes import FilterableStream
from vidgear.gears import WriteGear
from gance import divisor
from gance.gance_types import ImageSourceType, RGBInt8ImageType
from gance.image_sources.image_sources_common import ImageResolution, image_resolution
from gance.logger_common import LOGGER
def _write_video(video_path: Path, audio: FilterableStream, output_path: Path) -> None:
"""
Adds an audio file to a video file. Copied from: https://stackoverflow.com/a/65547166
:param video_path: Path to the video file.
:param audio: The ffmpeg representation of the audio.
:param output_path: The path to write the new file to.
:return: None
"""
ffmpeg.run(
ffmpeg.output(
ffmpeg.input(str(video_path)).video, # get only video channel
audio, # get only audio channel
str(output_path),
vcodec="copy",
acodec="flac",
audio_bitrate=200,
strict="experimental",
),
quiet=True,
overwrite_output=True,
)
def _read_wav(audio_path: Path) -> FilterableStream:
"""
Read an audio file as an ffmpeg stream.
:param audio_path: Path to the audio file.
:return: The ffmpeg stream of the audio.
"""
return ffmpeg.input(str(audio_path)).audio
def add_wav_to_video(video_path: Path, audio_path: Path, output_path: Path) -> None:
"""
Adds an audio file to a video file. Copied from: https://stackoverflow.com/a/65547166
:param video_path: Path to the video file.
:param audio_path: Path to the audio file.
:param output_path: The path to write the new file to.
:return: None
"""
_write_video(video_path=video_path, audio=_read_wav(audio_path), output_path=output_path)
def add_wavs_to_video(video_path: Path, audio_paths: List[Path], output_path: Path) -> None:
"""
Adds an audio file to a video file. Copied from: https://stackoverflow.com/a/65547166
:param video_path: Path to the video file.
:param audio_paths: Paths to multiple audio files.
:param output_path: The path to write the new file to.
:return: None
"""
_write_video(
video_path=video_path,
audio=ffmpeg.concat(*[_read_wav(audio_path) for audio_path in audio_paths], v=0, a=1),
output_path=output_path,
)
class VideoOutputController(NamedTuple):
"""
Interface for something to write videos with.
Mimics `cv2.VideoWriter`.
"""
# Adds a frame to the video.
write: Callable[[RGBInt8ImageType], None]
# Finalizes the video, you should be able to open and read the video after calling this.
release: Callable[[], None]
def _create_video_writer_resolution(
video_path: Path, video_fps: float, resolution: ImageResolution, high_quality: bool
) -> VideoOutputController:
"""
Create a video writer of a given FPS and resolution.
:param video_path: Resulting file path.
:param video_fps: FPS of the video.
:param resolution: Size of the resulting video.
:param high_quality: If true, `ffmpeg` will be invoked directly via the VidGears library,
this will create a much larger, much higher quality output.
:return: The writer.
"""
if high_quality:
# Tries to best match YouTube's compression.
# See: https://video.stackexchange.com/a/24481
output_params = {
"-input_framerate": video_fps,
"-vf": f"yadif,scale={resolution.width}:{resolution.height}",
"-vcodec": "libx264",
"-crf": 18,
"-bf": 2,
"-use_editlist": 0,
"-movflags": "+faststart",
"-pix_fmt": "yuv422p",
}
ffmpeg_writer = WriteGear(
output_filename=str(video_path), compression_mode=True, **output_params
)
def write_frame(image: RGBInt8ImageType) -> None:
"""
Helper function to satisfy mypy.
:param image: To write.
:return: None
"""
try:
ffmpeg_writer.write(image)
except Exception as e:
LOGGER.exception(f"Ran into problem writing frame: {image}")
raise e
return VideoOutputController(
write=write_frame,
release=ffmpeg_writer.close,
)
else:
opencv_writer = cv2.VideoWriter(
str(video_path),
cv2.VideoWriter_fourcc(*"mp4v"),
video_fps,
(resolution.width, resolution.height),
)
def frame_writer(image: RGBInt8ImageType) -> None:
"""
Helper function to satisfy mypy.
Adds a check to make sure the input image matches the expected
resolution of the output. If this doesn't happen, openCV will
silently create a video without frames.
:param image: To write.
:return: None
"""
if image_resolution(image) != resolution:
raise ValueError("Incoming frame did not match output resolution!")
opencv_writer.write(image)
return VideoOutputController(write=frame_writer, release=opencv_writer.release)
def create_video_writer(
video_path: Path,
video_fps: float,
video_height: int,
num_squares_width: int,
num_squares_height: int = 1,
high_quality: bool = False,
) -> VideoOutputController:
"""
Helper function to configure the VideoWriter which writes frames to a video file.
:param video_path: Path to the file on disk.
:param video_fps: Desired FPS of the video.
:param video_height: Height of the video in pixels.
:param num_squares_width: Since each section of the video is a `video_height` x `video_height`
square this parameter sets the width for the video in pixels, with the number of these squares
that will be written in each frame.
:param num_squares_height: Like `num_squares_width`, but for height. Sets the height of
the video in units of `video_height`.
:param high_quality: Will be forwarded to library function.
:return: The openCV `VideoWriter` object.
"""
return _create_video_writer_resolution(
video_path=video_path,
video_fps=video_fps,
resolution=ImageResolution(
width=video_height * num_squares_width, height=video_height * num_squares_height
),
high_quality=high_quality,
)
class VideoFrames(NamedTuple):
"""
Contains metadata about the video, and an iterator that produces the frames.
"""
original_fps: float
total_frame_count: int
original_resolution: ImageResolution
frames: Iterator[RGBInt8ImageType]
def reduce_fps_take_every(original_fps: float, new_fps: float) -> Optional[int]:
"""
When reducing a video from a high FPS, to a lower FPS, you should take every nth (output)
frame of the input video.
TODO: Wording very rough here.
:param original_fps: Must be higher than `new_fps`.
:param new_fps: Must go evenly into `original_fps`.
:return: Take every n frames to get an evenly reduced output video.
"""
if new_fps is not None:
whole = divisor.divide_no_remainder(numerator=original_fps, denominator=new_fps)
if whole != 1:
return int(whole)
return None
def frames_in_video(
video_path: Path,
video_fps: Optional[float] = None,
reduce_fps_to: Optional[float] = None,
width_height: Optional[ImageResolution] = None,
) -> VideoFrames:
"""
Creates an interface to read each frame from a video into local memory for
analysis + manipulation.
:param video_path: The path to the video file on disk.
:param video_fps: Can be used to override the actual FPS of the video.
:param reduce_fps_to: Discards frames such that the frames that are returned are at this
FPS.
:param width_height: If given, the output frames will be resized to this resolution.
:return: An NT containing metadata about the video, and an iterator that produces the frames.
Frames are in RGB color order.
:raises: ValueError if the video can't be opened, or the given `reduce_fps_to` is impossible.
"""
vid_capture = cv2.VideoCapture(str(video_path))
file_fps = float(vid_capture.get(cv2.CAP_PROP_FPS))
if video_fps:
if video_fps != file_fps:
LOGGER.warning(
f"Override FPS of: {video_fps} fps "
f"did not match the fps from the file of: {file_fps} fps. "
f"Projected frames will not line up exactly."
)
fps = video_fps
else:
fps = file_fps
take_every = reduce_fps_take_every(original_fps=fps, new_fps=reduce_fps_to)
original_width_height = ImageResolution(
int(vid_capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(vid_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)),
)
resize = (original_width_height != width_height) if width_height is not None else False
if not vid_capture.isOpened():
raise ValueError(f"Couldn't open video file: {video_path}")
def frames() -> Iterator[RGBInt8ImageType]:
"""
Read frames off of the video capture until there none left or pulling a frame fails.
:return: An iterator of frames.
"""
while vid_capture.isOpened():
ret, frame = vid_capture.read()
if ret:
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
output = (
image
if not resize
else cv2.resize(image, (width_height.width, width_height.height))
)
yield output
else:
break
return VideoFrames(
original_fps=vid_capture.get(cv2.CAP_PROP_FPS),
original_resolution=original_width_height,
total_frame_count=int(vid_capture.get(cv2.CAP_PROP_FRAME_COUNT)),
frames=itertools.islice(frames(), None, None, take_every),
)
def write_source_to_disk_forward(
source: ImageSourceType,
video_path: Path,
video_fps: float,
audio_paths: Optional[List[Path]] = None,
high_quality: bool = False,
) -> ImageSourceType:
"""
Consume an image source, write it out to disk.
:param source: To write to disk.
:param video_path: Output video path.
:param video_fps: Frames/Second of the output video.
:param audio_paths: If given, the audio files will be written to the output video.
:param high_quality: Flag will be forwarded to library function.
:return: None
"""
def setup_iteration(output_path: Path) -> ImageSourceType:
"""
Helper function to set up the output and forwarding operation.
:param output_path: Intermediate video path.
:return: The frames to yield.
"""
try:
first_frame = next(source)
except StopIteration:
LOGGER.error("Frame source was empty, nothing to write to disk.")
raise
writer = _create_video_writer_resolution(
video_path=output_path,
video_fps=video_fps,
resolution=image_resolution(first_frame),
high_quality=high_quality,
)
def write_frame(frame: RGBInt8ImageType) -> None:
"""
Write the given frame to the file.
:param frame: To write.
:return: None
"""
writer.write(cv2.cvtColor(frame.astype(np.uint8), cv2.COLOR_BGR2RGB))
for index, image in enumerate(itertools.chain([first_frame], source)):
LOGGER.info(f"Writing frame #{index} to file: {video_path}")
write_frame(image)
yield image
writer.release()
try:
if audio_paths is None:
yield from setup_iteration(video_path)
else:
# Don't write the video-only file to disk at the output path, instead
with NamedTemporaryFile(suffix=video_path.suffix) as file:
temp_video_path = Path(file.name)
yield from setup_iteration(temp_video_path)
file.flush()
LOGGER.info(f"Finalizing {video_path}")
add_wavs_to_video(
video_path=temp_video_path, audio_paths=audio_paths, output_path=video_path
)
except Exception as e:
LOGGER.exception(f"Ran into an exception writing out a video: {pprint.pformat(e)}")
raise e
def write_source_to_disk_consume(
source: ImageSourceType,
video_path: Path,
video_fps: float,
audio_paths: Optional[List[Path]] = None,
high_quality: bool = False,
) -> None:
"""
Consume an image source, write it out to disk.
:param source: To write to disk.
:param video_path: Output video path.
:param video_fps: FPS of the output video.
:param audio_paths: If given, the audio file at this path will be written to the output video.
:param high_quality: Flag will be forwarded to library function.
:return: None
"""
more_itertools.consume(
write_source_to_disk_forward(
source=source,
video_path=video_path,
video_fps=video_fps,
audio_paths=audio_paths,
high_quality=high_quality,
)
)
def resize_source(source: ImageSourceType, resolution: ImageResolution) -> ImageSourceType:
"""
For each item in the input source, scale it to the given resolution.
:param source: Contains Images.
:param resolution: Desired resolution.
:return: A new source of scaled images.
"""
def resize_image(image: RGBInt8ImageType) -> RGBInt8ImageType:
"""
Resizes an image to the input resolution.
Uses, `cv2.INTER_CUBIC`, which is visually good-looking but somewhat slow.
May want to be able to pass this in.
:param image: To scale.
:return: Scaled image.
"""
output: RGBInt8ImageType = cv2.resize(
image, (resolution.height, resolution.width), interpolation=cv2.INTER_CUBIC
)
# The image in `source` has now been 'consumed', and can't be used again.
# We delete this frame here to avoid memory leaks.
# Not really sure if this is needed, but it shouldn't cause harm.
del image
# The scaled image.
return output
yield from (
resize_image(image) if image_resolution(image) != resolution else image for image in source
)
def scale_square_source_duplicate(
source: ImageSourceType, output_side_length: int, frame_multiplier: int = 1
) -> ImageSourceType:
"""
Scale the resolution and number of frames in a given source.
:param source: To scale.
:param output_side_length: Square frames will be resized to this side length.
:param frame_multiplier: Every frame will be duplicated this many times.
:return: Scaled source.
"""
resized = resize_source(source, ImageResolution(output_side_length, output_side_length))
return (
cast(
ImageSourceType,
more_itertools.repeat_each(
resized,
frame_multiplier,
),
)
if frame_multiplier != 1
else resized
)