|
| 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