import cv2
import numpy as np
from datetime import datetime, timedelta

# --- CONFIGURATION ---
video_file = "ghostofatale - 2026-02-22_11-19-28.mp4"
start_time = datetime.strptime("11:19:28", "%H:%M:%S")

# ROI Paint (y, x). On prend un carré de 50x50 pour moyenner la lumière
ry, rx, h, w = 306, 0, 50, 50

# Tes segments de timestamps (HH:MM:SS)
segments = [
    ("00:41:12", "00:51:12"),
    ("01:28:16", "01:34:41"),
    ("05:02:17", "05:14:02"),
    ("07:02:07", "07:15:00")
]

def to_sec(t):
    h, m, s = map(int, t.split(':'))
    return h*3600 + m*60 + s

cap = cv2.VideoCapture(video_file)
if not cap.isOpened():
    print("Erreur : Impossible d'ouvrir la vidéo. Vérifie le nom du fichier.")
    exit()

print(f"{'Heure Réelle':<15} | {'Luminosité':<10} | {'Segment'}")
print("-" * 45)

results = []

for s_start, s_end in segments:
    curr = to_sec(s_start)
    limit = to_sec(s_end)
    cap.set(cv2.CAP_PROP_POS_MSEC, curr * 1000)
    
    while curr <= limit:
        ret, frame = cap.read()
        if not ret: break
        
        # Découpe de la zone vitre
        roi = frame[ry:ry+h, rx:rx+w]
        # Calcul de la clarté moyenne (0-255)
        brightness = np.mean(cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY))
        
        real_t = (start_time + timedelta(seconds=curr)).strftime("%H:%M:%S")
        print(f"{real_t:<15} | {brightness:>10.2f} | {s_start}")
        results.append((real_t, brightness))
        
        # On avance de 30 secondes pour une lecture rapide en CLI
        curr += 30
        cap.set(cv2.CAP_PROP_POS_MSEC, curr * 1000)

cap.release()
