From 941e8d3f1845c7a18c144f5f6a33bd3073794bee Mon Sep 17 00:00:00 2001 From: goranai <28477490+goranai@users.noreply.github.com> Date: Tue, 10 Nov 2026 15:39:23 +0800 Subject: [PATCH 1/2] [bugfix] Reject invalid --kernel-size with a usage error instead of a traceback (#604) Co-Authored-By: Claude Opus 5.5 --- scenedetect/_cli/config.py | 8 ++++---- scenedetect/_cli/context.py | 17 +++++++++++++++-- tests/test_cli.py | 25 +++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/scenedetect/_cli/config.py b/scenedetect/_cli/config.py index 03918dcc..82c6339a 100644 --- a/scenedetect/_cli/config.py +++ b/scenedetect/_cli/config.py @@ -242,15 +242,15 @@ def from_config(config_value: str, default: "ScoreWeightsValue") -> "ScoreWeight class KernelSizeValue(ValidatedValue): - """Validator for kernel sizes (odd integer > 1, or -1 for auto size).""" + """Validator for kernel sizes (odd integer >= 3, or -1 for auto size).""" def __init__(self, value: int): self._value: int | None if value == -1: # Kernel size of -1 maps to None internally for auto-sized kernel. self._value = None - elif value < 0: - # Disallow other negative values. + elif value < 3: + # Disallow other negative values, 0 and 1 (detectors require an odd integer >= 3). raise ValueError() elif value % 2 == 0: # Disallow even values. @@ -273,7 +273,7 @@ def from_config(config_value: str, default: "KernelSizeValue") -> "KernelSizeVal return KernelSizeValue(int(config_value)) except ValueError as ex: raise OptionParseFailure( - "Value must be an odd integer greater than 1, or set to -1 for auto kernel size." + "Value must be an odd integer >= 3, or set to -1 for auto kernel size." ) from ex diff --git a/scenedetect/_cli/context.py b/scenedetect/_cli/context.py index d2914d46..da82e6fa 100644 --- a/scenedetect/_cli/context.py +++ b/scenedetect/_cli/context.py @@ -150,6 +150,15 @@ def _resolve_min_scene_len(self, command: str, override: str | None) -> int: assert parsed is not None return parsed.frame_num + @staticmethod + def _validate_kernel_size(kernel_size: int | None) -> int | None: + """Reject a `--kernel-size` the detectors would raise on, so it gives a usage error.""" + if kernel_size is not None and (kernel_size < 3 or kernel_size % 2 == 0): + raise click.BadParameter( + "Kernel size must be an odd integer >= 3.", param_hint="kernel-size" + ) + return kernel_size + def parse_timecode(self, value: str | None, correct_pts: bool = False) -> FrameTimecode | None: """Parses a user input string into a FrameTimecode assuming the given framerate. If `value` is None it will be passed through without processing. @@ -359,7 +368,9 @@ def get_detect_content_params( return { "weights": self.config.get_value("detect-content", "weights", weights), - "kernel_size": self.config.get_value("detect-content", "kernel-size", kernel_size), + "kernel_size": self.config.get_value( + "detect-content", "kernel-size", self._validate_kernel_size(kernel_size) + ), "luma_only": luma_only or self.config.get_value("detect-content", "luma-only"), "min_scene_len": min_scene_len_frames, "threshold": self.config.get_value("detect-content", "threshold", threshold), @@ -391,7 +402,9 @@ def get_detect_adaptive_params( return { "adaptive_threshold": self.config.get_value("detect-adaptive", "threshold", threshold), "weights": self.config.get_value("detect-adaptive", "weights", weights), - "kernel_size": self.config.get_value("detect-adaptive", "kernel-size", kernel_size), + "kernel_size": self.config.get_value( + "detect-adaptive", "kernel-size", self._validate_kernel_size(kernel_size) + ), "luma_only": luma_only or self.config.get_value("detect-adaptive", "luma-only"), "min_content_val": self.config.get_value( "detect-adaptive", "min-content-val", min_content_val diff --git a/tests/test_cli.py b/tests/test_cli.py index 9139ba1b..16d214d0 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -423,6 +423,31 @@ def test_cli_frame_rate_aliases(option: str): assert exit_code == 0 +@pytest.mark.parametrize( + "cli_args", + [ + ["detect-content", "-k", "1"], + ["detect-content", "-k", "4"], + ["detect-adaptive", "-k", "1"], + ], +) +def test_cli_kernel_size_rejects_invalid(cli_args: list[str]): + """Kernel sizes below 3 or even must be rejected with a usage error, not a traceback.""" + exit_code, output = invoke_cli(["-i", DEFAULT_VIDEO_PATH, *cli_args, "time", "-e", "1"]) + assert exit_code != 0 + assert "Kernel size must be an odd integer >= 3." in output + + +@pytest.mark.parametrize("command", ["detect-content", "detect-adaptive"]) +def test_cli_kernel_size_config_file_rejects_one(tmp_path: Path, command: str): + """A config file kernel-size of 1 must be rejected when loading, like other invalid sizes.""" + config_path = tmp_path.joinpath("config.cfg") + config_path.write_text(f"[{command}]\nkernel-size = 1\n") + with pytest.raises(ConfigLoadFailure) as ex: + ConfigRegistry(str(config_path)) + assert any("odd integer >= 3" in message for _, message in ex.value.init_log) + + @pytest.mark.parametrize( ("options", "succeeds"), [ From 4a4b66e3da153dbf3c5c797d3d6d93156899acf4 Mon Sep 17 00:00:00 2001 From: goranai <28477490+goranai@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:22:31 +0800 Subject: [PATCH 2/2] [bugfix] Reuse KernelSizeValue for -k so -1 means auto (#604) Adds tests for -k -1, negative and valid sizes, and a changelog entry. Co-Authored-By: Claude Opus 5.5 --- scenedetect/_cli/context.py | 21 +++++++++++++-------- tests/test_cli.py | 14 +++++++++++++- website/pages/changelog.md | 1 + 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/scenedetect/_cli/context.py b/scenedetect/_cli/context.py index da82e6fa..0c65165d 100644 --- a/scenedetect/_cli/context.py +++ b/scenedetect/_cli/context.py @@ -23,6 +23,7 @@ ConfigLoadFailure, ConfigRegistry, CropValue, + KernelSizeValue, ) from scenedetect.common import MAX_FPS_DELTA, FrameTimecode from scenedetect.detector import SceneDetector @@ -151,13 +152,17 @@ def _resolve_min_scene_len(self, command: str, override: str | None) -> int: return parsed.frame_num @staticmethod - def _validate_kernel_size(kernel_size: int | None) -> int | None: - """Reject a `--kernel-size` the detectors would raise on, so it gives a usage error.""" - if kernel_size is not None and (kernel_size < 3 or kernel_size % 2 == 0): + def _kernel_size_value(kernel_size: int | None) -> KernelSizeValue | None: + """Validate `--kernel-size` like the config file does, so -1 means auto and bad values + give a usage error instead of a traceback.""" + if kernel_size is None: + return None + try: + return KernelSizeValue(kernel_size) + except ValueError as ex: raise click.BadParameter( - "Kernel size must be an odd integer >= 3.", param_hint="kernel-size" - ) - return kernel_size + "Kernel size must be an odd integer >= 3, or -1 for auto.", param_hint="kernel-size" + ) from ex def parse_timecode(self, value: str | None, correct_pts: bool = False) -> FrameTimecode | None: """Parses a user input string into a FrameTimecode assuming the given framerate. If `value` @@ -369,7 +374,7 @@ def get_detect_content_params( return { "weights": self.config.get_value("detect-content", "weights", weights), "kernel_size": self.config.get_value( - "detect-content", "kernel-size", self._validate_kernel_size(kernel_size) + "detect-content", "kernel-size", self._kernel_size_value(kernel_size) ), "luma_only": luma_only or self.config.get_value("detect-content", "luma-only"), "min_scene_len": min_scene_len_frames, @@ -403,7 +408,7 @@ def get_detect_adaptive_params( "adaptive_threshold": self.config.get_value("detect-adaptive", "threshold", threshold), "weights": self.config.get_value("detect-adaptive", "weights", weights), "kernel_size": self.config.get_value( - "detect-adaptive", "kernel-size", self._validate_kernel_size(kernel_size) + "detect-adaptive", "kernel-size", self._kernel_size_value(kernel_size) ), "luma_only": luma_only or self.config.get_value("detect-adaptive", "luma-only"), "min_content_val": self.config.get_value( diff --git a/tests/test_cli.py b/tests/test_cli.py index 16d214d0..2ef60687 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -428,14 +428,26 @@ def test_cli_frame_rate_aliases(option: str): [ ["detect-content", "-k", "1"], ["detect-content", "-k", "4"], + ["detect-content", "-k", "-3"], ["detect-adaptive", "-k", "1"], + ["detect-adaptive", "-k", "-2"], ], ) def test_cli_kernel_size_rejects_invalid(cli_args: list[str]): """Kernel sizes below 3 or even must be rejected with a usage error, not a traceback.""" exit_code, output = invoke_cli(["-i", DEFAULT_VIDEO_PATH, *cli_args, "time", "-e", "1"]) assert exit_code != 0 - assert "Kernel size must be an odd integer >= 3." in output + assert "Kernel size must be an odd integer >= 3, or -1 for auto." in output + + +@pytest.mark.parametrize("command", ["detect-content", "detect-adaptive"]) +@pytest.mark.parametrize("kernel_size", ["3", "-1"]) +def test_cli_kernel_size_accepts_valid(command: str, kernel_size: str): + """An odd kernel size >= 3 works, and -1 means auto, the same as in a config file.""" + exit_code, _ = invoke_cli( + ["-i", DEFAULT_VIDEO_PATH, command, "-k", kernel_size, "time", "-e", "1"] + ) + assert exit_code == 0 @pytest.mark.parametrize("command", ["detect-content", "detect-adaptive"]) diff --git a/website/pages/changelog.md b/website/pages/changelog.md index 99338e7e..8bd527c8 100644 --- a/website/pages/changelog.md +++ b/website/pages/changelog.md @@ -859,3 +859,4 @@ Development ## PySceneDetect 0.8 (TBD) - [general] The root `pyproject.toml` is now `scenedetect` again (it had a development-only `scenedetect-core` name since 0.7.1). `pip install -e .` now installs the same distribution as PyPI. The `[opencv]` and `[opencv-headless]` extras of the development config were removed because `opencv-python` is now a declared dependency [#558](https://github.com/Breakthrough/PySceneDetect/issues/558) + - [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)