File size: 1,825 Bytes
99e41a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b504c6
f4fdc38
caa90b0
99e41a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caa90b0
99e41a8
caa90b0
 
99e41a8
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
"""README GIFs from the MP4 masters, sized for GitHub's ~960 px content column.

GitHub cannot play committed MP4s inline in a README, so each video gets a crisp GIF
(flat-colour palette, rectangle diff mode keeps files small) and the README links the MP4.
  robot_rollouts.gif     highlight: episode 1 (rollout + honest verdict + liar-majority verdict)
  swarm_rendezvous.gif   full clip
  planbench_modulo.gif   full clip (shown in a half-width table cell)
"""
import subprocess
from pathlib import Path

V = Path("media/videos")
SPEC = {  # name: (start s, duration s or None, width px, fps)
    "robot_rollouts": (4.0, 12.3, 960, 10),
    "swarm_rendezvous": (0.0, None, 960, 12),
    "planbench_modulo": (0.0, None, 720, 10),
    "maze_swarm": (4.0, None, 960, 10),
    "maze_false_walls": (4.0, None, 960, 10),
    "panda_rollouts": (4.0, 13.5, 960, 10),
}


def gif(name, start, dur, width, fps):
    src, out, pal = V / f"{name}.mp4", V / f"{name}.gif", V / f"{name}.pal.png"
    cut = ["-ss", str(start)] + (["-t", str(dur)] if dur else [])
    vf = f"fps={fps},scale={width}:-1:flags=lanczos"
    subprocess.run(["ffmpeg", "-y", "-loglevel", "error", *cut, "-i", str(src), "-vf",
                    vf + ",palettegen=max_colors=160:stats_mode=diff", str(pal)], check=True)
    subprocess.run(["ffmpeg", "-y", "-loglevel", "error", *cut, "-i", str(src), "-i", str(pal), "-lavfi",
                    vf + "[x];[x][1:v]paletteuse=dither=bayer:bayer_scale=4:diff_mode=rectangle", str(out)], check=True)
    pal.unlink()
    return out


if __name__ == "__main__":
    import sys
    for n, s in SPEC.items():
        if (sys.argv[1:] and n not in sys.argv[1:]) or not (V / f"{n}.mp4").exists():
            continue
        o = gif(n, *s)
        print(f"{o}  {o.stat().st_size / 1e6:.2f} MB")