import argparse
import json
from pathlib import Path
import subprocess

parser = argparse.ArgumentParser()
parser.add_argument('proof')
parser.add_argument('output')
args = parser.parse_args()
proof = json.loads(Path(args.proof).read_text())
out = Path(args.output)
out.mkdir(parents=True, exist_ok=True)
# Remove sign-in and cleanup; retain the recorded menu interactions at their original speed.
for side, start, end in [('base', 10, 26.6), ('head', 11, 20.2)]:
    label = 'before' if side == 'base' else 'after'
    video = out / f'{label}-{proof[side]["sha"][:10]}.mp4'
    subprocess.run(['ffmpeg', '-v', 'error', '-y', '-ss', str(start), '-i', proof[side]['videos'][0],
                    '-t', str(end-start), '-an', '-c:v', 'libx264', '-crf', '18',
                    '-pix_fmt', 'yuv420p', '-movflags', '+faststart', str(video)], check=True)
    # Review every half second of the trimmed recording, with the composer at readable size.
    subprocess.run(['ffmpeg', '-v', 'error', '-y', '-i', str(video),
                    '-vf', 'fps=2,crop=680:320:290:480,tile=3x4',
                    str(out / f'{label}-review-%02d.png')], check=True)
