-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_common.py
306 lines (257 loc) · 10.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
"""
Common functionality for dealing with video files.
"""
import itertools
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Iterator, List, NamedTuple, Optional, Tuple
import ffmpeg
import more_itertools
import numpy as np
from cv2 import cv2
from ffmpeg.nodes import FilterableStream
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="aac",
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,
)
def _create_video_writer_resolution(
video_path: Path, video_fps: float, resolution: ImageResolution
) -> cv2.VideoWriter:
"""
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.
:return: The writer.
"""
return cv2.VideoWriter(
str(video_path),
cv2.VideoWriter_fourcc(*"mp4v"),
video_fps,
(resolution.width, resolution.height),
)
def create_video_writer(
video_path: Path,
video_fps: float,
video_height: int,
num_squares_width: int,
num_squares_height: int = 1,
) -> cv2.VideoWriter:
"""
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`.
: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
),
)
class VideoFrames(NamedTuple):
"""
Contains metadata about the video, and an iterator that produces the frames.
"""
original_fps: float
total_frame_count: int
original_resolution: Tuple[int, int]
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[Tuple[int, int]] = 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 = (
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)
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,
) -> 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.
: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)
)
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()
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
)
def write_source_to_disk_consume(
source: ImageSourceType,
video_path: Path,
video_fps: float,
audio_paths: Optional[List[Path]] = None,
) -> 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.
:return: None
"""
more_itertools.consume(
write_source_to_disk_forward(
source=source, video_path=video_path, video_fps=video_fps, audio_paths=audio_paths
)
)