Skip to content

Commit b2b3559

Browse files
authored
fix(ffmpeg): detach FFmpeg children from the caller's standard input (#784)
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.
1 parent 099af7f commit b2b3559

6 files changed

Lines changed: 112 additions & 3 deletions

File tree

‎src/hflow/_video_measurements/_frame_statistics.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ def measure_video_frame_statistics(
747747
with tempfile.TemporaryFile(mode="w+", encoding="utf-8") as standard_error_file:
748748
instrument_process = subprocess.Popen(
749749
command,
750+
stdin=subprocess.DEVNULL,
750751
stdout=subprocess.PIPE,
751752
stderr=standard_error_file,
752753
text=True,

‎src/hflow/episode.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,9 @@ def frames(
891891
if end_s is not None:
892892
command += ["-to", f"{end_s:.6f}"]
893893
command.append(str(staging_dir / "decoded.framecrc"))
894-
completed = subprocess.run(command, capture_output=True, text=True, check=False)
894+
completed = subprocess.run(
895+
command, stdin=subprocess.DEVNULL, capture_output=True, text=True, check=False
896+
)
895897
staged_frames = sorted(staging_dir.glob("frame_*.jpg"))
896898
if completed.returncode != 0 or not staged_frames:
897899
stderr_tail = completed.stderr.strip().splitlines()[-5:]
@@ -1054,6 +1056,7 @@ def frames_at_indices(
10541056
]
10551057
completed_process = subprocess.run(
10561058
command,
1059+
stdin=subprocess.DEVNULL,
10571060
capture_output=True,
10581061
text=True,
10591062
check=False,

‎src/hflow/ffmpeg/_contact_sheet.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ def contact_sheet(
179179
str(ffmpeg_binary),
180180
"-hide_banner",
181181
"-nostats",
182+
"-nostdin",
182183
"-y",
183184
"-f",
184185
"concat",

‎src/hflow/importers/lerobot.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@ def _transcode_mp4_to_h264(
384384
"h264",
385385
"pipe:1",
386386
]
387-
completed_process = subprocess.run(ffmpeg_command, capture_output=True, timeout=600)
387+
completed_process = subprocess.run(
388+
ffmpeg_command, stdin=subprocess.DEVNULL, capture_output=True, timeout=600
389+
)
388390
if completed_process.returncode != 0:
389391
raise RuntimeError(
390392
"ffmpeg transcode failed: " + completed_process.stderr.decode(errors="ignore")

‎src/hflow/testing.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def _time_is_in_segment(time_s: float, segment: tuple[float, float] | None) -> b
256256

257257
def _run_ffmpeg(arguments: list[str]) -> None:
258258
command = [str(ffmpeg_path()), "-hide_banner", "-loglevel", "error", "-y", *arguments]
259-
completed = subprocess.run(command, capture_output=True, check=False)
259+
completed = subprocess.run(command, stdin=subprocess.DEVNULL, capture_output=True, check=False)
260260
if completed.returncode != 0:
261261
raise RuntimeError(
262262
f"ffmpeg failed ({' '.join(command)}): {completed.stderr.decode(errors='replace')}"
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""FFmpeg reads interactive commands from standard input (``q`` stops it with exit status 0)."""
2+
3+
import os
4+
from collections.abc import Callable, Iterator
5+
from pathlib import Path
6+
7+
import pytest
8+
from episode_test_helpers import synthesize_canonical_episode
9+
10+
import hflow.importers.lerobot as lerobot_importer
11+
from hflow._video_measurement_toolchain import resolved_video_measurement_toolchain
12+
from hflow._video_measurements._frame_statistics import measure_video_frame_statistics
13+
from hflow.episode import Episode
14+
from hflow.ffmpeg import contact_sheet
15+
from hflow.testing import SyntheticEpisodeSpec, synthesize_episode
16+
17+
_STANDARD_INPUT_DESCRIPTOR = 0
18+
_CAMERA_TOPIC = "/wrist_cam/compressed"
19+
_SPEC = SyntheticEpisodeSpec(
20+
duration_s=10.0,
21+
cameras=("wrist_cam",),
22+
image_hz=30.0,
23+
image_width=640,
24+
image_height=480,
25+
black_segment=None,
26+
timestamp_offset_segment=None,
27+
)
28+
_SOURCE_FRAME_COUNT = 300
29+
30+
31+
@pytest.fixture(scope="module")
32+
def canonical_episode(tmp_path_factory: pytest.TempPathFactory) -> Path:
33+
return synthesize_canonical_episode(tmp_path_factory.mktemp("standard-input"), _SPEC)
34+
35+
36+
@pytest.fixture
37+
def consumed_quit_command_count(tmp_path: Path) -> Iterator[Callable[[], int]]:
38+
quit_commands_path = tmp_path / "standard-input"
39+
quit_commands_path.write_bytes(b"q" * 4096)
40+
quit_commands = os.open(quit_commands_path, os.O_RDONLY)
41+
original_standard_input = os.dup(_STANDARD_INPUT_DESCRIPTOR)
42+
os.dup2(quit_commands, _STANDARD_INPUT_DESCRIPTOR)
43+
try:
44+
yield lambda: os.lseek(quit_commands, 0, os.SEEK_CUR)
45+
finally:
46+
os.dup2(original_standard_input, _STANDARD_INPUT_DESCRIPTOR)
47+
os.close(original_standard_input)
48+
os.close(quit_commands)
49+
50+
51+
@pytest.mark.requires_system_ffmpeg
52+
def test_frame_extraction_is_complete_and_leaves_standard_input_unread(
53+
canonical_episode: Path, tmp_path: Path, consumed_quit_command_count: Callable[[], int]
54+
) -> None:
55+
with Episode(canonical_episode, workdir=tmp_path / "workdir") as episode:
56+
sampled_frames = episode.frames(_CAMERA_TOPIC, fps=10.0)
57+
indexed_frames = episode.frames_at_indices(
58+
_CAMERA_TOPIC, frame_indices=[0, _SOURCE_FRAME_COUNT - 1]
59+
)
60+
sheet = contact_sheet(sampled_frames, tmp_path / "sheet.jpg")
61+
62+
assert len(sampled_frames) == 100
63+
assert sampled_frames[-1].log_time_ns == _SPEC.start_time_ns + 9_900_000_000
64+
assert len(indexed_frames) == 2
65+
assert sheet.path.stat().st_size > 0
66+
assert consumed_quit_command_count() == 0
67+
68+
69+
@pytest.mark.requires_system_ffmpeg
70+
def test_frame_statistics_cover_the_whole_video_and_leave_standard_input_unread(
71+
canonical_episode: Path, tmp_path: Path, consumed_quit_command_count: Callable[[], int]
72+
) -> None:
73+
with Episode(canonical_episode, workdir=tmp_path / "workdir") as episode:
74+
statistics = measure_video_frame_statistics(
75+
episode.video(_CAMERA_TOPIC), toolchain=resolved_video_measurement_toolchain()
76+
)
77+
78+
assert statistics.decoded_frame_count == _SOURCE_FRAME_COUNT
79+
assert consumed_quit_command_count() == 0
80+
81+
82+
@pytest.mark.requires_system_ffmpeg
83+
def test_synthetic_episode_renders_and_leaves_standard_input_unread(
84+
tmp_path: Path, consumed_quit_command_count: Callable[[], int]
85+
) -> None:
86+
source = synthesize_episode(tmp_path / "episode.mcap", _SPEC)
87+
88+
assert source.stat().st_size > 0
89+
assert consumed_quit_command_count() == 0
90+
91+
92+
@pytest.mark.requires_system_ffmpeg
93+
def test_lerobot_transcode_keeps_every_frame_and_leaves_standard_input_unread(
94+
canonical_episode: Path, tmp_path: Path, consumed_quit_command_count: Callable[[], int]
95+
) -> None:
96+
with Episode(canonical_episode, workdir=tmp_path / "workdir") as episode:
97+
access_units = lerobot_importer._transcode_mp4_to_h264(
98+
episode.video(_CAMERA_TOPIC), gop_seconds=1.0, frames_per_second=_SPEC.image_hz
99+
)
100+
101+
assert len(access_units) == _SOURCE_FRAME_COUNT
102+
assert consumed_quit_command_count() == 0

0 commit comments

Comments
 (0)