File size: 3,489 Bytes
a7841c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #!/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('<Left>', lambda _: step(-1))
root.bind('<Right>', lambda _: step(1))
root.bind('<space>', 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
|