Cotton code
99 lines · 4.4 KB · PythonRaw
#!/usr/bin/env python3"""Scan every frame of a recorded fan swipe (fan2 part 2, owner's build 280:the page's title dropped, a band showed and the ground flashed while the fanturned, snapping back at rest).    scripts/home-preview/check-swipe.py <recording.mp4> [--page TOP BOTTOM] [--fan TOP BOTTOM]Two rules, over the page area (default 10%..55% of the screen height) and thefan (default 60%..82%):  1. No frame shows the page area as ground alone: a page or its picture is     always there (a near-uniform page area is the ground showing through).  2. While the fan stands still, the page does not change: the swap from the     live page to its own picture at touch-down, and back at rest, is     invisible. A frame whose fan region did not move but whose page region     changed by more than the threshold in that one frame, with the frames     either side quiet, is a jump; a crossfade spreads its change over many.With --fan-still (lane trail: a record opening or going back on a fan card'spage — the zoom, the cross-fade — never moves the fan), a third rule over arecording in which nobody touches the fan (scripts/home-preview: -page-walk):  3. The fan does not change in any frame.Needs ffmpeg. Exits 1 when a rule is broken, printing the frames."""import argparseimport subprocessimport sysW, H = 120, 260def frames(path):    raw = subprocess.run(        ["ffmpeg", "-loglevel", "error", "-i", path, "-vf", f"fps=30,scale={W}:{H},format=gray",         "-f", "rawvideo", "-"], check=True, capture_output=True).stdout    size = W * H    return [raw[i:i + size] for i in range(0, len(raw) - size + 1, size)]def band(frame, top, bottom):    return frame[int(top * H) * W:int(bottom * H) * W]def mean_diff(a, b):    return sum(abs(x - y) for x, y in zip(a, b)) / len(a)def spread(region):    mean = sum(region) / len(region)    return (sum((x - mean) ** 2 for x in region) / len(region)) ** 0.5def main():    parser = argparse.ArgumentParser()    parser.add_argument("video")    parser.add_argument("--page", nargs=2, type=float, default=[0.10, 0.55])    parser.add_argument("--fan", nargs=2, type=float, default=[0.60, 0.82])    parser.add_argument("--still", type=float, default=0.3, help="fan mean change under which it stands still")    parser.add_argument("--jump", type=float, default=4.0,                        help="page mean change in ONE frame that is a jump (a fade spreads its change over many)")    parser.add_argument("--ground", type=float, default=6.0, help="page spread under which it is ground alone")    parser.add_argument("--fan-still", action="store_true",                        help="the fan is never touched in the recording: it must not change in any frame")    args = parser.parse_args()    # A simulator recording repeats frames while it renders slowly; a repeat    # says nothing, and leaving it in makes each step of a fade look alone.    shots = []    for frame in frames(args.video):        if not shots or mean_diff(shots[-1], frame) > 0.05:            shots.append(frame)    bare, jumps = [], []    page_moved, fan_moved = [0.0], [0.0]    for n, frame in enumerate(shots):        page = band(frame, *args.page)        if spread(page) < args.ground:            bare.append(n + 1)        if n:            fan_moved.append(mean_diff(band(shots[n - 1], *args.fan), band(frame, *args.fan)))            page_moved.append(mean_diff(band(shots[n - 1], *args.page), page))    quiet = args.jump / 4    for n in range(1, len(shots) - 1):        # A jump is the page changing in one frame, alone, with the fan still:        # a crossfade changes it a little in each of many frames.        if (fan_moved[n] < args.still and page_moved[n] > args.jump                and page_moved[n - 1] < quiet and page_moved[n + 1] < quiet):            jumps.append((n + 1, round(page_moved[n], 2)))    # The fan moving while the page turns in place: any change past `still`.    fan_moves = [(n + 1, round(moved, 2)) for n, moved in enumerate(fan_moved) if moved >= args.still] \        if args.fan_still else []    print(f"check-swipe: {len(shots)} distinct frames; ground alone {len(bare)} {bare[:20]}; "          f"page jumped with the fan still {len(jumps)} {jumps[:20]}"          + (f"; fan moved while the page turned {len(fan_moves)} {fan_moves[:20]}" if args.fan_still else ""))    sys.exit(1 if bare or jumps or fan_moves else 0)if __name__ == "__main__":    main()