#!/usr/bin/env python3
"""Crop private background chrome and add labels for measured driver command windows.

Android omits unchanged frames. fps/tpad retain the last app image during those
intervals; labels are annotations, not app UI or synthetic touch feedback.
"""
import argparse,json,subprocess
p=argparse.ArgumentParser();p.add_argument('raw');p.add_argument('timing');p.add_argument('output');p.add_argument('--side',required=True);p.add_argument('--commit',required=True);a=p.parse_args()
t=json.load(open(a.timing));duration=t['duration']+3
f=['crop=1080:2204:0:220','scale=540:1102','fps=15','tpad=stop_mode=clone:stop_duration=60',f'trim=duration={duration}', 'pad=540:1182:0:80:color=black',f"drawtext=text='{a.side} {a.commit} | Pixel 9 Android 36':x=12:y=8:fontsize=21:fontcolor=white", "drawtext=text='Driver input (annotation)':x=12:y=34:fontsize=16:fontcolor=white"]
for e in t['events']:
 f.append(f"drawtext=text='{e['label']}':x=12:y=55:fontsize=18:fontcolor=yellow:enable='between(t,{e['start']},{e['end']})'")
f.append(f"drawtext=text='All five commands completed':x=12:y=55:fontsize=18:fontcolor=yellow:enable='gte(t,{t['duration']})'")
subprocess.run(['ffmpeg','-v','error','-i',a.raw,'-vf',','.join(f),'-c:v','libx264','-preset','fast','-crf','22','-an',a.output],check=True)
