Skip to content

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

Open

Description

@EnesSoydan

Description

Since v0.7, save_images() / save-images picks the last image of each scene one frame later than v0.6 did. Scene end times are exclusive, but the last image is taken at end - frame_margin:

  • frame_margin=0: the last image is frame end, which is the first frame of the next scene. For the last scene of a video that frame doesn't exist.
  • frame_margin=1 (default): the last image is the last frame of the scene, so there is no margin at the end, while the first image does skip one frame.

In v0.6.7 the margin was counted from the last frame of the scene (end - 1).

Example

import tempfile
from scenedetect import FrameTimecode, open_video
from scenedetect.output import save_images  # scenedetect.scene_manager in v0.6

video = open_video("tests/resources/testvideo.mp4")
fps = video.frame_rate
scenes = [
    (FrameTimecode(0, fps), FrameTimecode(100, fps)),
    (FrameTimecode(100, fps), FrameTimecode(200, fps)),
]
for margin in (1, 0):
    with tempfile.TemporaryDirectory() as d:
        print(margin, save_images(scenes, video, num_images=3, frame_margin=margin,
                                  output_dir=d, image_name_template="s$SCENE_NUMBER-f$FRAME_NUMBER"))

Frame numbers of the three images of the first scene (frames 0-99):

frame_margin v0.6.7 main (47f95bd)
1 1, 50, 98 1, 50, 99
0 0, 50, 99 0, 50, 100

With frame_margin=0 the last image of scene 1 and the first image of scene 2 are the same frame (100).

A one-frame scene can also get its image from the next scene, because the midpoint is rounded up: (11, 12) with num_images=1 saves frame 12.

The cause is in _generate_timecode_list() in scenedetect/output/image.py: t = max(seg_end - margin_secs, seg_start) uses the exclusive end. Subtracting one frame duration there, and clamping to the last frame of the scene, gives the v0.6 results again. I have a fix with tests ready and can open a PR.

Environment

PySceneDetect main (47f95bd) and 0.7.3, Windows 11, Python 3.10, OpenCV backend. Compared against 0.6.7 from PyPI.

Media/Files

tests/resources/testvideo.mp4 from the resources branch.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions