import subprocess, sys
from PIL import Image, ImageDraw
source, target = sys.argv[1:]
w,h,fps=1280,800,25
reader=subprocess.Popen(['ffmpeg','-v','error','-i',source,'-f','rawvideo','-pix_fmt','rgb24','-'],stdout=subprocess.PIPE)
writer=subprocess.Popen(['ffmpeg','-v','error','-f','rawvideo','-pix_fmt','rgb24','-s',f'{w}x{h}','-r',str(fps),'-i','-','-an','-c:v','libx264','-crf','20','-pix_fmt','yuv420p','-movflags','+faststart',target],stdin=subprocess.PIPE)
blank=Image.new('RGB',(w,h),'#f5f5f5')
ImageDraw.Draw(blank).text((450,390),'Pairing / navigation omitted to protect credentials',fill='#333333')
blank=blank.tobytes()
frames=hidden=0
while True:
    raw=reader.stdout.read(w*h*3)
    if not raw: break
    if len(raw)!=w*h*3: raise RuntimeError('partial frame')
    # The real app has its blue T3 sidebar header; pairing and loading pages do not.
    blue=0
    for y in range(8,40,4):
        for x in range(40,190,10):
            r,g,b=raw[(y*w+x)*3:(y*w+x)*3+3]
            blue += b-r>45 and b-g>5
    keep=blue>45
    writer.stdin.write(raw if keep else blank)
    hidden+=not keep
    frames+=1
writer.stdin.close()
if reader.wait()!=0 or writer.wait()!=0: raise RuntimeError('ffmpeg failed')
print(f'{frames} frames processed; {hidden} pairing/navigation frames redacted')
