Skip to content

[bugfix] Keep save-images last image inside its scene - #608

Merged
Breakthrough merged 3 commits into
Breakthrough:mainfrom
EnesSoydan:fix/save-images-last-frame-margin
Nov 18, 2026
Merged

Breakthrough merged 3 commits into
Breakthrough:mainfrom
EnesSoydan:fix/save-images-last-frame-margin

Conversation

@EnesSoydan

Copy link
Copy Markdown
Contributor

Fixes #607

Scene end times are exclusive, but _generate_timecode_list() took the last image of each scene at end - frame_margin. So since v0.7:

  • frame_margin=0 saves the first frame of the next scene as the last image (a frame that doesn't exist for the last scene of a video)
  • frame_margin=1 (default) saves the last frame of the scene, with no margin at the end

This counts the margin from the last frame of the scene, as v0.6 did, and clamps every image to that frame. The clamp covers very short scenes, where the midpoint could round up into the next scene (a one-frame scene (11, 12) saved frame 12).

Frames saved for a scene covering frames 0-99 with num_images=3:

frame_margin v0.6.7 before after
1 1, 50, 98 1, 50, 99 1, 50, 98
0 0, 50, 99 0, 50, 100 0, 50, 99

Note that this changes the output for the default margin too: the last image moves one frame earlier, back to where v0.6 put it.

I left the frame_margin docstring alone. It still says a margin of 0 may fail to extract the very last frame of some videos, which I couldn't check here.

Tested on Windows with Python 3.10 and the OpenCV backend. The new tests fail without the change. The full suite passes except test_write_scene_list_ffmetadata_ffmpeg_roundtrip, which needs ffmpeg and I don't have it installed.

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.

@Breakthrough Breakthrough left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, good find.

Comment thread scenedetect/output/image.py Resolved
EnesSoydan and others added 2 commits November 17, 2026 16:24
Scenes can be shorter than one frame (e.g. VFR), in which case end - 1 frame falls before the scene start.

@Breakthrough Breakthrough left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

@Breakthrough
Breakthrough merged commit 8a0b5ce into Breakthrough:main Nov 18, 2026
23 checks passed
MuhammadBilalKhan267 added a commit to MuhammadBilalKhan267/PySceneDetect that referenced this pull request Nov 22, 2026
- Preserve FramePosition and exact Timecode arithmetic
- Restore frame-rate-based image margins and port the Breakthrough#608 clamp
- Retain upstream weight validation and formatter regression tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

save_images: last image of a scene is taken from the next scene with frame_margin=0

2 participants