import pathlib,json,subprocess,sys
out=pathlib.Path(sys.argv[1]);events=[json.loads(x) for x in (out/'recording.cast').read_text().splitlines()[1:]]
# Render the captured terminal output with its original timing, without editing observations.
lines=[];concat=[]
for i,event in enumerate(events):
 t,_,s=event;lines.append(f'{t:6.2f}s  {s.strip()}')
 text=out/f'frame-{i:03}.txt';text.write_text('\n'.join(lines[-30:]))
 png=out/f'frame-{i:03}.png'
 subprocess.run(['convert','-size','1200x720','xc:#10151f','-font','/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf','-pointsize','17','-fill','#e2e8f0','-interline-spacing','4','-annotate','+22+30',text.read_text(),str(png)],check=True)
 duration=max(.04,(events[i+1][0]-t) if i+1<len(events) else 6)
 concat.extend([f"file '{png}'",f'duration {duration:.3f}'])
concat.append(f"file '{png}'")
(out/'frames.txt').write_text('\n'.join(concat))
subprocess.run(['ffmpeg','-hide_banner','-loglevel','error','-y','-f','concat','-safe','0','-i',str(out/'frames.txt'),'-vf','fps=25','-c:v','libx264','-pix_fmt','yuv420p','-movflags','+faststart',str(out/'recording.mp4')],check=True)
print(out/'recording.mp4')
