From 8234965ee8711394ccc47cadde8754b17cb34ee9 Mon Sep 17 00:00:00 2001 From: Nguyen Van Nam Date: Thu, 23 Jul 2026 21:16:38 +0700 Subject: [PATCH] fix: clamp video frame seek index (#1790) Prevent get_video_frame() from seeking to invalid frame positions. The default frame_number=0 now resolves to the first frame instead of -1, and oversized frame requests clamp to the final valid frame instead of seeking past the end. Empty or invalid videos now return None safely after releasing the capture. Affected files: capturer.py Signed-off-by: Nguyen Van Nam --- modules/capturer.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/capturer.py b/modules/capturer.py index 1dee22d..5ef149a 100644 --- a/modules/capturer.py +++ b/modules/capturer.py @@ -14,8 +14,13 @@ def get_video_frame(video_path: str, frame_number: int = 0) -> Any: if modules.globals.color_correction: capture.set(cv2.CAP_PROP_CONVERT_RGB, 1) - frame_total = capture.get(cv2.CAP_PROP_FRAME_COUNT) - capture.set(cv2.CAP_PROP_POS_FRAMES, min(frame_total, frame_number - 1)) + frame_total = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) + if frame_total <= 0: + capture.release() + return None + + target_index = 0 if frame_number <= 1 else min(frame_total - 1, frame_number - 1) + capture.set(cv2.CAP_PROP_POS_FRAMES, target_index) has_frame, frame = capture.read() if has_frame and modules.globals.color_correction: