Skip to content

Commit fa97bc1

Browse files
fix(lerobot): compare source video times in presentation order (#782)
ffprobe lists packets in decode order, so an H.264 source with B-frames (libx264's default) reports non-monotonic pts_time values. The importer compares that list index by index with the parquet timestamps, so every such episode was refused with a timestamp disagreement at frame 1.
1 parent 93ad1b5 commit fa97bc1

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎src/hflow/importers/lerobot.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ def _get_video_pts_times(mp4_path: Path) -> list[float]:
417417
line = line.strip()
418418
if line and line != "N/A":
419419
times.append(float(line))
420+
# ffprobe lists packets in decode order, so a source with B-frames reports
421+
# non-monotonic presentation times. Frames (and the parquet rows they are
422+
# checked against) are in presentation order.
423+
times.sort()
420424
return times
421425

422426

‎tests/test_lerobot_converter.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,68 @@ def _episode_video_access_units(mcap: Path, camera_key: str) -> "list[bytes]":
649649
return units
650650

651651

652+
@_requires_system_ffmpeg
653+
@pytest.mark.requires_system_ffmpeg
654+
def test_video_pts_times_are_in_presentation_order_for_b_frame_sources(
655+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
656+
) -> None:
657+
"""A source encoded with B-frames (libx264's default, and what an H.264
658+
LeRobot dataset carries) stores packets in decode order. The packet times
659+
are matched index-by-index against the parquet rows, so they must come
660+
back in presentation order or frame 1 is compared with a later frame.
661+
"""
662+
assert _FFMPEG is not None and _FFPROBE is not None
663+
source = tmp_path / "source.mp4"
664+
subprocess.run(
665+
[
666+
_FFMPEG,
667+
"-hide_banner",
668+
"-y",
669+
"-f",
670+
"lavfi",
671+
"-i",
672+
"testsrc2=size=160x120:rate=30:duration=2,format=yuv420p",
673+
"-c:v",
674+
"libx264",
675+
"-preset",
676+
"medium",
677+
"-bf",
678+
"3",
679+
str(source),
680+
],
681+
capture_output=True,
682+
check=True,
683+
)
684+
decode_order = subprocess.run(
685+
[
686+
_FFPROBE,
687+
"-v",
688+
"error",
689+
"-select_streams",
690+
"v:0",
691+
"-show_entries",
692+
"packet=pts_time",
693+
"-of",
694+
"csv=p=0",
695+
str(source),
696+
],
697+
capture_output=True,
698+
text=True,
699+
check=True,
700+
).stdout.split()
701+
decode_order_times = [float(time) for time in decode_order]
702+
assert decode_order_times != sorted(decode_order_times), "source has no B-frames"
703+
704+
system_ffprobe_path = Path(_FFPROBE)
705+
monkeypatch.setattr(prep, "ffprobe_path", lambda: system_ffprobe_path)
706+
707+
times = prep._get_video_pts_times(source)
708+
assert times == pytest.approx([index / 30 for index in range(60)], abs=1e-3)
709+
710+
rebased = prep._relative_video_pts_times(source, 1.0, 1.5, 15, 30.0)
711+
assert rebased == pytest.approx([index / 30 for index in range(15)], abs=1e-3)
712+
713+
652714
@_requires_system_ffmpeg
653715
@pytest.mark.requires_system_ffmpeg
654716
def test_converter_slices_exactly_the_declared_frame_count(

0 commit comments

Comments
 (0)