"""Retain real navigation; pause on captured thread views and redact paths/auth transitions."""
import subprocess,sys
from pathlib import Path
from PIL import Image,ImageDraw
root,out=map(Path,sys.argv[1:3])
for side,start,end in [('base',21.12,25.16),('head',19.56,24.56)]:
 video=next((root/side).rglob('video.webm'))
 decode=subprocess.Popen(['ffmpeg','-v','error','-ss',str(start),'-i',str(video),'-t',str(end-start),'-vf','fps=25','-f','rawvideo','-pix_fmt','rgb24','-'],stdout=subprocess.PIPE)
 encode=subprocess.Popen(['ffmpeg','-y','-v','error','-f','rawvideo','-pix_fmt','rgb24','-s','1280x800','-r','25','-i','-','-an','-c:v','libx264','-crf','20','-pix_fmt','yuv420p','-movflags','+faststart',str(out/f'{side}-navigation.mp4')],stdin=subprocess.PIPE)
 for _ in range(75):encode.stdin.write(Image.open(out/f'{side}-parent.png').convert('RGB').tobytes())
 while data:=decode.stdout.read(1280*800*3):
  if len(data)!=1280*800*3:break
  im=Image.frombytes('RGB',(1280,800),data)
  r,g,b=im.getpixel((50,20))
  if b<r+30 or b<g:
   im=Image.new('RGB',(1280,800),'#fafafa');ImageDraw.Draw(im).text((520,390),'Navigating to child thread (authentication redacted)',fill='#555')
  else:
   d=ImageDraw.Draw(im)
   for box in [(675,109,934,131),(374,199,932,221)]:
    d.rectangle(box,fill='#e6e6e6');d.text((box[0]+5,box[1]+5),'[local path redacted]',fill='#555')
  encode.stdin.write(im.tobytes())
 for _ in range(75):encode.stdin.write(Image.open(out/f'{side}-child.png').convert('RGB').tobytes())
 decode.wait();encode.stdin.close();assert encode.wait()==0
