From 85407a05437a0155f5b0945ffb3a99f6615c5148 Mon Sep 17 00:00:00 2001 From: EnesSoydan <191651630+EnesSoydan@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:32:51 +0300 Subject: [PATCH 1/2] [bugfix] Keep save-images last image inside its scene Scene end times are exclusive, but the last image of each scene was taken at end - frame_margin, so with the default margin of 1 it was the last frame of the scene (no margin) and with 0 it was the first frame of the next scene. Count the margin from the last frame of the scene as v0.6 did, and clamp images of very short scenes to that frame. --- scenedetect/output/image.py | 7 +++++-- tests/test_output.py | 38 +++++++++++++++++++++++++++++++++++++ website/pages/changelog.md | 1 + 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/scenedetect/output/image.py b/scenedetect/output/image.py index 94c894ce..5716d483 100644 --- a/scenedetect/output/image.py +++ b/scenedetect/output/image.py @@ -83,6 +83,7 @@ def _generate_timecode_list( frame_rate = scene_list[0][0].frame_rate assert frame_rate is not None margin_secs = FrameTimecode(timecode=frame_margin, fps=frame_rate).seconds + frame_secs = float(1 / frame_rate) result = [] for (start, end), num_images in zip(scene_list, num_images_per_scene, strict=True): duration_secs = (end - start).seconds @@ -90,6 +91,8 @@ def _generate_timecode_list( result.append([start] * num_images) continue segment_secs = duration_secs / num_images + # `end` is exclusive, so the last frame of the scene is one frame before it. + last_frame_secs = end.seconds - frame_secs timecodes = [] for j in range(num_images): seg_start = start.seconds + j * segment_secs @@ -99,10 +102,10 @@ def _generate_timecode_list( elif j == 0: t = min(seg_start + margin_secs, seg_end) elif j == num_images - 1: - t = max(seg_end - margin_secs, seg_start) + t = max(last_frame_secs - margin_secs, seg_start) else: t = (seg_start + seg_end) / 2.0 - timecodes.append(FrameTimecode(t, fps=frame_rate)) + timecodes.append(FrameTimecode(min(t, last_frame_secs), fps=frame_rate)) result.append(timecodes) return result diff --git a/tests/test_output.py b/tests/test_output.py index 07a3afda..2852d095 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -204,6 +204,44 @@ def test_save_images_frame_margin_accepts_time_values( assert tmp_path.joinpath(path).exists() +@pytest.mark.parametrize(("frame_margin", "expected"), [(0, [0, 50, 99]), (1, [1, 50, 98])]) +def test_save_images_frame_margin_stays_in_scene( + test_video_file, tmp_path: Path, frame_margin, expected +): + """Scene end times are exclusive, so `frame_margin` must be applied from the last frame of the + scene rather than from the first frame of the next one.""" + video = VideoStreamCv2(test_video_file) + video_fps = video.frame_rate + scene_list = [ + (FrameTimecode(start, video_fps), FrameTimecode(end, video_fps)) + for start, end in [(0, 100), (100, 200)] + ] + image_filenames = save_images( + scene_list=scene_list, + output_dir=tmp_path, + video=video, + num_images=3, + image_name_template="$SCENE_NUMBER.$IMAGE_NUMBER.$FRAME_NUMBER", + frame_margin=frame_margin, + ) + frames = [[int(Path(path).stem.split(".")[-1]) for path in image_filenames[i]] for i in (0, 1)] + assert frames == [expected, [frame + 100 for frame in expected]] + + +def test_save_images_single_frame_scene_stays_in_scene(test_video_file, tmp_path: Path): + """The image for a one-frame scene must be that frame, not the first frame of the next scene.""" + video = VideoStreamCv2(test_video_file) + scene_list = [(FrameTimecode(11, video.frame_rate), FrameTimecode(12, video.frame_rate))] + image_filenames = save_images( + scene_list=scene_list, + output_dir=tmp_path, + video=video, + num_images=1, + image_name_template="$SCENE_NUMBER.$FRAME_NUMBER", + ) + assert image_filenames == {0: ["001.11.jpg"]} + + def test_save_images_rejects_negative_margin(test_video_file, tmp_path: Path): video = VideoStreamCv2(test_video_file) scene_list = [(FrameTimecode(0, video.frame_rate), FrameTimecode(10, video.frame_rate))] diff --git a/website/pages/changelog.md b/website/pages/changelog.md index 2589b7e7..2d9cd36d 100644 --- a/website/pages/changelog.md +++ b/website/pages/changelog.md @@ -863,3 +863,4 @@ Development - [feature] Add `--stdin` to read the input video from a pipe, e.g. `ffmpeg -i input.mp4 -c copy -f matroska - | scenedetect --stdin detect-content list-scenes`. Requires the PyAV backend. `save-html` omits images, and commands that need to seek or refer to a source file (`split-video`, `save-images`, `save-fcp`, `save-otio`, `load-scenes`, `time -s`) are not supported [#189](https://github.com/Breakthrough/PySceneDetect/issues/189) [#434](https://github.com/Breakthrough/PySceneDetect/issues/434) - [bugfix] The CLI now exits with 1 when no frames could be read from the input video, or seeking to the `time -s`/`--start` time fails, instead of 0 - [bugfix] `--kernel-size`/`-k` values the detectors cannot use (even numbers, 1, 0 and negatives other than -1) now give a usage error instead of a traceback, and `-k -1` now means auto, like the config file. A config file with `kernel-size = 1` is now an error [#604](https://github.com/Breakthrough/PySceneDetect/issues/604) + - [bugfix] `save-images` no longer takes the last image of a scene from the first frame of the next scene. `--frame-margin` is counted from the last frame of the scene again, as in v0.6 (`-m 0` now selects the last frame itself) From 7362973c4ffddeca706a027361133b4efded5c07 Mon Sep 17 00:00:00 2001 From: EnesSoydan <191651630+EnesSoydan@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:24:16 +0300 Subject: [PATCH 2/2] [bugfix] Clamp save-images last frame to scene start Scenes can be shorter than one frame (e.g. VFR), in which case end - 1 frame falls before the scene start. --- scenedetect/output/image.py | 5 +++-- tests/test_output.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/scenedetect/output/image.py b/scenedetect/output/image.py index 5716d483..b4bdbbb2 100644 --- a/scenedetect/output/image.py +++ b/scenedetect/output/image.py @@ -91,8 +91,9 @@ def _generate_timecode_list( result.append([start] * num_images) continue segment_secs = duration_secs / num_images - # `end` is exclusive, so the last frame of the scene is one frame before it. - last_frame_secs = end.seconds - frame_secs + # `end` is exclusive, so the last frame of the scene is one frame before it. Scenes can be + # shorter than one frame (e.g. VFR), so never go before the start of the scene. + last_frame_secs = max(end.seconds - frame_secs, start.seconds) timecodes = [] for j in range(num_images): seg_start = start.seconds + j * segment_secs diff --git a/tests/test_output.py b/tests/test_output.py index 2852d095..4c4ee220 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -242,6 +242,22 @@ def test_save_images_single_frame_scene_stays_in_scene(test_video_file, tmp_path assert image_filenames == {0: ["001.11.jpg"]} +def test_save_images_sub_frame_scene_stays_in_scene(test_video_file, tmp_path: Path): + """A scene shorter than one frame (e.g. with VFR timestamps) must not yield an image from + before the scene starts.""" + video = VideoStreamCv2(test_video_file) + start = FrameTimecode(0.5, video.frame_rate) + end = FrameTimecode(0.51, video.frame_rate) + image_filenames = save_images( + scene_list=[(start, end)], + output_dir=tmp_path, + video=video, + num_images=1, + image_name_template="$SCENE_NUMBER.$FRAME_NUMBER", + ) + assert image_filenames == {0: [f"001.{start.frame_num}.jpg"]} + + def test_save_images_rejects_negative_margin(test_video_file, tmp_path: Path): video = VideoStreamCv2(test_video_file) scene_list = [(FrameTimecode(0, video.frame_rate), FrameTimecode(10, video.frame_rate))]