from pathlib import Path
import subprocess,sys
root=Path(sys.argv[1]); out=Path(sys.argv[2])
for side in ['base','head']:
    video=next((root/side).glob('**/video.webm'))
    pixels=subprocess.check_output(['ffmpeg','-v','error','-i',str(video),'-vf','crop=2:2:330:12,scale=1:1','-pix_fmt','rgb24','-f','rawvideo','-'])
    frame=next(i//3 for i in range(0,len(pixels),3) if pixels[i]<45 and 30<pixels[i+1]<70 and 50<pixels[i+2]<110)
    start=frame/25
    filters=[]
    if side=='base':
        filters=["drawbox=x=258:y=524:w=872:h=20:color=0xf8fafc:t=fill:enable='lt(t,6)'", "drawtext=text='[shell prompt redacted]':x=264:y=527:fontsize=12:fontcolor=0x536170:enable='lt(t,6)'"]
    args=['ffmpeg','-v','error','-ss',str(start),'-i',str(video)]
    if filters: args+=['-vf',','.join(filters)]
    dest=out/('before.mp4' if side=='base' else 'after.mp4')
    subprocess.run([*args,'-an','-c:v','libx264','-crf','18','-preset','fast','-movflags','+faststart',str(dest)],check=True)
    subprocess.run(['ffmpeg','-v','error','-i',str(dest),'-vf','fps=3,scale=640:400,tile=4x12','-frames:v','1',str(out/f'{side}-recording-review.jpg')],check=True)
    print(side,start,dest.name)
