#!/opt/homebrew/bin/python3.11
"""
Magnifying Glass: High-density frame extraction + Florence-2 AI search for stamps
Extracts frames at 1fps around key dialogue moments for thorough analysis.
"""

import cv2
import os
import subprocess

UUID = "384b0ff44aaaa1f1"
VIDEO_PATH = f"output/{UUID}/{UUID}.mp4"
OUTPUT_DIR = f"output/{UUID}/magnifying_glass"
os.makedirs(OUTPUT_DIR, exist_ok=True)

# Key scenes from ASR dialogue - extract 10 seconds before and after at 1fps
KEY_SCENES = [
    (5509, 5529, "envelope_stamp"),  # "The envelope, but the stamp's on it."
    (5720, 5740, "valuable_stamp"),  # "It's the most valuable stamp in the world."
    (5850, 5870, "stamps_on_letter"),  # "It was the stamps on the letter..."
    (6259, 6285, "bring_stamps"),  # "Just bring those stamps over here."
    (6641, 6672, "turn_in_stamps"),  # "and turn in those stamps."
    (6746, 6767, "give_me_stamp"),  # "Now, come on. Give me the stamp."
    (6780, 6800, "ill_give_stamps"),  # "I'll give you the stamps."
    (6823, 6836, "dont_change_subject"),  # "No, don't change the subject..."
    (
        6836,
        6856,
        "may_i_have_stamps",
    ),  # "Well, before we start that, may I have the stamps?"
]

cap = cv2.VideoCapture(VIDEO_PATH)
fps = cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"🎬 Video: {fps} fps, {total_frames} frames, {total_frames / fps:.0f}s")

total_extracted = 0
for start, end, label in KEY_SCENES:
    scene_dir = os.path.join(OUTPUT_DIR, label)
    os.makedirs(scene_dir, exist_ok=True)
    print(f"\n🔍 Extracting {label} ({start}s - {end}s)...")

    for sec in range(start, end + 1):
        cap.set(cv2.CAP_PROP_POS_MSEC, sec * 1000)
        ret, frame = cap.read()
        if ret:
            frame_path = os.path.join(scene_dir, f"frame_{sec}s.jpg")
            cv2.imwrite(frame_path, frame, [cv2.IMWRITE_JPEG_QUALITY, 95])
            total_extracted += 1
        else:
            print(f"  ⚠️  Failed to read frame at {sec}s")

cap.release()
print(f"\n✅ Extracted {total_extracted} frames from {len(KEY_SCENES)} key scenes")
print(f"📁 Saved to: {OUTPUT_DIR}/")
