#!/usr/bin/env python3 """GUI reference player; records displayed frames only while focused.""" import hashlib import json from pathlib import Path import subprocess import time import tkinter as tk REFERENCE = Path('/root/media/reference.mp4') LOG = Path('/logs/artifacts/reference-viewing.jsonl') FPS = 30 SIZE = 600 def main(): raw = subprocess.run(['ffmpeg', '-nostdin', '-v', 'error', '-i', str(REFERENCE), '-vf', f'scale={SIZE}:{SIZE}', '-pix_fmt', 'rgb24', '-f', 'rawvideo', '-'], capture_output=True, check=True, timeout=30).stdout stride = SIZE * SIZE * 3 frames = [raw[i:i + stride] for i in range(0, len(raw), stride)] if len(frames) != 180 or any(len(f) != stride for f in frames): raise RuntimeError('Reference must decode to 180 complete frames') digest = hashlib.sha256(REFERENCE.read_bytes()).hexdigest() LOG.parent.mkdir(parents=True, exist_ok=True) session = f'{time.time_ns()}' with LOG.open('a') as out: out.write(json.dumps({'event': 'viewer_start', 'session': session, 'reference_sha256': digest, 'timestamp': time.time()}) + '\n') root = tk.Tk() root.title('Dance Blob — Reference Viewer') images = [tk.PhotoImage(data=b'P6\n600 600\n255\n' + f, format='PPM') for f in frames] picture = tk.Label(root) picture.pack() label = tk.Label(root) label.pack() controls = tk.Frame(root) controls.pack() index, playing, last_sample = 0, False, time.monotonic() def show(): picture.configure(image=images[index]) label.configure(text=f'Frame {index + 1}/180 {index / FPS:.2f}s — watch or step through the dance') def step(delta): nonlocal index, playing playing = False index = (index + delta) % len(images) show() def toggle(): nonlocal playing playing = not playing tk.Button(controls, text='Previous frame', command=lambda: step(-1)).pack(side='left') tk.Button(controls, text='Play / Pause', command=toggle).pack(side='left') tk.Button(controls, text='Next frame', command=lambda: step(1)).pack(side='left') root.bind('', lambda _: step(-1)) root.bind('', lambda _: step(1)) root.bind('', lambda _: toggle()) def tick(): nonlocal index, last_sample now = time.monotonic() wall_stamp = time.time() # No dwell credit while hidden, minimized or another application has focus. focused = root.state() == 'normal' and root.focus_displayof() is not None elapsed = min(.25, now - last_sample) last_sample = now if focused: with LOG.open('a') as out: out.write(json.dumps({'event': 'reference_frame', 'session': session, 'reference_sha256': digest, 'frame': index, 'visible_seconds': elapsed, 'timestamp': wall_stamp}) + '\n') if playing and focused: index = (index + 1) % len(images) show() root.after(33, tick) show() root.after(33, tick) root.mainloop() if __name__ == '__main__': try: main() except Exception as exc: LOG.parent.mkdir(parents=True, exist_ok=True) with LOG.open('a') as out: out.write(json.dumps({'event': 'viewer_error', 'error': str(exc), 'timestamp': time.time()}) + '\n') raise