summaryrefslogtreecommitdiff
path: root/design/motion/motion.py
blob: 87823ffed52e7d310457059303c33e033cd8080f (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#!/usr/bin/env python3
"""Instruments for looking at motion without watching it. See ../motion.md, "The instruments".

    motion.py chart  VIDEO [--regions NAME] [--out DIR]   timing chart, events, holds, pops
    motion.py sheet  VIDEO FROM TO [--step N] [--cols N] [--crop X,Y,W,H] [--width PX] [--diff] [--out FILE]
    motion.py auto   VIDEO [--regions NAME] [--out DIR]   a sheet for every event the chart finds
    motion.py track  VIDEO FROM TO --color RRGGBB [--crop X,Y,W,H] [--tol N]   where a colour is, frame by frame
    motion.py onion  VIDEO FROM TO [--crop X,Y,W,H] [--out FILE]   every frame of a move in one picture

FROM and TO are seconds (12.4) or frames (f372). Crops and regions are in
design units (1920 by 1080) whatever the video's size. Needs ffmpeg, numpy, Pillow.
"""
import argparse
import json
import os
import subprocess
import sys

import numpy as np
from PIL import Image, ImageDraw, ImageFont

W, H = 1920, 1080
AW, AH = 960, 540                       # analysis size: half, enough to see one hex digit change
THRESH = 24                             # a pixel moved if any channel changed by more than this (of 255)
SOFT = 6                                # ... and is fading if by more than this: a colour cooling, a caption arriving
MIN_PX = 12                             # a region is moving in a frame if this many of its pixels moved
MIN_SOFT = 150                          # ... and fading if this many did: under it is the codec
HOLD = 0.4                              # seconds of nothing that count as a hold
STILL = 12                              # frames still before a one-frame change counts as a pop


def cols(first, n, y, h):
    return (64 + first * 152, 120 + y, n * 152 - 32, h)


HEAD, CAPTION = (0, 0, W, 112), (0, 896, W, 184)
# What to measure, by scene (the video's file name): the bands every scene has, and its panels.
REGIONS = {
    "bands": {"head": HEAD, "stage": (0, 112, W, 784), "caption": CAPTION},
    "fib": {"head": HEAD, "source": cols(0, 5, 0, 768), "tree": cols(5, 7, 0, 336), "stack": cols(5, 3, 360, 408),
            "registers": cols(8, 4, 360, 408), "caption": CAPTION},
    "list": {"head": HEAD, "source": cols(0, 5, 0, 504), "heap map": cols(0, 5, 528, 240), "nodes": cols(5, 7, 0, 240),
             "words": cols(5, 7, 264, 504), "caption": CAPTION},
    "sort": {"head": HEAD, "array": cols(0, 12, 0, 408), "source": cols(0, 7, 432, 336), "locals": cols(7, 5, 432, 336),
             "caption": CAPTION},
    "syscalls": {"head": HEAD, "source": cols(0, 4, 0, 408), "registers": cols(4, 4, 0, 408), "memory": cols(8, 4, 0, 408),
                 "boundary": (0, 528, W, 96), "kernel": (0, 624, W, 272), "caption": CAPTION},
    "zoom": {"head": HEAD, "source": cols(0, 5, 0, 384), "registers": cols(0, 5, 408, 360), "stack": cols(5, 7, 0, 768),
             "caption": CAPTION},
    # The working frame (README.md, "The frame"): the two columns, each less the strips of its panes' minimaps at
    # its right (secondary action, their own regions), the caption's text alone (not the clock's count at its
    # right, which changes every step), the clock. The default for any other name.
    "frame": {"program": (64, 48, 838, 840), "program strips": (902, 48, 40, 840), "state": (976, 48, 838, 840),
              "state strips": (1814, 48, 40, 840), "caption": (64, 906, 1400, 126), "clock": (64, 894, 1792, 52)},
}


# Each pane's minimap (README.md, "The map and the minimap"): a strip 40 wide inside its frame at its right,
# under the tag; the source, the disassembly, the stack and a dump (the band's view) have one. A pane's region
# stops at its strip, which is a region of its own, secondary action.
def strips(panes):
    out = {}
    for name, (x, y, w, h) in panes.items():
        if name in ("source", "disasm", "stack", "view"):
            out[name] = (x, y, w - 42, h)
            out[name + " strip"] = (x + w - 42, y + 24, 40, h - 26)
        else:
            out[name] = (x, y, w, h)
    return out


def split(drawing, view, source, disasm, slots, backtrace=0):
    """A split's panes (theme.layout.split): a panel of n rows is 32 + 24 n, 12 apart."""
    ph = lambda n: 32 + 24 * n
    out = {"drawing": (64, 48, 880, drawing), "view": (976, 48, 880, view)} if drawing else {}
    left, right = 48 + (drawing + 12 if drawing else 0), 48 + (view + 12 if view else 0)
    out.update({"source": (64, left, 880, ph(source)), "disasm": (64, left + ph(source) + 12, 880, ph(disasm)),
                "registers": (976, right, 880, ph(17)), "stack": (976, right + ph(17) + 12, 880, ph(slots))})
    if backtrace:
        out["backtrace"] = (976, right + ph(17) + 12 + ph(slots) + 12, 880, ph(backtrace))
    return out


# The working frame by split, its strips apart (`--regions none|beside|subject|blocks`): `blocks` is the
# graph over the program column, the source alone (twenty lines), the disassembly its view, seven rows.
_words = {"caption": (64, 906, 1400, 126), "clock": (64, 894, 1792, 52)}
_blocks = {**split(296, 200, 10, 9, 6), "view": (976, 48, 880, 32 + 24 * 7), "source": (64, 356, 880, 32 + 24 * 20)}
del _blocks["disasm"]
for _name, _panes in {"none": split(0, 0, 14, 17, 8, 4), "beside": split(152, 152, 8, 17, 8),
                      "subject": split(296, 200, 10, 9, 6), "blocks": _blocks}.items():
    REGIONS[_name] = {**strips(_panes), **_words}
PALETTE = ["#9aa6d9", "#e8c46a", "#6fd6e6", "#7fdc9a", "#d59cf5", "#ff6a4a", "#eee6d2", "#17a0b0"]


def probe(video):
    out = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries",
                          "stream=r_frame_rate,nb_frames", "-of", "json", video], capture_output=True, text=True, check=True)
    s = json.loads(out.stdout)["streams"][0]
    num, den = s["r_frame_rate"].split("/")
    return int(num) / int(den), int(s["nb_frames"])


def frames(video, w, h, first=0, count=None, step=1):
    """Yield (index, HxWx3 uint8) for frames first, first+step, ..."""
    vf = f"select='gte(n\\,{first})*not(mod(n-{first}\\,{step}))',scale={w}:{h}:flags=area"
    cmd = ["ffmpeg", "-v", "error", "-i", video, "-vf", vf, "-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", "rgb24"]
    if count is not None:
        cmd += ["-frames:v", str(count)]
    p = subprocess.Popen(cmd + ["-"], stdout=subprocess.PIPE)
    i = first
    while True:
        buf = p.stdout.read(w * h * 3)
        if len(buf) < w * h * 3:
            break
        yield i, np.frombuffer(buf, np.uint8).reshape(h, w, 3)
        i += step
    p.stdout.close()
    p.wait()


def when(text, fps):
    """'12.4' seconds or 'f372' frames, as a frame index."""
    return int(text[1:]) if text.startswith("f") else int(round(float(text) * fps))


def font(size):
    for path in ("/usr/share/fonts/go/Go-Mono.ttf", "/usr/share/fonts/TTF/DejaVuSansMono.ttf"):
        if os.path.exists(path):
            return ImageFont.truetype(path, size)
    return ImageFont.load_default()


def regions_for(video, name):
    stem = os.path.splitext(os.path.basename(video))[0]
    return REGIONS.get(name or stem, REGIONS["frame"])


# ---- measure --------------------------------------------------------------
def measure(video, regions):
    """Per frame and region, against the frame before (at AW by AH): how many pixels moved, and
    how many changed at all (moved or fading). Returns names, moved, soft, region sizes."""
    names = ["all"] + list(regions)
    boxes = [(0, 0, AW, AH)] + [tuple(int(round(v * AW / W)) for v in r) for r in regions.values()]
    rows, soft, prev = [], [], None
    for _, f in frames(video, AW, AH):
        f = f.astype(np.int16)
        if prev is None:
            rows.append([0] * len(names))
            soft.append([0] * len(names))
        else:
            d = np.abs(f - prev).max(axis=2)
            rows.append([int((d[y:y + h, x:x + w] > THRESH).sum()) for x, y, w, h in boxes])
            soft.append([int((d[y:y + h, x:x + w] > SOFT).sum()) for x, y, w, h in boxes])
        prev = f
    return names, np.array(rows), np.array(soft), [w * h for _, _, w, h in boxes]


def busy(counts, soft):
    """Per frame and region: anything changing, by moving or by fading."""
    return (counts >= MIN_PX) | (soft >= MIN_SOFT)


def runs(active):
    """[(first, last)] of consecutive true frames."""
    out, start = [], None
    for i, a in enumerate(active):
        if a and start is None:
            start = i
        if not a and start is not None:
            out.append((start, i - 1))
            start = None
    if start is not None:
        out.append((start, len(active) - 1))
    return out


def events(names, counts, soft, fps, gap=2):
    """Stretches of change, joined across gaps of up to `gap` still frames."""
    spans = []
    on_ = busy(counts, soft)
    for a, b in runs(on_[:, 0]):
        if spans and a - spans[-1][1] - 1 <= gap:
            spans[-1] = (spans[-1][0], b)
        else:
            spans.append((a, b))
    out = []
    for a, b in spans:
        parts = {}
        for k, name in enumerate(names[1:], 1):
            on = np.nonzero(on_[a:b + 1, k])[0]
            if len(on):
                hard = np.nonzero(counts[a:b + 1, k] >= MIN_PX)[0]
                # (first, last, peak, frames that moved): a region that only fades moved in none
                parts[name] = (a + int(on[0]), a + int(on[-1]), int(counts[a:b + 1, k].max()), len(hard))
        hard = int((counts[a:b + 1, 0] >= MIN_PX).sum())
        out.append({"from": a, "to": b, "frames": b - a + 1, "seconds": round((b - a + 1) / fps, 3),
                    "peak": int(counts[a:b + 1, 0].max()), "moved": hard, "regions": parts})
    return out


def report(video, names, counts, soft, sizes, fps):
    ev = events(names, counts, soft, fps)
    n = len(counts)
    lines = [f"{os.path.basename(video)}: {n} frames at {fps:g} fps, {n / fps:.2f} s", ""]
    moving = sum(e["frames"] for e in ev)
    lines.append(f"moving {moving / n:.0%} of the time, still {1 - moving / n:.0%}; {len(ev)} events")
    lines.append("")
    lines.append("  from      to   frames  peak%  what changed (first frame..last, from the event's first; ~ fades only)")
    last = -1
    for e in ev:
        still = e["from"] - last - 1
        if still / fps >= HOLD:
            lines.append(f"          hold {still / fps:5.2f} s")
        parts = "  ".join(f"{'~' if n_ == 0 else ''}{k} {a - e['from']}..{b - e['from']}" for k, (a, b, _, n_) in e["regions"].items())
        # A pop: a lot changes in a frame or two with nothing either side of it.
        pop = "  POP" if e["moved"] <= 2 and e["frames"] <= 3 and e["peak"] / sizes[0] > 0.002 else ""
        many = len([k for k in e["regions"] if k not in ("head", "caption")])
        lines.append(f"{e['from'] / fps:6.2f}  {e['to'] / fps:6.2f}  {e['frames']:6d}  {100 * e['peak'] / sizes[0]:5.2f}  {parts}"
                     + (f"  [{many} panels]" if many > 2 else "") + pop)
        last = e["to"]
    if (n - last - 1) / fps >= HOLD:
        lines.append(f"          hold {(n - last - 1) / fps:5.2f} s")
    lines += ["", "checks (each should come out empty)"] + lint(names, counts, soft, sizes, fps, ev)
    return ev, "\n".join(lines)


# Regions that are not panels: the words, and the panes' minimaps (secondary action).
WORDS = ("head", "caption", "clock", "minimap", "program strips", "state strips",
         "source strip", "disasm strip", "stack strip", "view strip")

