import pathlib,subprocess
root=pathlib.Path(__file__).parent
for side in ('base','head'):
    run=next((root/side).iterdir())
    frames=sorted(p for p in run.glob('*.png') if p.stem.isdigit())
    entries=[]
    for i,p in enumerate(frames):
        entries.append(f"file '{p.name}'\n")
        entries.append(f"duration {max(.04,(frames[i+1].stat().st_mtime-p.stat().st_mtime)) if i+1<len(frames) else .25}\n")
    entries.append(f"file '{frames[-1].name}'\n")
    (run/'frames.ffconcat').write_text(''.join(entries))
    subprocess.run(['ffmpeg','-loglevel','error','-y','-f','concat','-safe','0','-i',str(run/'frames.ffconcat'),'-fps_mode','vfr','-c:v','libx264','-pix_fmt','yuv420p','-crf','20',str(root/(side+'-interaction.mp4'))],check=True)
