Skip to content

Reverse-engineer any video, shot by shot: filmed, AI-generated or coded, and how to remake it. Breakdowns in the open VIDEO.md format. 逆向一切视频,逐个镜头看是拍的、AI 生成的还是代码做的,再告诉你怎么复刻;拆解结果按 VIDEO.md 格式输出。

Time generated clips to the original's moves

june16e683ebba2f
diff --git a/rea_video/media.py b/rea_video/media.py
index cac95d2..a52dc3c 100644
--- a/rea_video/media.py
+++ b/rea_video/media.py
@@ -176,6 +176,10 @@ def iter_frames(
w, h = scaled_size(info, width)
vf = []
if fps:
+ if start is not None and start > 0:
+ # the first frame (half a frame past the seek point) at time zero, or the rate filter repeats it and every
+ # frame after it lands one late
+ vf.append("setpts=PTS-STARTPTS")
vf.append(f"fps={fps:g}")
if crop:
cx, cy, cw, ch = crop
diff --git a/rea_video/remake.py b/rea_video/remake.py
index 76bfa4f..16df45e 100644
--- a/rea_video/remake.py
+++ b/rea_video/remake.py
@@ -1,6 +1,7 @@
"""Rebuild a video on the original timeline from remake results.
-- generated clips (capsule-cinema) -> trimmed / padded to the original shot length
+- generated clips (capsule-cinema) -> retimed to the original shot, frame-exact: the clip's moves are warped onto
+ the original's (where they start, peak and stop), within the speed limits
- still_motion / static_hold shots -> rebuilt locally from one image using the measured
zoom/pan (deterministic, no model call)
- everything else -> original footage kept (marked in the compare video)
@@ -15,6 +16,7 @@ import subprocess
from pathlib import Path
import cv2
+import numpy as np
from .util import LOG, read_json, read_image
@@ -94,6 +96,122 @@ def _exact(fps: float, frames: int) -> tuple[str, list[str]]:
["-frames:v", str(frames), "-r", f"{fps}"])
+WARP_W = 96 # width the motion of a clip is measured at
+WARP_STEP_COST = 0.04 # how much a step away from the clip's mean speed costs against matching the motion better
+
+
+def motion_curve(frames) -> np.ndarray:
+ """How much the picture moves at each frame, 0-1 of its largest value: the change in the most-changed block of a
+ 12 x 8 grid (a small subject moving counts), held 3 frames and smoothed. Flat (all zero) when nothing moves."""
+ out, prev = [], None
+ for f in frames:
+ g = (f if f.ndim == 2 else cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)).astype(np.float32)
+ out.append(0.0 if prev is None else float(cv2.resize(np.abs(g - prev), (12, 8), interpolation=cv2.INTER_AREA).max()) / 255.0)
+ prev = g
+ c = np.array(out)
+ if len(c) >= 3:
+ c = np.array([c[max(0, i - 2):i + 1].max() for i in range(len(c))])
+ c = np.convolve(c, np.ones(3) / 3, mode="same")
+ top = float(c.max()) if len(c) else 0.0
+ return c / top if top >= 0.03 else np.zeros_like(c)
+
+
+def warp_map(ref: np.ndarray, gen: np.ndarray) -> list[int] | None:
+ """For each frame of the original shot (ref), the frame of the generated clip (gen) to show: a monotonic path that
+ lines the clip's moves up with the original's (dynamic time warping on the motion curves), each step 0-3 clip
+ frames (at most MAX_SPEEDUP; holds cost more), ending where the even fit would (the whole clip, or as much as
+ MAX_SPEEDUP allows). None when either has no motion to line up: the even fit is as good."""
+ n, m = len(ref), len(gen)
+ if n < 4 or m < 2 or not ref.any() or not gen.any():
+ return None
+ r = max(1 / MAX_SLOWDOWN, min(MAX_SPEEDUP, m / n))
+ end = min(m - 1, int(round((n - 1) * r)))
+ steps = (0, 1, 2, 3)
+ pen = {k: WARP_STEP_COST * (k - r) ** 2 for k in steps}
+ INF = 1e18
+ D = np.full((n, m), INF)
+ back = np.zeros((n, m), np.int8)
+ D[0, 0] = abs(ref[0] - gen[0])
+ for i in range(1, n):
+ best = np.full(m, INF)
+ arg = np.zeros(m, np.int8)
+ for k in steps:
+ prev = np.full(m, INF)
+ prev[k:] = D[i - 1, :m - k] if k else D[i - 1]
+ cand = prev + pen[k]
+ better = cand < best
+ best[better], arg[better] = cand[better], k
+ D[i] = best + np.abs(ref[i] - gen)
+ back[i] = arg
+ if D[n - 1, end] >= INF:
+ return None
+ path, j = [end], end
+ for i in range(n - 1, 0, -1):
+ j -= int(back[i, j])
+ path.append(j)
+ return path[::-1]
+
+
+def _frames_at(src: Path, fps: float, width: int, start_frame: int = 0, count: int | None = None, gray: bool = False):
+ """Frames of src resampled to fps, from frame start_frame (count of them, or to the end), at the given width."""
+ from .media import iter_frames, video_info
+
+ info = video_info(str(src))
+ it = iter_frames(str(src), info, width, start=start_frame / fps if start_frame else None, gray=gray, fps=fps)
+ for k, f in enumerate(it):
+ if count is not None and k >= count:
+ break
+ yield f
+
+
+def _warp_clip(src: Path, out: Path, path: list[int], size: tuple[int, int], fps: float) -> Path:
+ """Write the clip's frames in the order path gives (each clip frame repeated as often as it is used): exactly
+ len(path) frames, streamed, never the whole clip in memory."""
+ w, h = size
+ need: dict[int, int] = {}
+ for j in path:
+ need[j] = need.get(j, 0) + 1
+ proc = subprocess.Popen(["ffmpeg", "-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "bgr24", "-s", f"{w}x{h}", "-r", f"{fps}",
+ "-i", "-", "-frames:v", str(len(path)), "-c:v", "libx264", "-pix_fmt", "yuv420p", "-crf", "18", str(out)],
+ stdin=subprocess.PIPE)
+ assert proc.stdin is not None
+ written, last = 0, None
+ for j, f in enumerate(_frames_at(src, fps, max(w, int(round(h * 16 / 9))))):
+ if j > path[-1]:
+ break
+ fh, fw = f.shape[:2]
+ s = max(w / fw, h / fh) # cover, then centre crop: as the even fit's scale + crop
+ f = cv2.resize(f, (max(w, int(round(fw * s))), max(h, int(round(fh * s)))), interpolation=cv2.INTER_AREA)
+ y0, x0 = (f.shape[0] - h) // 2, (f.shape[1] - w) // 2
+ last = np.ascontiguousarray(f[y0:y0 + h, x0:x0 + w])
+ for _ in range(need.get(j, 0)):
+ proc.stdin.write(last.tobytes())
+ written += 1
+ while last is not None and written < len(path): # the clip decoded shorter than measured: hold its last frame
+ proc.stdin.write(last.tobytes())
+ written += 1
+ proc.stdin.close()
+ if proc.wait() != 0 or written == 0:
+ raise RuntimeError(f"warp failed for {src}")
+ return out
+
+
+def fit_to_original(src: Path, out: Path, original: Path, first: int, frames: int, size: tuple[int, int], fps: float) -> str:
+ """A generated clip into the original shot's frames: warped so its moves land where the original's do, else
+ sped up or slowed down evenly. Returns how ("warped" / "even")."""
+ try:
+ ref = motion_curve(_frames_at(original, fps, WARP_W, first, frames, gray=True))
+ gen = motion_curve(_frames_at(src, fps, WARP_W, gray=True))
+ path = warp_map(ref[:frames], gen) if len(ref) >= frames - 1 else None
+ if path:
+ _warp_clip(src, out, path + [path[-1]] * (frames - len(path)), size, fps)
+ return "warped"
+ except (OSError, RuntimeError, subprocess.CalledProcessError, ValueError) as exc:
+ LOG.warning("timing the clip to the original failed (%s); fitting it evenly", str(exc)[:120])
+ _fit_clip(src, out, frames, size, fps)
+ return "even"
+
+
def _fit_clip(src: Path, out: Path, frames: int, size: tuple[int, int], fps: float) -> Path:
w, h = size
speed = fit_speed(_clip_seconds(src), frames / fps)
@@ -220,6 +338,7 @@ def assemble(breakdown_path: Path, results_path: Path, out_dir: Path | None = No
results = {**unit_shots(b, results, seg_dir / "units", (w, h), fps, bounds),
**{k: v for k, v in results.items() if not (isinstance(v, dict) and v.get("unit"))}}
parts, legend = [], []
+ timing: dict[str, str] = {} # generated shots: warped onto the original's moves, or fitted evenly
for sh in b["shots"]:
first, n = bounds[sh["index"]]
dur = n / fps
@@ -227,8 +346,9 @@ def assemble(breakdown_path: Path, results_path: Path, out_dir: Path | None = No
res = results.get(str(sh["index"])) or {}
job = jobs.get(sh["index"], {})
if res.get("video") and Path(res["video"]).is_file():
- _fit_clip(Path(res["video"]), seg, n, (w, h), fps)
+ how = fit_to_original(Path(res["video"]), seg, src, first, n, (w, h), fps)
legend.append((sh["index"], "generated"))
+ timing[str(sh["index"])] = how
elif code_video and sh["base"]["label"] == "code_render" and Path(code_video).is_file():
_original_segment(Path(code_video), seg, first, n, (w, h), fps)
legend.append((sh["index"], "code"))
@@ -281,7 +401,8 @@ def assemble(breakdown_path: Path, results_path: Path, out_dir: Path | None = No
except subprocess.CalledProcessError:
if not with_text:
raise
- log = {"legend": legend, "remake": str(final), "compare": str(compare), **({"overlays": overlay_log} if overlays else {})}
+ log = {"legend": legend, "remake": str(final), "compare": str(compare), **({"timing": timing} if timing else {}),
+ **({"overlays": overlay_log} if overlays else {})}
(out_dir / "assemble_log.json").write_text(json.dumps(log, ensure_ascii=False, indent=2), encoding="utf-8")
LOG.info("remake -> %s ; compare -> %s", final, compare)
return json.dumps(log, ensure_ascii=False)
diff --git a/skills/rea-video/references/diy.md b/skills/rea-video/references/diy.md
index 74a63c6..f0bfec2 100644
--- a/skills/rea-video/references/diy.md
+++ b/skills/rea-video/references/diy.md
@@ -16,5 +16,5 @@
1. 按分镜表(`report.md` 的镜头号和时长)把素材对应到镜头。一条长素材覆盖多个镜头时,先用 ffmpeg 按动作切开;拿不准对应关系时给用户看截图确认。
2. 写进 `out/ref/remake/mine.json`:`{"results": {"<镜头号>": {"video": "<素材绝对路径>"}, ...}}`。手动生成的「生成单元」视频覆盖多个镜头时,写成 `{"G2": {"unit": "G2", "shots": [4, 5, 6], "video": "<路径>", "trim_to": <单元秒数>}}`,`remake` 会按原片切点拆回各个镜头。
-3. `rea-video remake out/ref`:每段素材整段变速塞进原镜头时长(最多快 3 倍、慢 1.25 倍,超出部分再裁),切点和原片对齐,没填的镜头暂时用原片占位,声音沿用原片;输出 `remake/remake.mp4`、左右对比的 `remake/compare.mp4` 和 `remake/status.md`。告诉用户哪些镜头还是原片占位、声音还要换。只想拼不想重画代码镜头时用 `rea-video assemble out/ref/breakdown.json <results.json 路径>`。
+3. `rea-video remake out/ref`:每段素材按动作对上原片这一镜的节奏塞进原镜头时长(动作起止、高点落在原片的时间上;最多快 3 倍、慢 1.25 倍,超出部分再裁;没什么动作的素材整段匀速变速),切点按原片帧号对齐、不会越拼越偏,没填的镜头暂时用原片占位,声音沿用原片;输出 `remake/remake.mp4`、左右对比的 `remake/compare.mp4` 和 `remake/status.md`。告诉用户哪些镜头还是原片占位、声音还要换。只想拼不想重画代码镜头时用 `rea-video assemble out/ref/breakdown.json <results.json 路径>`。
- 字幕叠回(`remake` 默认叠,`assemble` 要加 `--overlays`):把测得的字幕 / 花字 / 标题按原样式叠到换掉的镜头上(原片占位的镜头本来就有字,不重复叠)。字体是通用无衬线体,颜色和底框是测量值,可能和原片有出入;贴纸只在 `assemble_log.json` 的 `stickers_to_add` 里列出来,要自己找素材贴。