# One-frame changes that are right, by name: each a test of (region, frame, pixels moved there) that says
# whether the POP at that frame is one of its kind. They are counted apart, never silently dropped.
# `keyframe`: the encoder's, not the scene's: a few pixels of a still region re-quantized at an I frame.
KEYFRAMES = set()                       # the video's I frames (cmd_chart reads them)
EXEMPT = {"keyframe": lambda name, t, moved: t in KEYFRAMES and moved < 200}


def keyframes(video):
    out = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0", "-skip_frame", "nokey", "-show_entries",
                          "frame=pts", "-of", "csv=p=0", video], capture_output=True, text=True, check=True)
    fps, _ = probe(video)
    tb = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=time_base",
                         "-of", "csv=p=0", video], capture_output=True, text=True, check=True).stdout.strip().split("/")
    per = fps * int(tb[0]) / int(tb[1])
    return {int(round(int(line) * per)) for line in out.stdout.split() if line.strip().lstrip("-").isdigit()}


def lint(names, counts, soft, sizes, fps, ev):
    """What a viewer would trip on, found by rule. Each line: what, how often, the first few times."""
    def some(times):
        return ", ".join(f"{t:.2f}" for t in times[:4]) + (" ..." if len(times) > 4 else "")
    out = []
    on = busy(counts, soft)
    panels = [k for k, n_ in enumerate(names) if k > 0 and n_ not in WORDS]

    # POP: a region still for STILL frames changes in one frame and is still again, at any size: a label
    # swapped, a box gone, a count appearing. Nothing eased it. (And, as before, a large change in two.)
    pops, exempt = set(), {}
    for e in ev:
        if e["moved"] <= 2 and e["frames"] <= 3 and e["peak"] / sizes[0] > 0.002:
            kind = next((n_ for n_, test in EXEMPT.items() if test("all", e["from"], e["peak"])), None)
            exempt.setdefault(kind, set()).add(e["from"]) if kind else pops.add(e["from"])
    for k in range(1, len(names)):
        if names[k] == "clock":          # its count changes in a frame by design: it is time, not a thing
            continue
        col = on[:, k]
        for t in range(STILL, len(col) - 1):
            if col[t] and not col[t + 1] and not col[t - STILL:t].any() and counts[t, k] >= MIN_PX:
                kind = next((n_ for n_, test in EXEMPT.items() if test(names[k], t, counts[t, k])), None)
                if kind:
                    exempt.setdefault(kind, set()).add(t)
                else:
                    pops.add(t)
    pops = sorted(pops)
    if pops:
        out.append(f"  POP        {len(pops):3d}  a change in one frame after stillness: {some([t / fps for t in pops])}")
    for kind, ts in sorted(exempt.items()):
        ts = sorted(ts - set(pops))
        if ts:
            out.append(f"  POP exempt {len(ts):3d}  {kind}: {some([t / fps for t in ts])}")

    # CROWD: three or more panels begin to move within two frames of each other.
    crowd = []
    for e in ev:
        first = [a - e["from"] for k, (a, b, p, n_) in e["regions"].items() if k not in WORDS and n_ > 0]
        if sum(1 for f in first if f <= 1) >= 3:
            crowd.append(e["from"] / fps)
    if crowd:
        out.append(f"  CROWD      {len(crowd):3d}  three or more panels start together: {some(crowd)}")

    # METRONOME: six or more events in a row, evenly spaced.
    starts = [e["from"] for e in ev if e["peak"] / sizes[0] >= 0.005]      # the beats, not their echoes
    i, runs_ = 0, []
    while i < len(starts) - 1:
        j = i + 1
        gap = starts[j] - starts[i]
        while j + 1 < len(starts) and abs((starts[j + 1] - starts[j]) - gap) <= 1:
            j += 1
        if j - i >= 6:
            runs_.append((starts[i] / fps, starts[j] / fps, j - i + 1, gap / fps))
        i = j
    for a, b, n_, gap in runs_:
        out.append(f"  METRONOME  {n_:3d}  events {gap:.2f} s apart, from {a:.2f} to {b:.2f}")

    # CAPTION: a caption up too long or too short to be read, or the picture moving before it can be.
    # Its time on screen is from the end of the change that brought it to the start of the next change.
    # Without the text, the shortest is 90 f; tools/chart.py, which has the scene's captions, adds 3.6 f a
    # character. The picture may move 60 f after a caption arrives, no sooner.
    cap = names.index("caption") if "caption" in names else None
    late, longs, shorts = [], [], []
    if cap is not None:
        changes = [(a, b) for a, b in runs(on[:, cap]) if b - a >= 3]
        for (a, b), nxt in zip(changes, changes[1:] + [(len(on), len(on))]):
            up = nxt[0] - b - 1
            if up > 720:
                longs.append(b / fps)
            elif 6 < up < 90:            # 6: a caption leaving and the next arriving
                shorts.append(b / fps)
            later = [i for i in np.nonzero(on[b + 1:, panels].any(axis=1))[0]
                     if not ({b + 1 + i, b + i} & KEYFRAMES and counts[b + 1 + i, panels].max() < 200)]   # the encoder's (an I frame and the one after it), not the picture's
            if len(later) and later[0] < 60 and up >= 90:
                late.append(b / fps)
    for what, times in (("up over 720 f (12 s)", longs), ("up under 90 f", shorts),
                        ("the picture moves within 60 f of its arrival", late)):
        if times:
            out.append(f"  CAPTION    {len(times):3d}  {what}: {some(times)}")

    # AIR: nothing at all changes for three seconds.
    air = [(a / fps, (b - a + 1) / fps) for a, b in runs(~on[:, 0]) if (b - a + 1) / fps >= 3.0]
    if air:
        out.append(f"  AIR        {len(air):3d}  three seconds or more with nothing changing: " + ", ".join(f"{t:.2f} ({d:.1f} s)" for t, d in air[:4]))
    return out or ["  none"]


