Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions scenedetect/output/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,17 @@ 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
if duration_secs <= 0:
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. 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
Expand All @@ -99,10 +103,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

Expand Down
54 changes: 54 additions & 0 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,60 @@ 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_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))]
Expand Down
1 change: 1 addition & 0 deletions website/pages/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -863,4 +863,5 @@ 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)
- [bugfix] Out of range values for `detect-adaptive --frame-window` (below 1) and `save-images` `--num-images` (below 1), `--scale`, `--height` and `--width` (negative) now give a usage error instead of a traceback, and the same values are rejected in a config file [#609](https://github.com/Breakthrough/PySceneDetect/issues/609)
Loading