import cv2 import os class VideoService: @staticmethod def verify_integrity(video_path: str): """ Analyzes the physical properties of the biometric stream. [cite: 2026-02-23] Enhanced to handle 'headless' webm headers for stable timeline scaling. """ if not os.path.exists(video_path): return {"error": "File not found"} cap = cv2.VideoCapture(video_path) fps_claimed = cap.get(cv2.CAP_PROP_FPS) # 🎯 FIX: Robust Frame Counting [cite: 2026-02-23] # Some browsers don't write the frame count to the webm header. frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) if frame_count <= 0: # Fallback: Count frames manually if header is missing [cite: 2026-02-23] count = 0 while True: ret, _ = cap.read() if not ret: break count += 1 frame_count = count # Reset capture to the beginning for metadata checks cap.set(cv2.CAP_PROP_POS_FRAMES, 0) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 🎯 STABILITY: Default to 30fps if browser metadata is wonky [cite: 2026-02-23] effective_fps = fps_claimed if 5 < fps_claimed < 120 else 30 duration = frame_count / effective_fps if frame_count > 0 else 0 cap.release() # Clinical Integrity Logic [cite: 2026-02-21] integrity = "OPTIMAL" warning = None if effective_fps < 24: integrity = "SUBOPTIMAL" warning = "Low frame rate. Ocular accuracy is indicative only." if effective_fps < 12: integrity = "CRITICAL" warning = "Extreme frame drop. Biometric path is statistically insignificant." return { "fps": round(effective_fps, 2), "frame_count": frame_count, "duration": round(duration, 2), # 🚀 Syncs with Frontend Timeline "resolution": f"{width}x{height}", "integrity": integrity, "warning": warning }