|
| 1 | +"""VideoFrames – container for captured viewer frames with export helpers.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from typing import Any, List, Optional |
| 5 | + |
| 6 | +import numpy as np |
| 7 | + |
| 8 | + |
| 9 | +class VideoFrames: |
| 10 | + """Container for captured video frames with per-frame metadata. |
| 11 | +
|
| 12 | + Stores RGB numpy arrays together with timestamps so that export |
| 13 | + can use either a fixed frame rate or timing derived from the |
| 14 | + actual ping timestamps. |
| 15 | +
|
| 16 | + Examples |
| 17 | + -------- |
| 18 | + >>> viewer.frames.export_avif("out.avif", fps=10) |
| 19 | + >>> viewer.frames.export_mp4("out.mp4", fps=25) |
| 20 | + >>> viewer.frames.export_avif("out.avif", ping_time_speed=3.0) |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self) -> None: |
| 24 | + self._frames: List[np.ndarray] = [] # RGB uint8 arrays |
| 25 | + self._timestamps: List[Optional[float]] = [] # unix timestamps per frame |
| 26 | + |
| 27 | + # -- mutation ---------------------------------------------------------- |
| 28 | + |
| 29 | + def clear(self) -> None: |
| 30 | + """Remove all stored frames.""" |
| 31 | + self._frames.clear() |
| 32 | + self._timestamps.clear() |
| 33 | + |
| 34 | + def append(self, frame: np.ndarray, timestamp: Optional[float] = None) -> None: |
| 35 | + """Append a single RGB frame with optional ping timestamp.""" |
| 36 | + self._frames.append(frame) |
| 37 | + self._timestamps.append(timestamp) |
| 38 | + |
| 39 | + # -- properties -------------------------------------------------------- |
| 40 | + |
| 41 | + def __len__(self) -> int: |
| 42 | + return len(self._frames) |
| 43 | + |
| 44 | + def __getitem__(self, idx: int) -> np.ndarray: |
| 45 | + return self._frames[idx] |
| 46 | + |
| 47 | + @property |
| 48 | + def frames(self) -> List[np.ndarray]: |
| 49 | + """All stored RGB frames.""" |
| 50 | + return self._frames |
| 51 | + |
| 52 | + @property |
| 53 | + def timestamps(self) -> List[Optional[float]]: |
| 54 | + """Per-frame timestamps (may contain None).""" |
| 55 | + return self._timestamps |
| 56 | + |
| 57 | + # -- timing helpers ---------------------------------------------------- |
| 58 | + |
| 59 | + def _compute_durations(self, speed: float = 1.0) -> List[float]: |
| 60 | + """Compute per-frame durations from ping timestamps. |
| 61 | +
|
| 62 | + Parameters |
| 63 | + ---------- |
| 64 | + speed : float |
| 65 | + Speed multiplier applied to the real time gaps. |
| 66 | + ``speed=3`` means 3× real-time. |
| 67 | +
|
| 68 | + Returns |
| 69 | + ------- |
| 70 | + list of float |
| 71 | + Duration in seconds for each frame transition. |
| 72 | + """ |
| 73 | + durations: List[float] = [] |
| 74 | + for i in range(1, len(self._timestamps)): |
| 75 | + t_prev = self._timestamps[i - 1] |
| 76 | + t_cur = self._timestamps[i] |
| 77 | + if t_prev is not None and t_cur is not None: |
| 78 | + dt = abs(t_cur - t_prev) / max(speed, 0.001) |
| 79 | + durations.append(max(0.01, dt)) |
| 80 | + else: |
| 81 | + durations.append(0.1) # fallback 100 ms |
| 82 | + return durations |
| 83 | + |
| 84 | + # -- export ------------------------------------------------------------ |
| 85 | + |
| 86 | + def export_avif( |
| 87 | + self, |
| 88 | + filename: str = "video.avif", |
| 89 | + fps: Optional[float] = None, |
| 90 | + ping_time_speed: Optional[float] = None, |
| 91 | + quality: int = 75, |
| 92 | + loop: int = 0, |
| 93 | + ) -> str: |
| 94 | + """Export frames as animated AVIF. |
| 95 | +
|
| 96 | + Parameters |
| 97 | + ---------- |
| 98 | + filename : str |
| 99 | + Output path. |
| 100 | + fps : float, optional |
| 101 | + Fixed frame rate. Ignored when *ping_time_speed* is set. |
| 102 | + ping_time_speed : float, optional |
| 103 | + Use real ping timestamps scaled by this speed factor |
| 104 | + (e.g. 3.0 = 3× real-time). |
| 105 | + quality : int |
| 106 | + AVIF quality 1–100. |
| 107 | + loop : int |
| 108 | + Number of loops (0 = infinite). |
| 109 | +
|
| 110 | + Returns |
| 111 | + ------- |
| 112 | + str |
| 113 | + The filename that was written. |
| 114 | + """ |
| 115 | + if len(self._frames) == 0: |
| 116 | + raise ValueError("No frames to export") |
| 117 | + |
| 118 | + try: |
| 119 | + import pillow_avif # noqa: F401 |
| 120 | + except ImportError: |
| 121 | + raise ImportError("pip install pillow-avif-plugin") |
| 122 | + from PIL import Image |
| 123 | + |
| 124 | + pil_frames = [Image.fromarray(f) for f in self._frames] |
| 125 | + |
| 126 | + if ping_time_speed is not None: |
| 127 | + durations = self._compute_durations(speed=ping_time_speed) |
| 128 | + duration_ms: Any = [int(d * 1000) for d in durations] |
| 129 | + # first frame needs a duration too |
| 130 | + duration_ms.insert(0, duration_ms[0] if duration_ms else 100) |
| 131 | + elif fps is not None: |
| 132 | + duration_ms = int(1000 / max(fps, 0.1)) |
| 133 | + else: |
| 134 | + duration_ms = 100 # default 10 fps |
| 135 | + |
| 136 | + pil_frames[0].save( |
| 137 | + filename, |
| 138 | + save_all=True, |
| 139 | + append_images=pil_frames[1:], |
| 140 | + duration=duration_ms, |
| 141 | + loop=loop, |
| 142 | + quality=quality, |
| 143 | + ) |
| 144 | + return filename |
| 145 | + |
| 146 | + def export_mp4( |
| 147 | + self, |
| 148 | + filename: str = "video.mp4", |
| 149 | + fps: Optional[float] = None, |
| 150 | + ping_time_speed: Optional[float] = None, |
| 151 | + codec: str = "libx264", |
| 152 | + quality: int = 8, |
| 153 | + ) -> str: |
| 154 | + """Export frames as MP4 video. |
| 155 | +
|
| 156 | + Parameters |
| 157 | + ---------- |
| 158 | + filename : str |
| 159 | + Output path. |
| 160 | + fps : float, optional |
| 161 | + Fixed frame rate. Ignored when *ping_time_speed* is set. |
| 162 | + ping_time_speed : float, optional |
| 163 | + Use real ping timestamps; the *average* resulting fps is |
| 164 | + passed to ffmpeg (per-frame variable rate is not supported |
| 165 | + by most containers). |
| 166 | + codec : str |
| 167 | + FFmpeg video codec. |
| 168 | + quality : int |
| 169 | + FFmpeg quality parameter. |
| 170 | +
|
| 171 | + Returns |
| 172 | + ------- |
| 173 | + str |
| 174 | + The filename that was written. |
| 175 | + """ |
| 176 | + if len(self._frames) == 0: |
| 177 | + raise ValueError("No frames to export") |
| 178 | + |
| 179 | + try: |
| 180 | + import imageio_ffmpeg # noqa: F401 |
| 181 | + import imageio |
| 182 | + except ImportError: |
| 183 | + raise ImportError("pip install imageio imageio-ffmpeg") |
| 184 | + |
| 185 | + if ping_time_speed is not None: |
| 186 | + durations = self._compute_durations(speed=ping_time_speed) |
| 187 | + avg_dur = sum(durations) / len(durations) if durations else 0.1 |
| 188 | + effective_fps = 1.0 / avg_dur if avg_dur > 0 else 10.0 |
| 189 | + elif fps is not None: |
| 190 | + effective_fps = max(fps, 0.1) |
| 191 | + else: |
| 192 | + effective_fps = 10.0 |
| 193 | + |
| 194 | + writer = imageio.get_writer(filename, fps=effective_fps, codec=codec, quality=quality) |
| 195 | + for frame in self._frames: |
| 196 | + writer.append_data(frame) |
| 197 | + writer.close() |
| 198 | + return filename |
| 199 | + |
| 200 | + def __repr__(self) -> str: |
| 201 | + ts = [t for t in self._timestamps if t is not None] |
| 202 | + dt_str = "" |
| 203 | + if len(ts) >= 2: |
| 204 | + total = ts[-1] - ts[0] |
| 205 | + dt_str = f", span={total:.1f}s" |
| 206 | + return f"VideoFrames({len(self._frames)} frames{dt_str})" |
0 commit comments