Download scripts/camera_motion.py from conan123/tinydeskoutstanding-guides: direct link, hf CLI and curl.
- Browser
- Download file 2.75 kB
-
https://huggingface.co/datasets/conan123/tinydeskoutstanding-guides/resolve/main/scripts/camera_motion.py
- Command line
-
hf download hf://datasets/conan123/tinydeskoutstanding-guides/scripts/camera_motion.py
-
curl -L -o camera_motion.py https://huggingface.co/datasets/conan123/tinydeskoutstanding-guides/resolve/main/scripts/camera_motion.py
2.75 kB
| #!/usr/bin/env python3 | |
| """Global camera motion + cut map for a short vertical clip (NumPy + ffmpeg only). | |
| usage: python3 camera_motion.py clip.mp4 [more.mp4 ...] [--fps 12] [--csv out.csv] | |
| """ | |
| import argparse, csv, subprocess, sys | |
| import numpy as np | |
| W, H = 144, 256 # analysis size (9:16); small is enough for global motion | |
| def frames(path, fps): | |
| cmd = ["ffmpeg", "-v", "error", "-i", path, "-vf", | |
| f"fps={fps},scale={W}:{H}:force_original_aspect_ratio=disable,format=gray", | |
| "-f", "rawvideo", "-"] | |
| raw = subprocess.run(cmd, capture_output=True, check=True).stdout | |
| return np.frombuffer(raw, np.uint8).reshape(-1, H, W).astype(np.float32) | |
| WIN = np.outer(np.hanning(H), np.hanning(W)).astype(np.float32) | |
| def phase_corr(a, b): | |
| """Return (dx, dy, peak) so that b is roughly a shifted by (dx, dy).""" | |
| A = np.fft.fft2((a - a.mean()) * WIN) | |
| B = np.fft.fft2((b - b.mean()) * WIN) | |
| R = np.conj(A) * B | |
| R /= np.abs(R) + 1e-9 | |
| r = np.fft.ifft2(R).real | |
| y, x = np.unravel_index(np.argmax(r), r.shape) | |
| if y > H // 2: y -= H | |
| if x > W // 2: x -= W | |
| return float(x), float(y), float(r.max()) | |
| def analyse(path, fps, cut_peak=0.06): | |
| f = frames(path, fps) | |
| rows = [] | |
| for i in range(1, len(f)): | |
| dx, dy, peak = phase_corr(f[i - 1], f[i]) | |
| rows.append({"t": round(i / fps, 3), "dx": dx, "dy": dy, | |
| "peak": round(peak, 3), "cut": peak < cut_peak}) | |
| return rows | |
| def summary(rows): | |
| cuts = [r["t"] for r in rows if r["cut"]] | |
| # merge cut flags closer than 0.25 s (one cut can flag two pairs) | |
| merged = [] | |
| for t in cuts: | |
| if not merged or t - merged[-1] > 0.25: | |
| merged.append(t) | |
| still = [r for r in rows if not r["cut"]] | |
| pan = sum(abs(r["dx"]) for r in still) | |
| tilt = sum(abs(r["dy"]) for r in still) | |
| moving = sum(1 for r in still if abs(r["dx"]) + abs(r["dy"]) >= 1) / max(len(still), 1) | |
| return merged, pan, tilt, moving | |
| if __name__ == "__main__": | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("clips", nargs="+") | |
| ap.add_argument("--fps", type=float, default=12) | |
| ap.add_argument("--csv") | |
| a = ap.parse_args() | |
| out = [] | |
| print(f"{'clip':<14}{'cuts (s)':<34}{'pan px':>8}{'tilt px':>8}{'moving':>8}") | |
| for c in a.clips: | |
| rows = analyse(c, a.fps) | |
| cuts, pan, tilt, moving = summary(rows) | |
| name = c.rsplit("/", 1)[-1][:12] | |
| print(f"{name:<14}{', '.join(f'{t:.2f}' for t in cuts):<34}{pan:>8.0f}{tilt:>8.0f}{moving:>8.0%}") | |
| out += [{"clip": name, **r} for r in rows] | |
| if a.csv: | |
| with open(a.csv, "w", newline="") as fh: | |
| w = csv.DictWriter(fh, fieldnames=list(out[0])) | |
| w.writeheader(); w.writerows(out) | |