def chart(video, names, counts, soft, sizes, fps, ev, path, per_row=10.0):
    """Time left to right, a strip a region; bar height is how much of the region changed (log)."""
    px = max(2, int(round(180 / fps)))                 # pixels a frame: 6 at 30 fps, 3 at 60
    row_frames = int(per_row * fps)
    n = len(counts)
    nrows = -(-n // row_frames)
    left, strip, pad = 130, 34, 34
    rh = len(names) * strip + pad
    img = Image.new("RGB", (left + row_frames * px + 20, nrows * rh + 30), "#0a1030")
    d = ImageDraw.Draw(img)
    small, tiny = font(15), font(12)
    still = ~busy(counts, soft)[:, 0]
    for r in range(nrows):
        y0 = 20 + r * rh
        a = r * row_frames
        # holds, shaded and timed
        for s, e in runs(still):
            if (e - s + 1) / fps < HOLD:
                continue
            s2, e2 = max(s, a), min(e, a + row_frames - 1)
            if s2 > e2:
                continue
            d.rectangle([left + (s2 - a) * px, y0, left + (e2 - a + 1) * px - 1, y0 + len(names) * strip - 4], fill="#141c48")
            if s >= a:
                d.text((left + (s - a) * px + 4, y0 + 2), f"{(e - s + 1) / fps:.2f}", font=tiny, fill="#6a7ab8")
        for k, name in enumerate(names):
            y = y0 + k * strip
            d.text((8, y + 8), name, font=small, fill="#eee6d2" if k == 0 else PALETTE[(k - 1) % len(PALETTE)])
            d.line([left, y + strip - 5, left + row_frames * px, y + strip - 5], fill="#3a4a8a")
            ink = "#eee6d2" if k == 0 else PALETTE[(k - 1) % len(PALETTE)]
            for i in range(a, min(a + row_frames, n)):
                x = left + (i - a) * px
                for c, ok, fill in ((soft[i, k], soft[i, k] >= MIN_SOFT, None), (counts[i, k], counts[i, k] >= MIN_PX, ink)):
                    if not ok:
                        continue
                    hgt = int(np.clip((np.log10(c / sizes[k]) + 4.3) / 3.6, 0.08, 1) * (strip - 8))   # 0.005% .. 20%
                    # A fade is an outline, a move is solid.
                    d.rectangle([x, y + strip - 5 - hgt, x + px - 2, y + strip - 5], fill=fill, outline=ink)
        for i in range(a, min(a + row_frames, n) + 1):
            if i % int(fps) == 0:
                x = left + (i - a) * px
                d.line([x, y0 + len(names) * strip - 4, x, y0 + len(names) * strip + 4], fill="#9aa6d9")
                d.text((x + 3, y0 + len(names) * strip), f"{i / fps:g}s", font=tiny, fill="#9aa6d9")
            elif i % int(fps / 2) == 0:
                x = left + (i - a) * px
                d.line([x, y0 + len(names) * strip - 4, x, y0 + len(names) * strip], fill="#3a4a8a")
    img.save(path)


def cmd_chart(a):
    fps, _ = probe(a.video)
    KEYFRAMES.clear()
    KEYFRAMES.update(keyframes(a.video))
    regions = regions_for(a.video, a.regions)
    names, counts, soft, sizes = measure(a.video, regions)
    ev, text = report(a.video, names, counts, soft, sizes, fps)
    os.makedirs(a.out, exist_ok=True)
    stem = os.path.splitext(os.path.basename(a.video))[0]
    chart(a.video, names, counts, soft, sizes, fps, ev, os.path.join(a.out, stem + ".png"))
    open(os.path.join(a.out, stem + ".txt"), "w").write(text + "\n")
    np.savetxt(os.path.join(a.out, stem + ".csv"), np.hstack([counts, soft]), fmt="%d", delimiter=",",
               header=",".join(names + [n + "~" for n in names]), comments="")
    print(text)
    return fps, ev


# ---- look -----------------------------------------------------------------
def crop_of(text):
    return tuple(int(v) for v in text.split(",")) if text else (0, 0, W, H)


def grab(video, first, last, step, crop, width):
    """Frames first..last as PIL images of `crop` (design units), `width` pixels wide."""
    x, y, w, h = crop
    fps, total = probe(video)
    last = min(last, total - 1)
    out = []
    for i, f in frames(video, W, H, first, (last - first) // step + 1, step):
        im = Image.fromarray(f).crop((x, y, x + w, y + h))
        out.append((i, im.resize((width, max(1, round(width * h / w))), Image.LANCZOS)))
    return fps, out


def tile(shots, fps, cols_, diff=False):
    w, h = shots[0][1].size
    rows = -(-len(shots) // cols_)
    lab = 20
    sheet = Image.new("RGB", (cols_ * (w + 4) + 4, rows * (h + lab + 4) + 4), "#444444")
    d = ImageDraw.Draw(sheet)
    f = font(14)
    prev = None
    for k, (i, im) in enumerate(shots):
        x, y = 4 + (k % cols_) * (w + 4), 4 + (k // cols_) * (h + lab + 4)
        show = im
        if diff and prev is not None:
            # What changed since the tile before, white on the frame dimmed.
            a, b = np.asarray(im, np.int16), np.asarray(prev, np.int16)
            moved = np.abs(a - b).max(axis=2) > THRESH
            dim = (np.asarray(im, np.float32) * 0.35).astype(np.uint8)
            dim[moved] = (255, 255, 255)
            show = Image.fromarray(dim)
        sheet.paste(show, (x, y + lab))
        d.text((x + 2, y + 1), f"f{i}  {i / fps:.3f}s", font=f, fill="#ffffff")
        prev = im
    return sheet


def cmd_sheet(a):
    fps, _ = probe(a.video)
    first, last = when(a.start, fps), when(a.end, fps)
    crop = crop_of(a.crop)
    n = (last - first) // a.step + 1
    cols_ = a.cols or min(n, 6)
    width = a.width or min(crop[2], 1900 // cols_ - 4)
    _, shots = grab(a.video, first, last, a.step, crop, width)
    out = a.out or f"sheet-{os.path.splitext(os.path.basename(a.video))[0]}-f{first}-f{last}.png"
    os.makedirs(os.path.dirname(out) or ".", exist_ok=True)
    tile(shots, fps, cols_, a.diff).save(out)
    print(out, f"{len(shots)} frames")


def cmd_auto(a):
    """A sheet for each event: every frame of it (every 2nd or 3rd if long), and a frame either side."""
    fps, ev = cmd_chart(a)
    stem = os.path.splitext(os.path.basename(a.video))[0]
    made = short = 0
    for e in ev:
        if e["frames"] <= 2:                    # a pop: three of them show what the rest look like
            short += 1
            if short > 3:
                continue
        first, last = max(e["from"] - 1, 0), e["to"] + 1
        step = max(1, -(-(last - first + 1) // 24))
        _, shots = grab(a.video, first, last, step, (0, 0, W, H), 472)
        path = os.path.join(a.out, f"{stem}-{e['from'] / fps:06.2f}s.png")
        tile(shots, fps, 4).save(path)
        made += 1
    print(f"{made} sheets in {a.out}")


def cmd_track(a):
    """The centre and area of everything `color` in the crop, a frame a line: an object's spacing."""
    fps, _ = probe(a.video)
    first, last = when(a.start, fps), when(a.end, fps)
    x, y, w, h = crop_of(a.crop)
    key = np.array([int(a.color[i:i + 2], 16) for i in (0, 2, 4)], np.int16)
    print(" frame   time      x      y   area    dx    dy   box: left top width height")
    px = py = None
    for i, f in frames(a.video, W, H, first, last - first + 1):
        m = (np.abs(f[y:y + h, x:x + w].astype(np.int16) - key).max(axis=2) <= a.tol)
        area = int(m.sum())
        if area < 4:
            print(f"{i:6d} {i / fps:6.3f}      -      -      0")
            px = py = None
            continue
        ys, xs = np.nonzero(m)
        cx, cy = x + xs.mean(), y + ys.mean()
        step = f"{cx - px:5.1f} {cy - py:5.1f}" if px is not None else "           "
        box = f"{x + xs.min():5d} {y + ys.min():5d} {xs.max() - xs.min() + 1:5d} {ys.max() - ys.min() + 1:5d}"
        print(f"{i:6d} {i / fps:6.3f} {cx:6.1f} {cy:6.1f} {area:6d} {step}   {box}")
        px, py = cx, cy


def cmd_onion(a):
    """Every frame of a move in one picture: the first frame, and over it each later frame where it differs."""
    fps, _ = probe(a.video)
    first, last = when(a.start, fps), when(a.end, fps)
    crop = crop_of(a.crop)
    _, shots = grab(a.video, first, last, a.step, crop, min(crop[2], 1900))
    base = np.asarray(shots[0][1], np.int16)
    out = base.copy().astype(np.float32) * 0.5
    for k, (_, im) in enumerate(shots[1:], 1):
        f = np.asarray(im, np.int16)
        moved = np.abs(f - base).max(axis=2) > THRESH
        out[moved] = np.maximum(out[moved], f[moved] * (0.45 + 0.55 * k / (len(shots) - 1)))
    path = a.out or f"onion-{os.path.splitext(os.path.basename(a.video))[0]}-f{first}-f{last}.png"
    os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
    Image.fromarray(out.clip(0, 255).astype(np.uint8)).save(path)
    print(path, f"{len(shots)} frames")


def main():
    p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    sub = p.add_subparsers(dest="cmd", required=True)
    for name, fn in (("chart", cmd_chart), ("auto", cmd_auto)):
        c = sub.add_parser(name)
        c.add_argument("video")
        c.add_argument("--regions")
        c.add_argument("--out", default="tmp")
        c.set_defaults(fn=fn)
    c = sub.add_parser("sheet")
    c.add_argument("video"), c.add_argument("start"), c.add_argument("end")
    c.add_argument("--step", type=int, default=1), c.add_argument("--cols", type=int), c.add_argument("--crop")
    c.add_argument("--width", type=int), c.add_argument("--diff", action="store_true"), c.add_argument("--out")
    c.set_defaults(fn=cmd_sheet)
    c = sub.add_parser("track")
    c.add_argument("video"), c.add_argument("start"), c.add_argument("end")
    c.add_argument("--color", required=True), c.add_argument("--crop"), c.add_argument("--tol", type=int, default=40)
    c.set_defaults(fn=cmd_track)
    c = sub.add_parser("onion")
    c.add_argument("video"), c.add_argument("start"), c.add_argument("end")
    c.add_argument("--step", type=int, default=1), c.add_argument("--crop"), c.add_argument("--out")
    c.set_defaults(fn=cmd_onion)
    a = p.parse_args()
    a.fn(a)


if __name__ == "__main__":
    sys.exit(main())