diff --git a/scenedetect/_cli/__init__.py b/scenedetect/_cli/__init__.py index ee29c9f2..ad709136 100644 --- a/scenedetect/_cli/__init__.py +++ b/scenedetect/_cli/__init__.py @@ -1358,6 +1358,9 @@ def split_video_command( error = "The split-video command is incompatible with image sequences/URLs." raise click.BadParameter(error, param_hint="split-video") + if args is not None and not args.strip(" "): + args = "" + # Overwrite flags if no encoder flags/options were set via the CLI to avoid conflicting options # (e.g. `--copy` should override any `high-quality = yes` setting in the config file). if not (mkvmerge or copy or high_quality or args or rate_factor or preset): diff --git a/scenedetect/output/video.py b/scenedetect/output/video.py index c375aeea..1fb2e7d8 100644 --- a/scenedetect/output/video.py +++ b/scenedetect/output/video.py @@ -276,7 +276,8 @@ def split_video_ffmpeg( $VIDEO_NAME, $SCENE_NUMBER, $START_TIME, $END_TIME, $START_FRAME, $END_FRAME video_name: Name of the video to be substituted in output_file_template. If not passed will be calculated from input_video_path automatically. - arg_override: Allows overriding the arguments passed to ffmpeg for encoding. + arg_override: Allows overriding the arguments passed to ffmpeg for encoding. An empty or + all-space value passes no extra arguments; omitting it uses the default. show_progress: If True, will show progress bar provided by tqdm (if installed). show_output: If True, will show output from ffmpeg for first split. suppress_output: [DEPRECATED] DO NOT USE. For backwards compatibility only. @@ -313,7 +314,7 @@ def split_video_ffmpeg( arg_override = arg_override.replace('\\"', '"') ret_val = 0 - ffmpeg_args = arg_override.split(" ") + ffmpeg_args = [arg for arg in arg_override.split(" ") if arg] scene_num_format = "%0" scene_num_format += str(max(3, math.floor(math.log(len(scene_list), 10)) + 1)) + "d" diff --git a/tests/test_cli.py b/tests/test_cli.py index 4144feaf..e9b7a800 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -794,6 +794,47 @@ def test_cli_split_video_ffmpeg(tmp_path: Path): ) +@pytest.mark.parametrize( + ("config", "expected_args"), + [ + ("", "-map 0:v:0 -map 0:a? -map 0:s? -c:v libx264 -preset veryfast -crf 22 -c:a aac"), + ("[split-video]\nargs = -c:v libx265 -c:a aac\n", "-c:v libx265 -c:a aac"), + ], +) +def test_cli_split_video_empty_args_use_defaults( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, config: str, expected_args: str +): + """Empty and all-space -a values should behave like omitting the option.""" + config_path = tmp_path / "scenedetect.cfg" + config_path.write_text(config) + ffmpeg_args = [] + + def split_video_ffmpeg(**kwargs): + ffmpeg_args.append(kwargs["arg_override"]) + return 0 + + monkeypatch.setattr("scenedetect._cli.check_split_video_requirements", lambda **kwargs: None) + monkeypatch.setattr("scenedetect._cli.commands.split_video_ffmpeg", split_video_ffmpeg) + for args in ([], ["-a", ""], ["-a", " "]): + exit_code, output = invoke_cli( + [ + "-c", + str(config_path), + "-i", + DEFAULT_VIDEO_PATH, + "time", + "-e", + "1", + DEFAULT_DETECTOR, + "split-video", + *args, + ] + ) + assert exit_code == 0, output + + assert ffmpeg_args == [expected_args] * 3 + + @pytest.mark.skipif(condition=not is_mkvmerge_available(), reason="mkvmerge is not available") def test_cli_split_video_mkvmerge(tmp_path: Path): """Test `split-video` command using mkvmerge.""" diff --git a/tests/test_output.py b/tests/test_output.py index 697649e5..490fbe52 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -106,6 +106,36 @@ def name_formatter(video: VideoMetadata, scene: SceneMetadata): assert len(entries) == len(scenes) +@pytest.mark.parametrize( + ("arg_override", "expected"), + [ + ("", []), + (" ", []), + (" ", []), + ("-c:v libx264 -crf 30", ["-c:v", "libx264", "-crf", "30"]), + (" -c:v libx264 -crf 30", ["-c:v", "libx264", "-crf", "30"]), + ("-c:v libx264 -crf 30 ", ["-c:v", "libx264", "-crf", "30"]), + (" -c:v libx264 -crf 30 ", ["-c:v", "libx264", "-crf", "30"]), + ("-metadata title=a\u3000b", ["-metadata", "title=a\u3000b"]), + ], +) +def test_split_video_ffmpeg_arg_override_whitespace(tmp_path, monkeypatch, arg_override, expected): + calls = [] + monkeypatch.setattr( + "scenedetect.output.video.invoke_command", lambda call: calls.append(call) or 0 + ) + scenes = [(FrameTimecode(0, 30.0), FrameTimecode(30, 30.0))] + + assert ( + split_video_ffmpeg("video.mp4", scenes, output_dir=tmp_path, arg_override=arg_override) == 0 + ) + + assert len(calls) == 1 + duration_index = calls[0].index("-t") + assert calls[0][duration_index + 2 : -2] == expected + assert calls[0][-2:] == ["-sn", str(tmp_path / "video-Scene-001.mp4")] + + # TODO: Add tests for `split_video_mkvmerge`. diff --git a/website/pages/changelog.md b/website/pages/changelog.md index 44a4691e..990c78fe 100644 --- a/website/pages/changelog.md +++ b/website/pages/changelog.md @@ -872,5 +872,8 @@ Major breaking release that overhauls how time is managed. Rather than assigning - [bugfix] `FrameTimecode.get_timecode()` with `use_rounding=False` now truncates instead of sometimes rounding up the last digit. Previously 0.4996 seconds with `precision=2` gave `00:00:00.50`, and 1.99997 seconds gave `00:00:01.000` instead of `00:00:01.999`. [#611](https://github.com/Breakthrough/PySceneDetect/issues/611) - [bugfix] `FrameTimecode.get_timecode()` now shows a negative time (e.g. from a negative PTS) as zero. Previously a large negative time gave output such as `-1:-6:00.000`. A negative `precision` now raises a `ValueError` - [bugfix] Allow image sequences as inputs when the default backend is already OpenCV [#620](https://github.com/Breakthrough/PySceneDetect/issues/620) + - [bugfix] Ignore extra ASCII spaces in `split-video -a`/`--args` and `split_video_ffmpeg(arg_override=...)` instead of passing empty arguments to ffmpeg, so doubled spaces no longer pass empty values [#634](https://github.com/Breakthrough/PySceneDetect/issues/634) + - Breaking: an all-space CLI `-a` is treated like `-a ""` or an unset `-a`, using config-file arguments and then built-in defaults + - Breaking: an empty or all-space API `arg_override` adds no encoding or -map arguments to the ffmpeg command, instead of failing - [general] Updates to Windows distributions: - Python 3.13 -> Python 3.14