# Zoom test for the CapCut zoom guide (2026-11-19).
# Test 1: how wide is a hard black/white edge after a centre zoom of z, from a 1080p source and from a 4K source, in a 1080p project.
#   Width = distance between the 10% and 90% brightness crossings on the middle row of the output (linear interpolation), in output pixels.
#   Control A: z=1 from a 1080p source must give a width below 1.0 px (a pixel-aligned step in, same grid out).
#   Control B: a deliberately blurred edge (gblur sigma=3) must measure wider than 4 px, so the measurement can see softness.
# Test 2: does a centre zoom keep an off-centre marker in the frame? Marker centre at 75% of the width; visible while z <= 2.0 (arithmetic: 0.25*W*z < 0.5*W).
# Test 3: scale change per frame for a 100% to 130% zoom over 0.5 s, 1 s, 2 s, 4 s at 30 fps (linear).
import subprocess, os, sys
W = os.path.expanduser("~/rt/zoom-in-in-capcut/work"); os.makedirs(W, exist_ok=True)
def run(a, **k): return subprocess.run(a, capture_output=True, **k)
def edge_src(w, h, path):
    run(["ffmpeg","-y","-v","error","-f","lavfi","-i",f"color=c=black:s={w}x{h}:d=1,format=gray","-vf",f"drawbox=x={w//2}:y=0:w={w//2}:h={h}:color=white:t=fill","-frames:v","1",path])
def row(path, vf=None, y=540, w=1920):
    cmd=["ffmpeg","-v","error","-i",path]
    if vf: cmd+=["-vf",vf]
    cmd+=["-frames:v","1","-f","rawvideo","-pix_fmt","gray","-"]
    d=run(cmd).stdout
    return list(d[y*w:(y+1)*w])
def width(r):
    lo,hi=25.5,229.5; xs=[]
    def cross(t):
        for i in range(len(r)-1):
            a,b=r[i],r[i+1]
            if a<=t<b: return i+(t-a)/(b-a)
        return None
    a,b=cross(lo),cross(hi); return (b-a) if a is not None and b is not None else float("nan")
out=[]
for name,(sw,sh) in {"1080p":(1920,1080),"4K":(3840,2160)}.items():
    p=f"{W}/edge_{name}.png"; edge_src(sw,sh,p)
    for z in (1.0,1.15,1.5,2.0,2.5,3.0):
        vf=f"scale=iw*{z}*1920/{sw}:ih*{z}*1080/{sh}:flags=bicubic,crop=1920:1080"
        out.append(f"edge\t{name}\tz={z}\t10-90% edge width {width(row(p,vf)):.2f} px")
# control B: blurred edge from 1080p source at z=1
out.append(f"edge-control\t1080p blurred sigma=3\tz=1.0\t10-90% edge width {width(row(W+"/edge_1080p.png","gblur=sigma=3")):.2f} px")
# test 2
mk=f"{W}/marker.png"
run(["ffmpeg","-y","-v","error","-f","lavfi","-i","color=c=black:s=1920x1080:d=1,format=gray","-vf","drawbox=x=1420:y=500:w=40:h=40:color=white:t=fill,drawbox=x=940:y=520:w=40:h=40:color=gray:t=fill","-frames:v","1",mk])
for z in (1.0,1.5,1.9,2.0,2.1,2.5):
    vf=f"scale=iw*{z}:ih*{z}:flags=bicubic,crop=1920:1080"
    d=run(["ffmpeg","-v","error","-i",mk,"-vf",vf,"-frames:v","1","-f","rawvideo","-pix_fmt","gray","-"]).stdout
    white=sum(1 for v in d if v>200)
    out.append(f"offcentre\tmarker centre at 75% width (x=1440)\tz={z}\twhite marker pixels in frame {white}\tarithmetic centre x after zoom {960+(1440-960)*z:.0f} (frame ends at 1920)")
# test 3
for secs in (0.5,1,2,4):
    n=int(secs*30); step=30/n
    out.append(f"speed\t100->130% in {secs} s\t{n} frames\tlinear step {step:.3f} points per frame\t{30/secs:.1f} points per second")
open(os.path.expanduser("~/rt/zoom-in-in-capcut/test-results.txt"),"w").write("\n".join(out)+"\n")
print("\n".join(out))
