From 4605bc28b6b87422f5bf7cf4881eb80f94954370 Mon Sep 17 00:00:00 2001 From: EnesSoydan <191651630+EnesSoydan@users.noreply.github.com> Date: Fri, 6 Nov 2026 04:01:39 +0300 Subject: [PATCH 1/2] [cli] Add --stdin to read the input video from a pipe Closes #434 Closes #189 --- docs/cli.rst | 4 ++ scenedetect/__main__.py | 2 + scenedetect/_cli/__init__.py | 26 +++++++- scenedetect/_cli/context.py | 66 ++++++++++++++++--- scenedetect/_cli/controller.py | 8 +++ tests/test_cli.py | 116 +++++++++++++++++++++++++++++++++ website/pages/changelog.md | 1 + 7 files changed, 211 insertions(+), 12 deletions(-) diff --git a/docs/cli.rst b/docs/cli.rst index c904c43d..55fab9c5 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -54,6 +54,10 @@ Options [REQUIRED] Input video file. Image sequences and URLs are supported. +.. option:: --stdin + + Read the input video from standard input instead of :option:`-i/--input <-i>`. Requires the pyav backend and a container that can be read from a pipe (e.g. MKV or MPEG-TS). Commands that need to seek in the video are not supported. + .. option:: -o DIR, --output DIR Output directory for created files. If unset, working directory will be used. May be overridden by command options. diff --git a/scenedetect/__main__.py b/scenedetect/__main__.py index f97bc320..f074117e 100755 --- a/scenedetect/__main__.py +++ b/scenedetect/__main__.py @@ -49,6 +49,8 @@ def main(): if DEBUG_MODE: raise raise SystemExit(1) from None + except SystemExit: + raise except BaseException as ex: if DEBUG_MODE: raise diff --git a/scenedetect/_cli/__init__.py b/scenedetect/_cli/__init__.py index 58e51e3b..a9932e30 100644 --- a/scenedetect/_cli/__init__.py +++ b/scenedetect/_cli/__init__.py @@ -205,6 +205,12 @@ def print_command_help(ctx: click.Context, command: click.Command): type=click.STRING, help="[REQUIRED] Input video file. Image sequences and URLs are supported.", ) +@click.option( + "--stdin", + is_flag=True, + default=False, + help="Read the input video from standard input instead of -i/--input. Requires the pyav backend and a container that can be read from a pipe (e.g. MKV or MPEG-TS). Commands that need to seek in the video are not supported.", +) @click.option( "--output", "-o", @@ -336,6 +342,7 @@ def print_command_help(ctx: click.Context, command: click.Command): def scenedetect( ctx: click.Context, input: str | None, + stdin: bool, output: str | None, stats: str | None, config: str | None, @@ -356,6 +363,7 @@ def scenedetect( ctx.handle_options( input_path=input, + use_stdin=stdin, output=output, frame_rate=frame_rate, stats_file=stats, @@ -374,6 +382,12 @@ def scenedetect( ) +def check_not_stdin(ctx: CliContext, command: str) -> None: + """Rejects `command` if the input is read from stdin, which cannot seek and has no path.""" + if ctx.use_stdin: + raise click.BadParameter(f"{command} is incompatible with --stdin.", param_hint=command) + + def add_hidden_alias(command: click.Command, alias: str): """Adds a copy of `command` that can be invoked under the name `alias`.""" # Shallow copy: deepcopy fails on Python 3.10 + click >=8.3 because click's internal @@ -492,6 +506,8 @@ def time_command( "Only one of --duration/-d or --end/-e can be specified, not both.", param_hint="time", ) + if start is not None: + check_not_stdin(ctx, "time -s/--start") logger.debug("Setting video time:\n start: %s, duration: %s, end: %s", start, duration, end) # *NOTE*: The Python API uses 0-based frame indices, but the CLI uses 1-based indices to # match the default start number used by `ffmpeg` when saving frames as images. As such, @@ -1005,6 +1021,7 @@ def load_scenes_command(ctx: click.Context, input: str | None, start_col_name: s ctx = ctx.obj assert isinstance(ctx, CliContext) + check_not_stdin(ctx, "load-scenes") logger.debug("Will load scenes from %s (start_col_name = %s)", input, start_col_name) assert ctx.scene_manager is not None if ctx.scene_manager.get_num_detectors() > 0: @@ -1093,7 +1110,8 @@ def save_html_command( assert isinstance(ctx, CliContext) # Make sure a save-images command is in the pipeline for us to use the results from if we need # to include images. - include_images = not ctx.config.get_value("save-html", "no-images", no_images) + no_images = ctx.config.get_value("save-html", "no-images", no_images) or ctx.use_stdin + include_images = not no_images if include_images and not ctx.save_images: assert save_images_command.callback is not None save_images_command.callback() @@ -1101,7 +1119,7 @@ def save_html_command( "filename": ctx.config.get_value("save-html", "filename", filename), "image_width": ctx.config.get_value("save-html", "image-width", image_width), "image_height": ctx.config.get_value("save-html", "image-height", image_height), - "no_images": ctx.config.get_value("save-html", "no-images", no_images), + "no_images": no_images, "show": ctx.config.get_value("save-html", "show", show), } ctx.add_command(cli_commands.save_html, save_html_args) @@ -1330,6 +1348,7 @@ def split_video_command( ctx = ctx.obj assert isinstance(ctx, CliContext) + check_not_stdin(ctx, "split-video") check_split_video_requirements(use_mkvmerge=mkvmerge) assert ctx.video_stream is not None if "%" in ctx.video_stream.path or "://" in ctx.video_stream.path: @@ -1575,6 +1594,7 @@ def save_images_command( assert isinstance(ctx, CliContext) assert ctx.video_stream is not None + check_not_stdin(ctx, "save-images") if "://" in ctx.video_stream.path: error_str = "\nThe save-images command is incompatible with URLs." logger.error(error_str) @@ -1902,6 +1922,7 @@ def save_fcp_command( ctx = ctx.obj assert isinstance(ctx, CliContext) + check_not_stdin(ctx, "save-fcp") save_fcp_args = { "filename": ctx.config.get_value("save-fcp", "filename", filename), "format": ctx.config.get_value("save-fcp", "format", format), @@ -1965,6 +1986,7 @@ def save_otio_command( ctx = ctx.obj assert isinstance(ctx, CliContext) + check_not_stdin(ctx, "save-otio") if audio and no_audio: raise click.BadArgumentUsage("Only one of --audio or --no-audio can be specified.") diff --git a/scenedetect/_cli/context.py b/scenedetect/_cli/context.py index d2914d46..3aa6dde2 100644 --- a/scenedetect/_cli/context.py +++ b/scenedetect/_cli/context.py @@ -12,6 +12,7 @@ """Context of which command-line options and config settings the user provided.""" import logging +import sys import typing as ty import click @@ -24,6 +25,7 @@ ConfigRegistry, CropValue, ) +from scenedetect.backends import VideoStreamAv from scenedetect.common import MAX_FPS_DELTA, FrameTimecode from scenedetect.detector import SceneDetector from scenedetect.detectors import ( @@ -45,6 +47,20 @@ """The user config, which can be overriden by command-line. If not found, will be default config.""" +class _PipeReader: + """Read-only view of a pipe. Windows reports pipes as seekable, but seeking on one fails or + returns bogus positions, so this keeps PyAV from trying.""" + + def __init__(self, pipe: ty.BinaryIO): + self._pipe = pipe + + def read(self, size: int = -1) -> bytes: + return self._pipe.read(size) + + def seekable(self) -> bool: + return False + + def check_split_video_requirements(use_mkvmerge: bool) -> None: """Validates that the proper tool is available on the system to perform the `split-video` command. @@ -90,6 +106,7 @@ def __init__(self): # Input: self.video_stream: VideoStream | None = None + self.use_stdin: bool = False # --stdin self.load_scenes_input: str | None = None # load-scenes -i/--input self.load_scenes_column_name: str | None = None # load-scenes -c/--start-col-name self.start_time: FrameTimecode | None = None # time -s/--start @@ -177,6 +194,7 @@ def parse_timecode(self, value: str | None, correct_pts: bool = False) -> FrameT def handle_options( self, input_path: str | None, + use_stdin: bool, output: str | None, frame_rate: float | None, stats_file: str | None, @@ -256,12 +274,20 @@ def handle_options( param_hint="frame skip + stats file", ) + if use_stdin and input_path is not None: + raise click.BadParameter( + "-i/--input cannot be used with --stdin.", param_hint="--stdin" + ) + # Handle case where -i/--input was not specified (e.g. for the `help` command). - if input_path is None: + if input_path is None and not use_stdin: return # Load the input video to obtain a time base for parsing timecodes. - self._open_video_stream(input_path, frame_rate, backend) + if use_stdin: + self._open_stdin_stream(frame_rate, backend) + else: + self._open_video_stream(input_path, frame_rate, backend) self.output = self.config.get_value("global", "output", output) if self.output: @@ -492,13 +518,27 @@ def _initialize_logging( # Initialize logger with the set CLI args / user configuration. init_logger(log_level=curr_verbosity, show_stdout=not self.quiet_mode, log_file=logfile) + def _open_stdin_stream(self, frame_rate: float | None, backend: str | None): + if backend not in (None, "pyav") or VideoStreamAv is None: + raise click.BadParameter( + "--stdin requires the pyav backend. Install it with `pip install av`.", + param_hint="--stdin", + ) + if sys.stdin.isatty(): + raise click.BadParameter( + "--stdin requires a video to be piped to the program.", param_hint="--stdin" + ) + self.use_stdin = True + self._open_video_stream(None, frame_rate, "pyav") + def _open_video_stream( self, - input_path: str, + input_path: str | None, frame_rate: float | None, backend: str | None, ): - if "%" in input_path and backend != "opencv": + input_hint = "--stdin" if self.use_stdin else "-i/--input" + if input_path is not None and "%" in input_path and backend != "opencv": raise click.BadParameter( "The OpenCV backend (`--backend opencv`) must be used to process image sequences.", param_hint="-i/--input", @@ -514,7 +554,15 @@ def _open_video_stream( ) # Open the video with the specified backend, loading any required config settings. - if backend == "pyav": + if self.use_stdin: + self.video_stream = VideoStreamAv( + _PipeReader(sys.stdin.buffer), + frame_rate=frame_rate, + name="stdin", + threading_mode=self.config.get_value("backend-pyav", "threading-mode"), + suppress_output=self.config.get_value("backend-pyav", "suppress-output"), + ) + elif backend == "pyav": self.video_stream = open_video( path=input_path, frame_rate=frame_rate, @@ -558,7 +606,7 @@ def _open_video_stream( raise click.BadParameter( "Failed to obtain frame rate for input video. Manually specify frame rate with the" " -f/--frame-rate option, or try re-encoding the file.", - param_hint="-i/--input", + param_hint=input_hint, ) from ex except VideoOpenFailure as ex: if DEBUG_MODE: @@ -567,11 +615,9 @@ def _open_video_stream( "Failed to open input video{}: {}".format( f" using {backend} backend" if backend else "", str(ex) ), - param_hint="-i/--input", + param_hint=input_hint, ) from ex except OSError as ex: if DEBUG_MODE: raise - raise click.BadParameter( - f"Input error:\n\n\t{ex!s}\n", param_hint="-i/--input" - ) from None + raise click.BadParameter(f"Input error:\n\n\t{ex!s}\n", param_hint=input_hint) from None diff --git a/scenedetect/_cli/controller.py b/scenedetect/_cli/controller.py index bf8b6f63..591ff7b2 100644 --- a/scenedetect/_cli/controller.py +++ b/scenedetect/_cli/controller.py @@ -143,6 +143,14 @@ def _detect(context: CliContext) -> tuple[SceneList, CutList] | None: ) return None + if num_frames <= 0 and context.use_stdin: + logger.critical( + "Failed to read any frames from stdin. Some containers can't be read from a pipe, such" + " as an MP4 with its index at the end of the file. Try remuxing to MKV or MPEG-TS:\n" + " ffmpeg -i input.mp4 -c copy -f matroska -\n" + ) + raise SystemExit(1) + perf_duration = time.time() - perf_start_time logger.info( "Processed %d frames in %.1f seconds (average %.2f FPS).", diff --git a/tests/test_cli.py b/tests/test_cli.py index 9139ba1b..272ff13f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -30,13 +30,16 @@ # TODO: Missing tests for --min-scene-len and --drop-short-scenes. import sys from pathlib import Path +from types import SimpleNamespace +import click import cv2 import numpy as np import pytest import scenedetect from scenedetect._cli.config import ConfigLoadFailure, ConfigRegistry +from scenedetect._cli.context import CliContext from scenedetect.output import is_ffmpeg_available, is_mkvmerge_available from scenedetect.platform import StrPath from tests.helpers import invoke_cli @@ -1755,3 +1758,116 @@ def test_cli_save_fcp_fcp7(tmp_path: Path): assert second_file is not None assert second_file.attrib["id"] == "file1" assert second_file.find("pathurl") is None + + +# Matroska can be demuxed from a pipe, unlike an MP4 with its index at the end of the file. +STDIN_VIDEO_PATH = "tests/resources/issue-236-split-copy.mkv" + + +def run_stdin(*args: str, video: str | None = STDIN_VIDEO_PATH) -> subprocess.CompletedProcess: + """Runs the CLI with `video` written to stdin through a real pipe.""" + return subprocess.run( + [*SCENEDETECT_CMD.split(" "), *args], + input=Path(video).read_bytes() if video else b"", + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + + +def test_cli_stdin_matches_file_input(): + """Scenes detected from stdin should match those from the same video opened by path.""" + command = ["detect-content", "list-scenes", "-n"] + piped = run_stdin("--stdin", *command) + assert piped.returncode == 0, piped.stdout.decode(errors="replace") + from_file = subprocess.run( + [*SCENEDETECT_CMD.split(" "), "-i", STDIN_VIDEO_PATH, "-b", "pyav", *command], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + assert from_file.returncode == 0 + + def scene_rows(output: bytes) -> list[str]: + return [line for line in output.decode().splitlines() if line.strip().startswith("|")] + + assert scene_rows(piped.stdout) + assert scene_rows(piped.stdout) == scene_rows(from_file.stdout) + + +def test_cli_stdin_output_filename(tmp_path: Path): + """$VIDEO_NAME should have a usable value since there is no input path.""" + result = run_stdin("--stdin", "-o", str(tmp_path), "detect-content", "list-scenes") + assert result.returncode == 0, result.stdout.decode(errors="replace") + assert (tmp_path / "stdin-Scenes.csv").exists() + + +def test_cli_stdin_save_html_without_images(tmp_path: Path): + """save-html needs to seek for images, so they should be left out when reading from stdin.""" + result = run_stdin("--stdin", "-o", str(tmp_path), "detect-content", "save-html") + assert result.returncode == 0, result.stdout.decode(errors="replace") + assert (tmp_path / "stdin-Scenes.html").exists() + assert not list(tmp_path.glob("*.jpg")) + + +def test_cli_stdin_with_input(): + """--stdin and -i/--input should be mutually exclusive.""" + result = run_stdin("--stdin", "-i", DEFAULT_VIDEO_PATH, "detect-content") + assert result.returncode != 0 + assert b"cannot be used with --stdin" in result.stdout + + +def test_cli_stdin_with_other_backend(): + """--stdin is only implemented by the PyAV backend.""" + result = run_stdin("--stdin", "-b", "opencv", "detect-content") + assert result.returncode != 0 + assert b"requires the pyav backend" in result.stdout + + +def test_cli_stdin_terminal(monkeypatch: pytest.MonkeyPatch): + """Reading from a terminal would block forever, so it should be rejected.""" + monkeypatch.setattr("sys.stdin", SimpleNamespace(isatty=lambda: True)) + with pytest.raises(click.BadParameter, match="requires a video to be piped"): + CliContext()._open_stdin_stream(frame_rate=None, backend=None) + + +def test_cli_stdin_stats_file(tmp_path: Path): + """A stats file should be written for each frame read from stdin.""" + result = run_stdin("--stdin", "-o", str(tmp_path), "-s", "stats.csv", "detect-content") + assert result.returncode == 0, result.stdout.decode(errors="replace") + assert len((tmp_path / "stats.csv").read_text().splitlines()) > 1 + + +@pytest.mark.parametrize( + "command", + [ + "split-video", + "save-images", + "save-fcp", + "save-otio", + # The option requires a path that exists, but it's rejected before the file is parsed. + f"load-scenes -i {DEFAULT_CONFIG_FILE}", + "time -s 1s", + ], +) +def test_cli_stdin_incompatible_commands(command: str): + """Commands that need to seek or refer to a source file should fail before processing.""" + result = run_stdin("--stdin", *command.split(" ")) + assert result.returncode != 0 + output = result.stdout.decode(errors="replace") + assert "incompatible with --stdin" in output + assert "Processed" not in output + + +def test_cli_stdin_unreadable_container(): + """An MP4 with its index at the end decodes no frames from a pipe, which shouldn't be silent.""" + result = run_stdin("--stdin", "detect-content", video="tests/resources/testvideo.mp4") + assert result.returncode != 0 + assert b"Failed to read any frames from stdin" in result.stdout + + +def test_cli_stdin_no_data(monkeypatch: pytest.MonkeyPatch): + """An empty pipe should be reported as a failure to open the input.""" + # Errors opening the input are only reported as usage errors outside of debug mode. + monkeypatch.setenv("SCENEDETECT_DEBUG", "") + result = run_stdin("--stdin", "detect-content", video=None) + assert result.returncode != 0 + assert b"Invalid value for --stdin: Failed to open input video" in result.stdout diff --git a/website/pages/changelog.md b/website/pages/changelog.md index 99338e7e..fdee4d4e 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) + - [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) From 97e01e6de19b04299f52253f1dc50e73c035d732 Mon Sep 17 00:00:00 2001 From: EnesSoydan <191651630+EnesSoydan@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:59:58 +0300 Subject: [PATCH 2/2] [cli] Address review feedback on --stdin - Return None from _detect() when no frames are read from stdin, and exit with 1 from main() when run_scenedetect() reports a failure - Explain in the --stdin help that an MP4 needs its index at the start of the file (-movflags +faststart), otherwise remux to MKV or MPEG-TS - Raise UsageError for commands that are incompatible with --stdin - Warn when save-html leaves out images because of --stdin - Handle sys.stdin being None (e.g. under pythonw.exe) - Add a CONCORDANCE row for --stdin and regenerate docs/cli.rst - Fix pyright errors in _open_video_stream --- docs/cli.rst | 5 ++++- docs/generate_cli_docs.py | 5 +++++ scenedetect/__main__.py | 6 +++--- scenedetect/_cli/__init__.py | 9 ++++++--- scenedetect/_cli/context.py | 8 +++++--- scenedetect/_cli/controller.py | 17 +++++++++++------ tests/test_cli.py | 13 +++++++++++-- website/pages/changelog.md | 1 + 8 files changed, 46 insertions(+), 18 deletions(-) diff --git a/docs/cli.rst b/docs/cli.rst index 55fab9c5..c0f5b09e 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -56,7 +56,7 @@ Options .. option:: --stdin - Read the input video from standard input instead of :option:`-i/--input <-i>`. Requires the pyav backend and a container that can be read from a pipe (e.g. MKV or MPEG-TS). Commands that need to seek in the video are not supported. + Read the input video from standard input instead of :option:`-i/--input <-i>`. Requires the pyav backend. An MP4 needs its index at the start of the file (ffmpeg -movflags +faststart), otherwise remux to MKV or MPEG-TS. Commands that need to seek in the video are not supported. .. option:: -o DIR, --output DIR @@ -136,6 +136,9 @@ Global options, their config file keys, and their Python API counterparts. * - :option:`-i/--input ` - - :func:`~scenedetect.open_video`, :func:`~scenedetect.detect` + * - :option:`--stdin ` + - + - :class:`~scenedetect.backends.pyav.VideoStreamAv` (``path_or_io``) * - :option:`-o/--output ` - :ref:`[global] output ` - ``output_dir`` argument of :func:`~scenedetect.output.save_images` and :func:`~scenedetect.output.split_video_ffmpeg`; output path argument of each ``write_scene_list*`` function diff --git a/docs/generate_cli_docs.py b/docs/generate_cli_docs.py index d20beee6..2673745f 100644 --- a/docs/generate_cli_docs.py +++ b/docs/generate_cli_docs.py @@ -255,6 +255,11 @@ class Counterpart: option="input", api=(":func:`~scenedetect.open_video`", ":func:`~scenedetect.detect`"), ), + Counterpart( + "Input video from a pipe", + option="stdin", + api=(f":class:`~{_BE}.pyav.VideoStreamAv` (``path_or_io``)",), + ), Counterpart( "Start, end, or duration", command="time", diff --git a/scenedetect/__main__.py b/scenedetect/__main__.py index f074117e..ec84e720 100755 --- a/scenedetect/__main__.py +++ b/scenedetect/__main__.py @@ -43,19 +43,19 @@ def main(): with log_redirect: try: - run_scenedetect(context) + success = run_scenedetect(context) except KeyboardInterrupt: logger.info("Stopped.") if DEBUG_MODE: raise raise SystemExit(1) from None - except SystemExit: - raise except BaseException as ex: if DEBUG_MODE: raise logger.critical("ERROR: Unhandled exception:", exc_info=ex) raise SystemExit(1) from ex + if not success: + raise SystemExit(1) if __name__ == "__main__": diff --git a/scenedetect/_cli/__init__.py b/scenedetect/_cli/__init__.py index a9932e30..28bd0065 100644 --- a/scenedetect/_cli/__init__.py +++ b/scenedetect/_cli/__init__.py @@ -209,7 +209,7 @@ def print_command_help(ctx: click.Context, command: click.Command): "--stdin", is_flag=True, default=False, - help="Read the input video from standard input instead of -i/--input. Requires the pyav backend and a container that can be read from a pipe (e.g. MKV or MPEG-TS). Commands that need to seek in the video are not supported.", + help="Read the input video from standard input instead of -i/--input. Requires the pyav backend. An MP4 needs its index at the start of the file (ffmpeg -movflags +faststart), otherwise remux to MKV or MPEG-TS. Commands that need to seek in the video are not supported.", ) @click.option( "--output", @@ -385,7 +385,7 @@ def scenedetect( def check_not_stdin(ctx: CliContext, command: str) -> None: """Rejects `command` if the input is read from stdin, which cannot seek and has no path.""" if ctx.use_stdin: - raise click.BadParameter(f"{command} is incompatible with --stdin.", param_hint=command) + raise click.UsageError(f"{command} is incompatible with --stdin.") def add_hidden_alias(command: click.Command, alias: str): @@ -1110,7 +1110,10 @@ def save_html_command( assert isinstance(ctx, CliContext) # Make sure a save-images command is in the pipeline for us to use the results from if we need # to include images. - no_images = ctx.config.get_value("save-html", "no-images", no_images) or ctx.use_stdin + no_images = ctx.config.get_value("save-html", "no-images", no_images) + if ctx.use_stdin and not no_images: + logger.warning("WARNING: save-html will not include images when reading from --stdin.") + no_images = True include_images = not no_images if include_images and not ctx.save_images: assert save_images_command.callback is not None diff --git a/scenedetect/_cli/context.py b/scenedetect/_cli/context.py index 3aa6dde2..276d2515 100644 --- a/scenedetect/_cli/context.py +++ b/scenedetect/_cli/context.py @@ -524,7 +524,8 @@ def _open_stdin_stream(self, frame_rate: float | None, backend: str | None): "--stdin requires the pyav backend. Install it with `pip install av`.", param_hint="--stdin", ) - if sys.stdin.isatty(): + # sys.stdin is None if there is no console, e.g. under pythonw.exe on Windows. + if sys.stdin is None or sys.stdin.isatty(): raise click.BadParameter( "--stdin requires a video to be piped to the program.", param_hint="--stdin" ) @@ -554,9 +555,10 @@ def _open_video_stream( ) # Open the video with the specified backend, loading any required config settings. - if self.use_stdin: + if input_path is None: # --stdin self.video_stream = VideoStreamAv( - _PipeReader(sys.stdin.buffer), + # _PipeReader only implements the part of BinaryIO that PyAV uses. + ty.cast(ty.BinaryIO, _PipeReader(sys.stdin.buffer)), frame_rate=frame_rate, name="stdin", threading_mode=self.config.get_value("backend-pyav", "threading-mode"), diff --git a/scenedetect/_cli/controller.py b/scenedetect/_cli/controller.py index 591ff7b2..329d5685 100644 --- a/scenedetect/_cli/controller.py +++ b/scenedetect/_cli/controller.py @@ -27,18 +27,21 @@ logger = logging.getLogger("pyscenedetect") -def run_scenedetect(context: CliContext): +def run_scenedetect(context: CliContext) -> bool: """Perform main CLI application control logic. Run once all command-line options and configuration file options have been validated. Arguments: context: Prevalidated command-line option context to use for processing. + + Returns: + False if scene detection failed, True otherwise. """ # No input may have been specified depending on the commands/args that were used. logger.debug("Running controller.") if context.scene_manager is None: logger.debug("No input specified.") - return + return True # Suppress warnings when reading past EOF in MoviePy (#461). if VideoStreamMoviePy and isinstance(context.video_stream, VideoStreamMoviePy): @@ -58,7 +61,7 @@ def run_scenedetect(context: CliContext): # Perform scene detection on input. result = _detect(context) if result is None: - return + return False scenes, cuts = result scenes = _postprocess_scene_list(context, scenes) # Handle -s/--stats option. @@ -76,6 +79,7 @@ def run_scenedetect(context: CliContext): # Handle post-processing commands the user wants to run (see scenedetect._cli.commands). for handler, kwargs in context.commands: handler(context=context, scenes=scenes, cuts=cuts, **kwargs) + return True def _postprocess_scene_list(context: CliContext, scene_list: SceneList) -> SceneList: @@ -145,11 +149,12 @@ def _detect(context: CliContext) -> tuple[SceneList, CutList] | None: if num_frames <= 0 and context.use_stdin: logger.critical( - "Failed to read any frames from stdin. Some containers can't be read from a pipe, such" - " as an MP4 with its index at the end of the file. Try remuxing to MKV or MPEG-TS:\n" + "Failed to read any frames from stdin. An MP4 needs its index at the start of the file" + " to be read from a pipe (ffmpeg -movflags +faststart). Otherwise, try remuxing to MKV" + " or MPEG-TS:\n" " ffmpeg -i input.mp4 -c copy -f matroska -\n" ) - raise SystemExit(1) + return None perf_duration = time.time() - perf_start_time logger.info( diff --git a/tests/test_cli.py b/tests/test_cli.py index 272ff13f..a940e409 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1804,6 +1804,7 @@ def test_cli_stdin_save_html_without_images(tmp_path: Path): """save-html needs to seek for images, so they should be left out when reading from stdin.""" result = run_stdin("--stdin", "-o", str(tmp_path), "detect-content", "save-html") assert result.returncode == 0, result.stdout.decode(errors="replace") + assert b"save-html will not include images" in result.stdout assert (tmp_path / "stdin-Scenes.html").exists() assert not list(tmp_path.glob("*.jpg")) @@ -1822,9 +1823,16 @@ def test_cli_stdin_with_other_backend(): assert b"requires the pyav backend" in result.stdout -def test_cli_stdin_terminal(monkeypatch: pytest.MonkeyPatch): +@pytest.mark.parametrize( + "stdin", + [ + SimpleNamespace(isatty=lambda: True), + None, # No console, e.g. under pythonw.exe on Windows. + ], +) +def test_cli_stdin_terminal(monkeypatch: pytest.MonkeyPatch, stdin: SimpleNamespace | None): """Reading from a terminal would block forever, so it should be rejected.""" - monkeypatch.setattr("sys.stdin", SimpleNamespace(isatty=lambda: True)) + monkeypatch.setattr("sys.stdin", stdin) with pytest.raises(click.BadParameter, match="requires a video to be piped"): CliContext()._open_stdin_stream(frame_rate=None, backend=None) @@ -1859,6 +1867,7 @@ def test_cli_stdin_incompatible_commands(command: str): def test_cli_stdin_unreadable_container(): """An MP4 with its index at the end decodes no frames from a pipe, which shouldn't be silent.""" + # This relies on testvideo.mp4 having its moov atom at the end of the file. result = run_stdin("--stdin", "detect-content", video="tests/resources/testvideo.mp4") assert result.returncode != 0 assert b"Failed to read any frames from stdin" in result.stdout diff --git a/website/pages/changelog.md b/website/pages/changelog.md index fdee4d4e..d225226f 100644 --- a/website/pages/changelog.md +++ b/website/pages/changelog.md @@ -860,3 +860,4 @@ Development - [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) - [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