From b5e4536d935bccc6dbea7f92a32ad09e02e507ee Mon Sep 17 00:00:00 2001 From: Suvrakamal Das Date: Sat, 14 Nov 2026 07:03:25 +0000 Subject: [PATCH] fix(ffmpeg): detach FFmpeg children from the caller's standard input FFmpeg reads interactive commands from standard input: 'q' stops it with exit status 0 and other bytes are discarded. Frame extraction, contact sheets, frame statistics, synthetic episodes and the LeRobot transcode inherited the caller's standard input, so a shell read loop or a typed key could truncate their output or lose the loop's input. Give them /dev/null or -nostdin, like the other FFmpeg call sites. --- .../_video_measurements/_frame_statistics.py | 1 + src/hflow/episode.py | 5 +- src/hflow/ffmpeg/_contact_sheet.py | 1 + src/hflow/importers/lerobot.py | 4 +- src/hflow/testing.py | 2 +- tests/test_ffmpeg_standard_input.py | 102 ++++++++++++++++++ 6 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 tests/test_ffmpeg_standard_input.py diff --git a/src/hflow/_video_measurements/_frame_statistics.py b/src/hflow/_video_measurements/_frame_statistics.py index 06d79a69e..d574b466c 100644 --- a/src/hflow/_video_measurements/_frame_statistics.py +++ b/src/hflow/_video_measurements/_frame_statistics.py @@ -747,6 +747,7 @@ def measure_video_frame_statistics( with tempfile.TemporaryFile(mode="w+", encoding="utf-8") as standard_error_file: instrument_process = subprocess.Popen( command, + stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=standard_error_file, text=True, diff --git a/src/hflow/episode.py b/src/hflow/episode.py index 19acc3ad0..539b87480 100644 --- a/src/hflow/episode.py +++ b/src/hflow/episode.py @@ -891,7 +891,9 @@ def frames( if end_s is not None: command += ["-to", f"{end_s:.6f}"] command.append(str(staging_dir / "decoded.framecrc")) - completed = subprocess.run(command, capture_output=True, text=True, check=False) + completed = subprocess.run( + command, stdin=subprocess.DEVNULL, capture_output=True, text=True, check=False + ) staged_frames = sorted(staging_dir.glob("frame_*.jpg")) if completed.returncode != 0 or not staged_frames: stderr_tail = completed.stderr.strip().splitlines()[-5:] @@ -1054,6 +1056,7 @@ def frames_at_indices( ] completed_process = subprocess.run( command, + stdin=subprocess.DEVNULL, capture_output=True, text=True, check=False, diff --git a/src/hflow/ffmpeg/_contact_sheet.py b/src/hflow/ffmpeg/_contact_sheet.py index bea468fa1..565ee8fa7 100644 --- a/src/hflow/ffmpeg/_contact_sheet.py +++ b/src/hflow/ffmpeg/_contact_sheet.py @@ -179,6 +179,7 @@ def contact_sheet( str(ffmpeg_binary), "-hide_banner", "-nostats", + "-nostdin", "-y", "-f", "concat", diff --git a/src/hflow/importers/lerobot.py b/src/hflow/importers/lerobot.py index 3dccdbe0e..cb0b1e1b6 100644 --- a/src/hflow/importers/lerobot.py +++ b/src/hflow/importers/lerobot.py @@ -384,7 +384,9 @@ def _transcode_mp4_to_h264( "h264", "pipe:1", ] - completed_process = subprocess.run(ffmpeg_command, capture_output=True, timeout=600) + completed_process = subprocess.run( + ffmpeg_command, stdin=subprocess.DEVNULL, capture_output=True, timeout=600 + ) if completed_process.returncode != 0: raise RuntimeError( "ffmpeg transcode failed: " + completed_process.stderr.decode(errors="ignore") diff --git a/src/hflow/testing.py b/src/hflow/testing.py index 6adcb83a7..465e7edd9 100644 --- a/src/hflow/testing.py +++ b/src/hflow/testing.py @@ -256,7 +256,7 @@ def _time_is_in_segment(time_s: float, segment: tuple[float, float] | None) -> b def _run_ffmpeg(arguments: list[str]) -> None: command = [str(ffmpeg_path()), "-hide_banner", "-loglevel", "error", "-y", *arguments] - completed = subprocess.run(command, capture_output=True, check=False) + completed = subprocess.run(command, stdin=subprocess.DEVNULL, capture_output=True, check=False) if completed.returncode != 0: raise RuntimeError( f"ffmpeg failed ({' '.join(command)}): {completed.stderr.decode(errors='replace')}" diff --git a/tests/test_ffmpeg_standard_input.py b/tests/test_ffmpeg_standard_input.py new file mode 100644 index 000000000..56b4c7d79 --- /dev/null +++ b/tests/test_ffmpeg_standard_input.py @@ -0,0 +1,102 @@ +"""FFmpeg reads interactive commands from standard input (``q`` stops it with exit status 0).""" + +import os +from collections.abc import Callable, Iterator +from pathlib import Path + +import pytest +from episode_test_helpers import synthesize_canonical_episode + +import hflow.importers.lerobot as lerobot_importer +from hflow._video_measurement_toolchain import resolved_video_measurement_toolchain +from hflow._video_measurements._frame_statistics import measure_video_frame_statistics +from hflow.episode import Episode +from hflow.ffmpeg import contact_sheet +from hflow.testing import SyntheticEpisodeSpec, synthesize_episode + +_STANDARD_INPUT_DESCRIPTOR = 0 +_CAMERA_TOPIC = "/wrist_cam/compressed" +_SPEC = SyntheticEpisodeSpec( + duration_s=10.0, + cameras=("wrist_cam",), + image_hz=30.0, + image_width=640, + image_height=480, + black_segment=None, + timestamp_offset_segment=None, +) +_SOURCE_FRAME_COUNT = 300 + + +@pytest.fixture(scope="module") +def canonical_episode(tmp_path_factory: pytest.TempPathFactory) -> Path: + return synthesize_canonical_episode(tmp_path_factory.mktemp("standard-input"), _SPEC) + + +@pytest.fixture +def consumed_quit_command_count(tmp_path: Path) -> Iterator[Callable[[], int]]: + quit_commands_path = tmp_path / "standard-input" + quit_commands_path.write_bytes(b"q" * 4096) + quit_commands = os.open(quit_commands_path, os.O_RDONLY) + original_standard_input = os.dup(_STANDARD_INPUT_DESCRIPTOR) + os.dup2(quit_commands, _STANDARD_INPUT_DESCRIPTOR) + try: + yield lambda: os.lseek(quit_commands, 0, os.SEEK_CUR) + finally: + os.dup2(original_standard_input, _STANDARD_INPUT_DESCRIPTOR) + os.close(original_standard_input) + os.close(quit_commands) + + +@pytest.mark.requires_system_ffmpeg +def test_frame_extraction_is_complete_and_leaves_standard_input_unread( + canonical_episode: Path, tmp_path: Path, consumed_quit_command_count: Callable[[], int] +) -> None: + with Episode(canonical_episode, workdir=tmp_path / "workdir") as episode: + sampled_frames = episode.frames(_CAMERA_TOPIC, fps=10.0) + indexed_frames = episode.frames_at_indices( + _CAMERA_TOPIC, frame_indices=[0, _SOURCE_FRAME_COUNT - 1] + ) + sheet = contact_sheet(sampled_frames, tmp_path / "sheet.jpg") + + assert len(sampled_frames) == 100 + assert sampled_frames[-1].log_time_ns == _SPEC.start_time_ns + 9_900_000_000 + assert len(indexed_frames) == 2 + assert sheet.path.stat().st_size > 0 + assert consumed_quit_command_count() == 0 + + +@pytest.mark.requires_system_ffmpeg +def test_frame_statistics_cover_the_whole_video_and_leave_standard_input_unread( + canonical_episode: Path, tmp_path: Path, consumed_quit_command_count: Callable[[], int] +) -> None: + with Episode(canonical_episode, workdir=tmp_path / "workdir") as episode: + statistics = measure_video_frame_statistics( + episode.video(_CAMERA_TOPIC), toolchain=resolved_video_measurement_toolchain() + ) + + assert statistics.decoded_frame_count == _SOURCE_FRAME_COUNT + assert consumed_quit_command_count() == 0 + + +@pytest.mark.requires_system_ffmpeg +def test_synthetic_episode_renders_and_leaves_standard_input_unread( + tmp_path: Path, consumed_quit_command_count: Callable[[], int] +) -> None: + source = synthesize_episode(tmp_path / "episode.mcap", _SPEC) + + assert source.stat().st_size > 0 + assert consumed_quit_command_count() == 0 + + +@pytest.mark.requires_system_ffmpeg +def test_lerobot_transcode_keeps_every_frame_and_leaves_standard_input_unread( + canonical_episode: Path, tmp_path: Path, consumed_quit_command_count: Callable[[], int] +) -> None: + with Episode(canonical_episode, workdir=tmp_path / "workdir") as episode: + access_units = lerobot_importer._transcode_mp4_to_h264( + episode.video(_CAMERA_TOPIC), gop_seconds=1.0, frames_per_second=_SPEC.image_hz + ) + + assert len(access_units) == _SOURCE_FRAME_COUNT + assert consumed_quit_command_count() == 0