import pathlib,json,subprocess,textwrap
root=pathlib.Path(__file__).parent
runs={'before':root/'before-20261111T070829Z','after':root/'after-20261111T070816Z'}
for side,run in runs.items():
    events=json.loads((run/'events.json').read_text())
    frames=[]
    for i,event in enumerate(events):
        lines=[f'{side.upper()} | Captured real CLI output (timed transcript replay)', 'Linux x86_64 | fresh synthetic service state | same port and driver','']
        for e in events[:i+1]:
            lines.extend(textwrap.wrap(f'[{e["seconds"]:05.2f}s] {e["text"]}',width=94,subsequent_indent='          '))
        frame=run/f'frame-{i:02}.png'
        subprocess.run(['convert','-size','1600x850','xc:#111827','-font','/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf','-pointsize','23','-fill','#e5e7eb','-gravity','NorthWest','-annotate','+32+32','\n'.join(lines),str(frame)],check=True)
        duration=max(.3,events[i+1]['seconds']-event['seconds']) if i+1<len(events) else 4
        frames.extend([f"file '{frame}'",f'duration {duration}'])
    frames.append(f"file '{frame}'")
    (run/'frames.txt').write_text('\n'.join(frames))
    subprocess.run(['ffmpeg','-y','-loglevel','error','-f','concat','-safe','0','-i',str(run/'frames.txt'),'-vf','fps=15','-c:v','libx264','-pix_fmt','yuv420p',str(root/f'{side}-cli.mp4')],check=True)
    subprocess.run(['cp',str(frame),str(root/f'{side}-cli.png')],check=True)
    subprocess.run(['cp',str(run/'transcript.txt'),str(root/f'{side}-transcript.txt')],check=True)
