import json,sys,pathlib,subprocess
root=pathlib.Path(sys.argv[1]); side=sys.argv[2]
a=root/f'{side}-proof-A-capture-2'; b=root/f'{side}-proof-B-capture'
# The two labeled segments are unaltered CDP frames of the real Electron renderer.
for label,directory in [('A',a),('B',b)]:
    frames=json.loads((directory/'frames.json').read_text())
    lines=[]
    for i,f in enumerate(frames):
        duration=(frames[i+1]['time']-f['time'])/1000 if i+1<len(frames) else 3
        lines += [f"file '{f['name']}'", f'duration {max(.04,duration):.3f}']
    lines += [f"file '{frames[-1]['name']}'"]
    (directory/'concat.txt').write_text('\n'.join(lines)+'\n')
    text=f'{side.upper()} - Home A / choose Dark' if label=='A' else f'{side.upper()} - fresh Home B / inspect without changing theme'
    subprocess.run(['ffmpeg','-hide_banner','-loglevel','error','-y','-f','concat','-safe','0','-i',str(directory/'concat.txt'),'-vf',f"pad=1100:840:0:60:color=0x222222,drawtext=fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf:text='{text}':fontcolor=white:fontsize=22:x=20:y=18",'-r','20','-c:v','libx264','-pix_fmt','yuv420p',str(root/f'{side}-{label}.mp4')],check=True)
(root/f'{side}-segments.txt').write_text(f"file '{side}-A.mp4'\nfile '{side}-B.mp4'\n")
subprocess.run(['ffmpeg','-hide_banner','-loglevel','error','-y','-f','concat','-safe','0','-i',str(root/f'{side}-segments.txt'),'-c','copy','-movflags','+faststart',str(root/f'{side}-comparison.mp4')],check=True)
