Skip to content

Commit 3fc1cb3

Browse files
rmasseikostrykin
andauthored
minor code fixes for better input and error handling
Co-authored-by: Leonid Kostrykin <void@evoid.de>
1 parent 4f011e1 commit 3fc1cb3

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

tools/cv2/cv2_extract_frames.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ def extract_frames(output_dir: Union[str, Path], video_path: Union[str, Path], s
3737
frame_count = video.get(cv2.CAP_PROP_FRAME_COUNT)
3838
print('Total frames:', frame_count)
3939

40-
start_frame = int(start_time * fps)
41-
end_frame = int(end_time * fps)
42-
print(f'Starting extracting from frame {start_frame} until {end_frame}...')
40+
start_frame = round(start_time * fps)
41+
end_frame = round(end_time * fps)
42+
if end_frame >= frame_count:
43+
exit("End frame is beyond the end of the sequence.")
44+
end_frame %= frame_count
45+
if start_frame > end_frame:
46+
exit("Start frame is beyond the end frame.")
47+
print(f'Starting extracting from frame {start_frame} until {end_frame}...')
4348

4449
video.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
4550
current_frame = start_frame
@@ -62,7 +67,7 @@ def extract_frames(output_dir: Union[str, Path], video_path: Union[str, Path], s
6267
print('Extraction was successfully executed. Enjoy your frames.')
6368

6469
except IOError:
65-
print("Cannot open video file")
70+
exit("Cannot open video file.")
6671

6772

6873
if __name__ == "__main__":

tools/cv2/cv2_extract_frames.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</command>
3737
<inputs>
3838
<param name="video_path" type="data" optional="False" format="mp4,avi" label="Input video to convert"/>
39-
<param name="start_time" type="float" value= "0" optional="False" label="Start time (seconds)" help="Start time in seconds"/>
39+
<param name="start_time" type="float" value= "0" min="0" optional="False" label="Start time (seconds)" help="Start time in seconds"/>
4040
<param name="end_time" type="float" value= "-1" optional="False" label="End time (seconds)" help="End time in seconds"/>
4141
<param name="convert_to_grey" type="boolean" label="Convert output to single-channel grayscale?" help="Convert the file to grayscale (will be RGB otherwise)."/>
4242
</inputs>

0 commit comments

Comments
 (0)