Skip to content

Commit b5a3dad

Browse files
committed
Added option to duplicate frames instead of inserting black ones.
1 parent 78f63a3 commit b5a3dad

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

PythonTools/video.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,21 @@ def extract_frames_ffmpeg(video_file: str, timestamps_df: pd.DataFrame, output_d
135135
ffmpeg_read_process = None
136136

137137

138-
def rebuild_video(dir: Path, frames: pd.DataFrame, video_info: VideoInfo, outfile: Path) -> None:
138+
def rebuild_video(dir: Path, frames: pd.DataFrame, video_info: VideoInfo, outfile: Path, duplicate_last: bool = False)\
139+
-> None:
140+
141+
"""
142+
Given the directory containing unpacked frames and the dataframe, rebuilds a new video at teh given path.
143+
:param dir: The directory containing the single frames. Each frame with name specified in the frames dadaframe
144+
:param frames: A dataframe with two columns: `timestamp` is the name of the frame to append to the video,
145+
and `generated` can be either `Original` (file should be loaded) or `Generated` (insert a black frame or duplicate last).
146+
:param video_info: Anticipated info regarding the resolution of the output video.
147+
Resolution must match the resolution of input frame images.
148+
:param outfile: Path to the video output file.
149+
:param duplicate_last: If False (default) a black frame is inserted whenever a `Generated` frame is found.
150+
When True, the last valid frame is duplicated.
151+
:return: Nothing
152+
"""
139153

140154
# Extract the vido information.
141155
frame_width = video_info.width
@@ -164,6 +178,10 @@ def rebuild_video(dir: Path, frames: pd.DataFrame, video_info: VideoInfo, outfil
164178

165179
assert frame_width is not None and frame_height is not None and ffmpeg_video_out_process is not None
166180

181+
# Initialize data for "Generated" frames
182+
black_frame = np.zeros((frame_height, frame_width, 3), dtype=np.uint8)
183+
last_frame = black_frame
184+
167185
#
168186
# Cycle through all the frames.
169187
for idx, row in frames.iterrows():
@@ -190,12 +208,18 @@ def rebuild_video(dir: Path, frames: pd.DataFrame, video_info: VideoInfo, outfil
190208
# Send the frame to the ffmpeg process
191209
ffmpeg_video_out_process.stdin.write(img.tobytes())
192210

211+
last_frame = img
212+
193213
elif gen == "Generated":
194214

195-
# Create an artificial black frame
196-
print(f"Injecting Black frame at idx {idx}")
197-
black_frame = np.zeros((frame_height, frame_width, 3), dtype=np.uint8)
198-
ffmpeg_video_out_process.stdin.write(black_frame.tobytes())
215+
if duplicate_last:
216+
pass
217+
print("Duplicating last valid frame")
218+
ffmpeg_video_out_process.stdin.write(last_frame.tobytes())
219+
else:
220+
# Create an artificial black frame
221+
print(f"Injecting Black frame at idx {idx}")
222+
ffmpeg_video_out_process.stdin.write(black_frame.tobytes())
199223

200224
else:
201225
raise Exception(f"Unexpected value '{gen}' in column 'generated' at index {idx}")

0 commit comments

Comments
 (0)