From 83c3a05a183a766ea9a9b8939e9c235ec3ee0392 Mon Sep 17 00:00:00 2001 From: Christoph Deil Date: Fri, 7 Nov 2025 17:55:11 +0100 Subject: [PATCH 1/3] Fix process_video and add test case --- supervision/utils/video.py | 15 +++++---------- test/utils/test_video.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 test/utils/test_video.py diff --git a/supervision/utils/video.py b/supervision/utils/video.py index 3b281b4e2..0623e1193 100644 --- a/supervision/utils/video.py +++ b/supervision/utils/video.py @@ -230,29 +230,24 @@ def callback(scene: np.ndarray, index: int) -> np.ndarray: ``` """ source_video_info = VideoInfo.from_video_path(video_path=source_path) + max_frames = max_frames or source_video_info.total_frames + if source_video_info.total_frames is not None and max_frames is not None: + max_frames = min(max_frames, source_video_info.total_frames) + video_frames_generator = get_video_frames_generator( source_path=source_path, end=max_frames ) with VideoSink(target_path=target_path, video_info=source_video_info) as sink: - total_frames = ( - min(source_video_info.total_frames, max_frames) - if max_frames is not None - else source_video_info.total_frames - ) for index, frame in enumerate( tqdm( video_frames_generator, - total=total_frames, + total=max_frames, disable=not show_progress, desc=progress_message, ) ): result_frame = callback(frame, index) sink.write_frame(frame=result_frame) - else: - for index, frame in enumerate(video_frames_generator): - result_frame = callback(frame, index) - sink.write_frame(frame=result_frame) class FPSMonitor: diff --git a/test/utils/test_video.py b/test/utils/test_video.py new file mode 100644 index 000000000..21bd66f54 --- /dev/null +++ b/test/utils/test_video.py @@ -0,0 +1,32 @@ +import tempfile + +import cv2 +import numpy as np + +from supervision.utils.video import process_video + + +def create_test_video(path, num_frames, width=20, height=10): + fourcc = cv2.VideoWriter_fourcc(*"mp4v") + out = cv2.VideoWriter(path, fourcc, 1.0, (width, height)) + + for _ in range(num_frames): + frame = np.zeros((height, width, 3), dtype=np.uint8) + out.write(frame) + + out.release() + + +def test_process_video_max_frames_exceeds_total_frames(): + with ( + tempfile.NamedTemporaryFile(suffix=".mp4") as source_file, + tempfile.NamedTemporaryFile(suffix=".mp4") as target_file, + ): + create_test_video(source_file.name, num_frames=5) + + process_video( + source_path=source_file.name, + target_path=target_file.name, + callback=lambda frame, _: frame, + max_frames=10, + ) From d91cf18db728a131dc66bc5ccd39f51b9a695edc Mon Sep 17 00:00:00 2001 From: Christoph Deil Date: Fri, 7 Nov 2025 18:11:58 +0100 Subject: [PATCH 2/3] Simplify test_video.py temp file, itsa OK on Windows letsa hope --- test/utils/test_video.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/test/utils/test_video.py b/test/utils/test_video.py index 21bd66f54..4489892e5 100644 --- a/test/utils/test_video.py +++ b/test/utils/test_video.py @@ -1,7 +1,6 @@ -import tempfile - import cv2 import numpy as np +import pytest from supervision.utils.video import process_video @@ -17,16 +16,15 @@ def create_test_video(path, num_frames, width=20, height=10): out.release() -def test_process_video_max_frames_exceeds_total_frames(): - with ( - tempfile.NamedTemporaryFile(suffix=".mp4") as source_file, - tempfile.NamedTemporaryFile(suffix=".mp4") as target_file, - ): - create_test_video(source_file.name, num_frames=5) +def test_process_video_max_frames_exceeds_total_frames(tmp_path): + source_path = tmp_path / "source.mp4" + target_path = tmp_path / "target.mp4" + + create_test_video(str(source_path), num_frames=5) - process_video( - source_path=source_file.name, - target_path=target_file.name, - callback=lambda frame, _: frame, - max_frames=10, - ) + process_video( + source_path=str(source_path), + target_path=str(target_path), + callback=lambda frame, _: frame, + max_frames=10, + ) From 05005491db42e2069a1d7ba7fc7dc268573350aa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 7 Nov 2025 17:12:21 +0000 Subject: [PATCH 3/3] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/utils/test_video.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/utils/test_video.py b/test/utils/test_video.py index 4489892e5..1343154f1 100644 --- a/test/utils/test_video.py +++ b/test/utils/test_video.py @@ -1,6 +1,5 @@ import cv2 import numpy as np -import pytest from supervision.utils.video import process_video