from pathlib import Path
import subprocess,time,sys
from PIL import Image,ImageChops
root=Path(sys.argv[1]);side=sys.argv[2];r=Path((root/(side+'-recorded-latest')).read_text())
subprocess.run(['ffmpeg','-y','-i',str(r/'recording.mp4'),'-vf','crop=1600:886:0:0','-c:v','libx264','-preset','fast','-crf','18',str(r/'capture.mp4')],capture_output=True,check=True)
for n in ['still-a.png','capture.png']:
 subprocess.run(['ffmpeg','-y','-f','lavfi','-i','color=c=0x101820:s=1600x900:r=2','-vf',f'drawtext=fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf:textfile={r}/screen.txt:expansion=none:fontsize=17:fontcolor=white:x=28:y=28:line_spacing=7,crop=1600:886:0:0','-frames:v','1',str(r/n)],capture_output=True,check=True)
 if n=='still-a.png':time.sleep(.5)
assert ImageChops.difference(Image.open(r/'still-a.png'),Image.open(r/'capture.png')).getbbox() is None
print(side,'stable screenshots 500ms apart identical')
subprocess.run(['ffmpeg','-y','-i',str(r/'capture.mp4'),'-vf','fps=1/30,scale=800:443,tile=2x3','-frames:v','1',str(root/(side+'-contact.png'))],capture_output=True,check=True)
