Add FPS display feature and update configuration for display settings

This commit is contained in:
2026-05-07 19:56:04 +02:00
parent b65d0e2130
commit d117be5eec
4 changed files with 66 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
from __future__ import annotations
import json
import time
from datetime import datetime
from pathlib import Path
from typing import Any
@@ -54,6 +55,10 @@ class VideoRecorder:
self.path: Path | None = None
self.writer: cv2.VideoWriter | None = None
self.started_at: str | None = None
self.started_monotonic: float | None = None
self.fps = 0.0
self.frames_written = 0
self.last_frame: np.ndarray | None = None
@property
def is_recording(self) -> bool:
@@ -66,24 +71,37 @@ class VideoRecorder:
capture_cfg = self.config["capture"]
self.path = MediaStore(self.config, self.app_config).video_path()
h, w = frame_bgr.shape[:2]
fps = float(self.config["camera"].get("fps", 30))
self.fps = float(self.config["camera"].get("fps", 30))
codec = str(capture_cfg.get("video_codec", "mp4v"))
fourcc = cv2.VideoWriter_fourcc(*codec[:4])
self.writer = cv2.VideoWriter(str(self.path), fourcc, fps, (w, h))
self.writer = cv2.VideoWriter(str(self.path), fourcc, self.fps, (w, h))
if not self.writer.isOpened():
self.writer = None
raise RuntimeError("Nie mozna uruchomic zapisu wideo")
self.started_at = datetime.now().isoformat(timespec="seconds")
self.started_monotonic = time.monotonic()
self.frames_written = 0
self.last_frame = None
self.write(frame_bgr)
return self.path
def write(self, frame_bgr: np.ndarray) -> None:
if self.writer is not None:
self.writer.write(frame_bgr)
if self.writer is None or self.started_monotonic is None:
return
elapsed = max(0.0, time.monotonic() - self.started_monotonic)
target_frames = max(1, int(elapsed * self.fps) + 1)
frame = frame_bgr.copy()
while self.frames_written < target_frames:
self.writer.write(frame)
self.frames_written += 1
self.last_frame = frame
def stop(self, metadata: dict[str, Any]) -> Path | None:
if self.writer is None:
return None
if self.last_frame is not None:
self.write(self.last_frame)
self.writer.release()
self.writer = None
path = self.path
@@ -93,9 +111,15 @@ class VideoRecorder:
"recording": {
"started_at": self.started_at,
"stopped_at": datetime.now().isoformat(timespec="seconds"),
"fps": self.fps,
"frames_written": self.frames_written,
},
}
write_metadata(path, metadata)
self.path = None
self.started_at = None
self.started_monotonic = None
self.fps = 0.0
self.frames_written = 0
self.last_frame = None
return path