Skip to content

Commit fe6287c

Browse files
authored
Refuse an unreadable layout frame rate instead of assuming 30 fps. (#201)
A video that reports 0 fps used to be checked as if it were 30 fps.
1 parent 3812765 commit fe6287c

2 files changed

Lines changed: 80 additions & 2 deletions

File tree

‎src/docgen/manim_layout.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
from __future__ import annotations
44

5+
import math
56
from dataclasses import dataclass, field
67
from pathlib import Path
7-
from typing import TYPE_CHECKING
8+
from typing import Any, TYPE_CHECKING
89

910
if TYPE_CHECKING:
1011
from docgen.config import Config
@@ -24,6 +25,19 @@ class LayoutReport:
2425
passed: bool = True
2526

2627

28+
def _require_positive_fps(cap: Any, raw: float) -> float:
29+
"""A zero or missing frame rate must not be treated as 30 fps."""
30+
try:
31+
fps = float(raw)
32+
except (TypeError, ValueError) as exc:
33+
cap.release()
34+
raise ValueError("video frame rate is not a number") from exc
35+
if not math.isfinite(fps) or fps <= 0:
36+
cap.release()
37+
raise ValueError("video frame rate must be a positive finite number")
38+
return fps
39+
40+
2741
class LayoutValidator:
2842
def __init__(self, config: Config) -> None:
2943
self.config = config
@@ -45,7 +59,7 @@ def validate_video(self, video_path: str | Path) -> LayoutReport:
4559
report.issues.append(LayoutIssue(0, "error", "Could not open video"))
4660
return report
4761

48-
fps = cap.get(cv2.CAP_PROP_FPS) or 30
62+
fps = _require_positive_fps(cap, cap.get(cv2.CAP_PROP_FPS))
4963
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
5064
duration = total / fps if fps > 0 else 0
5165
h_frame = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

‎tests/test_manim_layout.py‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""Layout checks must not invent a frame rate when the video does not report one."""
2+
3+
from __future__ import annotations
4+
5+
from pathlib import Path
6+
7+
import cv2
8+
import pytest
9+
import yaml
10+
11+
from docgen.config import Config
12+
from docgen.manim_layout import LayoutValidator
13+
14+
15+
def _cfg(tmp_path: Path) -> Config:
16+
raw = {"segments": {"all": ["01"], "default": ["01"]}}
17+
path = tmp_path / "docgen.yaml"
18+
path.write_text(yaml.dump(raw), encoding="utf-8")
19+
return Config.from_yaml(path)
20+
21+
22+
class _Cap:
23+
def __init__(self, *_args: object, **_kwargs: object) -> None:
24+
self.fps = 0.0
25+
self.frames = 0.0
26+
self.sampled = False
27+
28+
def isOpened(self) -> bool:
29+
return True
30+
31+
def get(self, prop: int) -> float:
32+
if prop == cv2.CAP_PROP_FRAME_COUNT:
33+
return self.frames
34+
if prop in (cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FRAME_WIDTH):
35+
return 100.0
36+
return self.fps
37+
38+
def set(self, *_args: object) -> None:
39+
self.sampled = True
40+
41+
def read(self) -> tuple[bool, None]:
42+
self.sampled = True
43+
return False, None
44+
45+
def release(self) -> None:
46+
return None
47+
48+
49+
def test_zero_fps_is_refused(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
50+
cap = _Cap()
51+
monkeypatch.setattr(cv2, "VideoCapture", lambda *_a, **_k: cap)
52+
with pytest.raises(ValueError, match="positive finite"):
53+
LayoutValidator(_cfg(tmp_path)).validate_video(tmp_path / "clip.mp4")
54+
assert cap.sampled is False
55+
56+
57+
def test_positive_fps_still_samples(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
58+
cap = _Cap()
59+
cap.fps = 30.0
60+
cap.frames = 90.0
61+
monkeypatch.setattr(cv2, "VideoCapture", lambda *_a, **_k: cap)
62+
report = LayoutValidator(_cfg(tmp_path)).validate_video(tmp_path / "clip.mp4")
63+
assert cap.sampled is True
64+
assert report.passed is True

0 commit comments

Comments
 (0)