-
Notifications
You must be signed in to change notification settings - Fork 519
Add a minimum fade-out duration to ThresholdDetector #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aprylewu
wants to merge
2
commits into
Breakthrough:main
Choose a base branch
from
aprylewu:feat/threshold-min-out-length
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| # | ||
| # PySceneDetect: Python-Based Video Scene Detector | ||
| # ------------------------------------------------------------------- | ||
| # [ Site: https://scenedetect.com ] | ||
| # [ Docs: https://scenedetect.com/docs/ ] | ||
| # [ Github: https://github.com/Breakthrough/PySceneDetect/ ] | ||
| # | ||
| # Copyright (C) 2026 Brandon Castellano <http://www.bcastell.com>. | ||
| # PySceneDetect is licensed under the BSD 3-Clause License; see the | ||
| # included LICENSE file, or visit one of the above pages for details. | ||
| # | ||
| """Duration filtering for threshold-based fades.""" | ||
|
|
||
| import csv | ||
|
|
||
| import cv2 | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from scenedetect import FrameTimecode, SceneManager, open_video | ||
| from scenedetect.detectors import ThresholdDetector | ||
| from tests.helpers import invoke_cli | ||
|
|
||
|
|
||
| def _detect_levels(levels, **kwargs): | ||
| detector = ThresholdDetector(**kwargs) | ||
| cuts = [] | ||
| for index, level in enumerate(levels): | ||
| timecode = FrameTimecode(index, fps=10.0) | ||
| cuts.extend(detector.process_frame(timecode, np.full((4, 4, 3), level, np.uint8))) | ||
| cuts.extend(detector.post_process(timecode)) | ||
| return [cut.frame_num for cut in cuts] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("method", list(ThresholdDetector.Method)) | ||
| @pytest.mark.parametrize("fade_bias, expected", [(-1.0, 22), (0.0, 25), (1.0, 28)]) | ||
| def test_short_fade_does_not_affect_next_cut(method, fade_bias, expected): | ||
| levels = [128] * 10 + [0] * 2 + [128] * 10 + [0] * 6 + [128] * 10 | ||
| if method == ThresholdDetector.Method.CEILING: | ||
| levels = [255 - level for level in levels] | ||
| assert _detect_levels( | ||
| levels, | ||
| threshold=128, | ||
| method=method, | ||
| min_scene_len=3, | ||
| min_out_length=4, | ||
| fade_bias=fade_bias, | ||
| ) == [expected] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("out_length, expected", [(3, []), (4, [12]), (5, [12])]) | ||
| @pytest.mark.parametrize("minimum", [4, 0.4, "0.4s", "00:00:00.400", FrameTimecode(4, 10.0)]) | ||
| def test_min_out_length_boundary(out_length, expected, minimum): | ||
| assert ( | ||
| _detect_levels( | ||
| [128] * 10 + [0] * out_length + [128] * 10, | ||
| min_scene_len=0, | ||
| min_out_length=minimum, | ||
| ) | ||
| == expected | ||
| ) | ||
|
|
||
|
|
||
| def test_min_scene_len_still_applies(): | ||
| levels = [128] * 10 + [0] * 4 + [128] * 3 + [0] * 4 + [128] * 10 | ||
| assert _detect_levels(levels, min_scene_len=10, min_out_length=4) == [12] | ||
| assert _detect_levels(levels, min_scene_len=0, min_out_length=4) == [12, 19] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("add_final_scene", [False, True]) | ||
| @pytest.mark.parametrize("out_length", [3, 4]) | ||
| def test_final_fade_counts_last_frame(out_length, add_final_scene): | ||
| assert _detect_levels( | ||
| [128] * 10 + [0] * out_length, | ||
| min_scene_len=0, | ||
| min_out_length=4, | ||
| add_final_scene=add_final_scene, | ||
| ) == ([10] if add_final_scene and out_length == 4 else []) | ||
|
|
||
|
|
||
| def test_default_keeps_short_fades(): | ||
| levels = [128] * 10 + [0] * 2 + [128] * 10 + [0] * 3 | ||
| assert _detect_levels(levels, min_scene_len=0, add_final_scene=True) == [11, 22] | ||
| assert _detect_levels(levels, min_scene_len=0, add_final_scene=True, min_out_length=0) == [ | ||
| 11, | ||
| 22, | ||
| ] | ||
|
|
||
|
|
||
| def _write_video(path, levels): | ||
| writer = cv2.VideoWriter(str(path), cv2.VideoWriter.fourcc(*"MJPG"), 10, (64, 48)) | ||
| assert writer.isOpened() | ||
| try: | ||
| for level in levels: | ||
| writer.write(np.full((48, 64, 3), level, np.uint8)) | ||
| finally: | ||
| writer.release() | ||
| return str(path) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("backend", ["opencv", "pyav"]) | ||
| @pytest.mark.parametrize("final_out_length", [3, 4]) | ||
| def test_min_out_length_decoded_video(tmp_path, auto_close, backend, final_out_length): | ||
| if backend == "pyav": | ||
| pytest.importorskip("av") | ||
| levels = [128] * 10 + [0] * 2 + [128] * 10 + [0] * 6 + [128] * 10 | ||
| path = _write_video(tmp_path / "fades.avi", levels + [0] * final_out_length) | ||
| video = auto_close(open_video(path, backend=backend)) | ||
| manager = SceneManager() | ||
| manager.add_detector( | ||
| ThresholdDetector(min_scene_len=0, min_out_length="0.4s", add_final_scene=True) | ||
| ) | ||
| assert manager.detect_scenes(video) == len(levels) + final_out_length | ||
| scenes = manager.get_scene_list() | ||
| assert [start.frame_num for start, _ in scenes] == ( | ||
| [0, 25, 38] if final_out_length == 4 else [0, 25] | ||
| ) | ||
| assert scenes[-1][1].frame_num == len(levels) + final_out_length | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("minimum", ["4", "0.4s", "00:00:00.400"]) | ||
| @pytest.mark.parametrize("use_config", [False, True]) | ||
| def test_min_out_length_cli(tmp_path, minimum, use_config): | ||
| levels = [128] * 10 + [0] * 2 + [128] * 10 + [0] * 6 + [128] * 10 | ||
| path = _write_video(tmp_path / "fades.avi", levels) | ||
| args = ["-i", path, "-o", str(tmp_path), "-m", "0"] | ||
| if use_config: | ||
| config = tmp_path / "scenedetect.cfg" | ||
| config.write_text(f"[detect-threshold]\nmin-out-length = {minimum}\n") | ||
| args += ["-c", str(config), "detect-threshold"] | ||
| else: | ||
| args += ["detect-threshold", "--min-out-length", minimum] | ||
| exit_code, output = invoke_cli([*args, "list-scenes", "-f", "scenes", "--skip-cuts"]) | ||
| assert exit_code == 0, output | ||
| with (tmp_path / "scenes.csv").open(newline="") as scene_file: | ||
| scenes = list(csv.DictReader(scene_file)) | ||
| assert [int(scene["Start Frame"]) - 1 for scene in scenes] == [0, 25] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File headers must all use standardized format:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to the exact header format you provided in 8c1d7df. Also replaced the OpenCV fourcc alias in the test with
cv2.VideoWriter.fourcc, which fixes the Pyright failure without changing the generated video.