@@ -135,7 +135,21 @@ def extract_frames_ffmpeg(video_file: str, timestamps_df: pd.DataFrame, output_d
135
135
ffmpeg_read_process = None
136
136
137
137
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
+ """
139
153
140
154
# Extract the vido information.
141
155
frame_width = video_info .width
@@ -164,6 +178,10 @@ def rebuild_video(dir: Path, frames: pd.DataFrame, video_info: VideoInfo, outfil
164
178
165
179
assert frame_width is not None and frame_height is not None and ffmpeg_video_out_process is not None
166
180
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
+
167
185
#
168
186
# Cycle through all the frames.
169
187
for idx , row in frames .iterrows ():
@@ -190,12 +208,18 @@ def rebuild_video(dir: Path, frames: pd.DataFrame, video_info: VideoInfo, outfil
190
208
# Send the frame to the ffmpeg process
191
209
ffmpeg_video_out_process .stdin .write (img .tobytes ())
192
210
211
+ last_frame = img
212
+
193
213
elif gen == "Generated" :
194
214
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 ())
199
223
200
224
else :
201
225
raise Exception (f"Unexpected value '{ gen } ' in column 'generated' at index { idx } " )
0 commit comments