diff --git a/scenedetect/_cli/__init__.py b/scenedetect/_cli/__init__.py index e88719ed..ee29c9f2 100644 --- a/scenedetect/_cli/__init__.py +++ b/scenedetect/_cli/__init__.py @@ -680,7 +680,7 @@ def detect_content_command( "--frame-window", "-f", metavar="VAL", - type=click.INT, + type=_click_range("detect-adaptive", "frame-window"), default=None, help="Size of window to detect deviations from mean. Represents how many frames before/after the current one to use for mean.{}".format( USER_CONFIG.get_help_string("detect-adaptive", "frame-window") @@ -1458,7 +1458,7 @@ def split_video_command( "-n", metavar="N", default=None, - type=click.INT, + type=_click_range("save-images", "num-images"), help="Number of images to generate per scene. Will always include start/end frame, unless -n 1, in which case the image will be the frame at the mid-point of the scene. Cannot be used with -i/--interval.{}".format( USER_CONFIG.get_help_string("save-images", "num-images") ), @@ -1549,7 +1549,7 @@ def split_video_command( "-s", metavar="S", default=None, - type=click.FLOAT, + type=_click_range("save-images", "scale"), help="Factor to scale images by. Ignored if -W/--width or -H/--height is set.{}".format( USER_CONFIG.get_help_string("save-images", "scale", show_default=False) ), @@ -1559,7 +1559,7 @@ def split_video_command( "-H", metavar="H", default=None, - type=click.INT, + type=_click_range("save-images", "height"), help="Height (pixels) of images.{}".format( USER_CONFIG.get_help_string("save-images", "height", show_default=False) ), @@ -1569,7 +1569,7 @@ def split_video_command( "-W", metavar="W", default=None, - type=click.INT, + type=_click_range("save-images", "width"), help="Width (pixels) of images.{}".format( USER_CONFIG.get_help_string("save-images", "width", show_default=False) ), diff --git a/scenedetect/_cli/config.py b/scenedetect/_cli/config.py index 75a6a62e..794f8762 100644 --- a/scenedetect/_cli/config.py +++ b/scenedetect/_cli/config.py @@ -383,7 +383,7 @@ def default_to_str(value: ConfigValue) -> str: "threading-mode": "auto", }, "detect-adaptive": { - "frame-window": 2, + "frame-window": RangeValue(2, min_val=1, max_val=None), "kernel-size": KernelSizeValue(-1), "luma-only": False, "min-content-val": RangeValue(15.0, min_val=0.0, max_val=255.0), @@ -469,17 +469,17 @@ def default_to_str(value: ConfigValue) -> str: "filename": "$VIDEO_NAME-Scene-$SCENE_NUMBER-$IMAGE_NUMBER", "format": "jpeg", "frame-margin": TimecodeValue(1), - "height": 0, + "height": RangeValue(0, min_val=0, max_val=None), "interval": TimecodeValue(0), # Placeholder, as there is no interval by default "max-images": RangeValue(0, min_val=0, max_val=None), "min-images": RangeValue(1, min_val=1, max_val=None), - "num-images": 3, + "num-images": RangeValue(3, min_val=1, max_val=None), "output": None, "quality": RangeValue(_PLACEHOLDER, min_val=0, max_val=100), - "scale": 1.0, + "scale": RangeValue(1.0, min_val=0.0, max_val=None), "scale-method": Interpolation.LINEAR, "threading": True, - "width": 0, + "width": RangeValue(0, min_val=0, max_val=None), }, "save-otio": { "audio": True, diff --git a/tests/test_cli.py b/tests/test_cli.py index 58a1edeb..4f570820 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -463,6 +463,47 @@ def test_cli_kernel_size_config_file_rejects_one(tmp_path: Path, command: str): assert any("odd integer >= 3" in message for _, message in ex.value.init_log) +@pytest.mark.parametrize( + "cli_args", + [ + ["detect-adaptive", "--frame-window", "0"], + ["detect-adaptive", "--frame-window", "-3"], + ["detect-content", "save-images", "--num-images", "0"], + ["detect-content", "save-images", "--num-images", "-1"], + ["detect-content", "save-images", "--scale", "-1"], + ["detect-content", "save-images", "--height", "-5"], + ["detect-content", "save-images", "--width", "-5"], + ], +) +def test_cli_option_out_of_range(tmp_path: Path, cli_args: list[str]): + """Values out of an option's range must give a usage error, not a traceback.""" + exit_code, output = invoke_cli( + ["-i", DEFAULT_VIDEO_PATH, "-o", str(tmp_path), "time", "-e", "1", *cli_args] + ) + assert exit_code == 2 + assert "Traceback" not in output + assert "Invalid value" in output + + +@pytest.mark.parametrize( + ("command", "option"), + [ + ("detect-adaptive", "frame-window = 0"), + ("save-images", "num-images = 0"), + ("save-images", "scale = -1"), + ("save-images", "height = -5"), + ("save-images", "width = -5"), + ], +) +def test_cli_config_file_option_out_of_range(tmp_path: Path, command: str, option: str): + """A config file value out of range must be rejected when loading.""" + config_path = tmp_path.joinpath("config.cfg") + config_path.write_text(f"[{command}]\n{option}\n") + with pytest.raises(ConfigLoadFailure) as ex: + ConfigRegistry(str(config_path)) + assert any("Value must be" in message for _, message in ex.value.init_log) + + @pytest.mark.parametrize( ("options", "succeeds"), [ diff --git a/website/pages/changelog.md b/website/pages/changelog.md index 2589b7e7..bb086d23 100644 --- a/website/pages/changelog.md +++ b/website/pages/changelog.md @@ -863,3 +863,4 @@ 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] 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)