Skip to content

Open source CapCut alternative.

TypeScript

feat(site): point a sweep at missing borders and bands [rebuild]

David Le958471fec82f
diff --git a/site/src/cut/components/AiPanel.tools.ts b/site/src/cut/components/AiPanel.tools.ts
index 213dffd5..be17f2b5 100644
--- a/site/src/cut/components/AiPanel.tools.ts
+++ b/site/src/cut/components/AiPanel.tools.ts
@@ -188,12 +188,12 @@ export const AI_PANEL_TOOLS = [
description:
"Check the cut against the thing it was built from, in numbers and in pictures. Every frame is scored by `diff`: the mean difference per pixel between your frame and the source's, 0 (the same) to 255, read at one small size so a score means the same thing in every call; under " +
`${SWEEP_TARGET} reads as the same picture. ` +
- "mode sweep scores every step of the cut (or from/to) with no pictures and returns the median, p95 and max with the worst stretches, each with its peak frame and a 4×4 grid of where in the frame it differs. " +
+ "mode sweep scores every step of the cut (or from/to) with no pictures and returns the median, p95 and max with the worst stretches, each with its peak frame and a 4×4 grid of where in the frame it differs, and `edges`: stretches whose difference runs along whole edges of the frame while the middle matches — a border, band or letterbox — listed under target too, since thin bars barely move the mean. " +
"A decoder that fails mid-read is replaced and the read goes on: `lost` names source frames no decoder read, and a source that stops decoding ends the sweep at `coveredTo` with `stopped` saying where and why. " +
`mode moments (the default) takes up to ${MAX_COMPARE} \`times\` and hands back each as the source's frame and yours side by side, with its score and grid. ` +
"Either mode says when a frame matches the source better a few frames away: `shift` is that distance, and a positive one means move what is there later by that much. " +
- "This is how a replica is verified. The loop: sweep, look at the worst stretches' peaks in moments mode, fix what differs, sweep again — until the median is under " +
- `${SWEEP_TARGET} and no stretch stands out. ` +
+ "This is how a replica is verified. The loop: sweep, look at the peaks of the worst stretches and the edges in moments mode, fix what differs, sweep again — until the median is under " +
+ `${SWEEP_TARGET} and no stretch in worst or edges stands out. ` +
"Blocks standing in for footage keep a difference no edit removes, so read those by the grid cells over your titles and shapes. " +
"Pass `times` as timeline seconds, or {at, source} pairs when the cut sits at a different second than the source does; `offset` maps every bare second and the whole sweep (source second = timeline second + offset). " +
"Only the source's picture box is held against your frame (find_picture), so a reference recorded inside an app scores its picture alone.",
diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.ts
index 3e07e266..f34b81cc 100644
--- a/site/src/cut/lib/aiTools.ts
+++ b/site/src/cut/lib/aiTools.ts
@@ -734,7 +734,8 @@ const COMPARE_NOTE =
const SWEEP_NOTE =
"diff is the mean difference per pixel between your frame and the source's, 0 (the same) to 255; median is the typical frame, p95 the bad tail, max the worst. " +
"worst lists the stretches most off, worst first, each with its peak frame (at on the cut, source on the source) and that frame's 4×4 grid, top row first. " +
- "Look at them: compare_to_source with times [{at, source}] at those peaks, fix what differs, then sweep again, until the median is under target. " +
+ "edges lists stretches whose difference runs along whole edges of the frame (top, bottom, left, right) while the middle matches: a border, band or letterbox one side has and the other lacks. Thin bars barely move the mean, so these come back under target too. " +
+ "Look at them: compare_to_source with times [{at, source}] at those peaks, fix what differs, then sweep again, until the median is under target and each stretch in worst and edges is fixed or is a difference no edit removes. " +
"shift on a stretch, or timing for the whole span, means the source matches better that many seconds away: positive, move it later by that much; negative, earlier. " +
"onBlocks is the share of samples over blocks standing in for footage; those keep a difference no edit removes.";
/** Faces suggest_font hands back by default, and at most. */
diff --git a/site/src/cut/lib/frameDiff.test.ts b/site/src/cut/lib/frameDiff.test.ts
index 940528ac..f362f130 100644
--- a/site/src/cut/lib/frameDiff.test.ts
+++ b/site/src/cut/lib/frameDiff.test.ts
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test";
-import { bestShift, diffPixels, meanDiff, runShift, sweepStats, worstStretches } from "./frameDiff";
+import { bestShift, diffPixels, edgeBand, meanDiff, runShift, sweepStats, worstStretches } from "./frameDiff";
// The numbers a replica is judged by. A score is only useful to the model if
// it means the same thing every call: 0 for identical frames, 255 for black
@@ -120,3 +120,23 @@ test("a run of sweep samples that each match the source a step later reads as on
expect(runShift(near, 1, 1, 1, 0.5)?.shift).toBe(0.5);
expect(runShift(new Float32Array([NaN, 9, NaN]), 1, 0, 0, 0.5)).toBeUndefined();
});
+
+test("a difference along a whole edge while the middle matches is a band, under target too", () => {
+ // Thin black bars over a colour: the top and bottom rows of the grid run
+ // warm end to end, the middle is quiet, and the frame's mean stays low.
+ const bars = [
+ [7, 7, 8, 7],
+ [1, 0, 1, 1],
+ [1, 1, 0, 1],
+ [6, 7, 7, 7],
+ ];
+ expect(edgeBand(bars, 5)).toEqual({ edges: ["top", "bottom"], diff: 7 });
+ const pillars = bars[0].map((_, c) => bars.map((row) => row[c]));
+ expect(edgeBand(pillars, 5)?.edges).toEqual(["left", "right"]);
+ // A title near the top warms part of that row, not all of it.
+ expect(edgeBand([[9, 9, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], 5)).toBeNull();
+ // A frame off everywhere is off everywhere, not at its edges.
+ expect(edgeBand([[9, 9, 9, 9], [9, 9, 9, 9], [9, 9, 9, 9], [9, 9, 9, 9]], 5)).toBeNull();
+ // A faint edge is noise.
+ expect(edgeBand([[3, 3, 3, 3], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], 5)).toBeNull();
+});
diff --git a/site/src/cut/lib/frameDiff.ts b/site/src/cut/lib/frameDiff.ts
index 5700597a..b62d3d82 100644
--- a/site/src/cut/lib/frameDiff.ts
+++ b/site/src/cut/lib/frameDiff.ts
@@ -221,3 +221,30 @@ export function worstStretches(
});
return stretches.sort((a, b) => b.weight - a.weight).slice(0, limit);
}
+
+export type Edge = "top" | "bottom" | "left" | "right";
+
+/**
+ * The edges of a frame a difference runs the whole length of while its
+ * middle matches: a border, a band or a letterbox one picture has and the
+ * other lacks. Thin bars move a frame's mean little, so this reads the grid:
+ * every cell along the edge over the target, and the middle cells under a
+ * third of that edge's least. `diff` is the mean over those edges' cells.
+ */
+export function edgeBand(grid: number[][], target: number): { edges: Edge[]; diff: number } | null {
+ const n = grid.length;
+ if (n < 3 || grid.some((row) => row.length !== n)) return null;
+ const middle: number[] = [];
+ for (let r = 1; r < n - 1; r++) for (let c = 1; c < n - 1; c++) middle.push(grid[r][c]);
+ const calm = middle.reduce((a, b) => a + b, 0) / middle.length;
+ const lines: [Edge, number[]][] = [
+ ["top", grid[0]],
+ ["bottom", grid[n - 1]],
+ ["left", grid.map((row) => row[0])],
+ ["right", grid.map((row) => row[n - 1])],
+ ];
+ const hot = lines.filter(([, cells]) => Math.min(...cells) > target && calm * 3 <= Math.min(...cells));
+ if (hot.length === 0) return null;
+ const cells = hot.flatMap(([, c]) => c);
+ return { edges: hot.map(([e]) => e), diff: round1(cells.reduce((a, b) => a + b, 0) / cells.length) };
+}
diff --git a/site/src/cut/lib/sourceCompare.ts b/site/src/cut/lib/sourceCompare.ts
index 68ce9d6f..35337299 100644
--- a/site/src/cut/lib/sourceCompare.ts
+++ b/site/src/cut/lib/sourceCompare.ts
@@ -1,7 +1,7 @@
import { renderProjectFrames } from "./exportRender";
import { FULL_FRAME, isFullFrame } from "./measure/picture";
import type { FrameBox } from "./measure/sourceMeasure";
-import { bestShift, diffPixels, meanDiff, runShift, SCORE_LONG_SIDE, SWEEP_TARGET, sweepStats, worstStretches } from "./frameDiff";
+import { bestShift, diffPixels, edgeBand, meanDiff, runShift, SCORE_LONG_SIDE, SWEEP_TARGET, sweepStats, worstStretches, type Edge } from "./frameDiff";
import { composeComparison, makeStillFrame } from "./media";
import type { WrappedCanvas } from "mediabunny";
import { framesAt, type FrameSize } from "./mediaRead";
@@ -398,6 +398,9 @@ export type SweepResult = {
max: number;
target: number;
worst: Stretch[];
+ /** Stretches whose difference runs along whole edges of the frame while
+ * the middle matches — a border or band one side lacks — under target too. */
+ edges?: (Omit<Stretch, "shift"> & { edges: Edge[] })[];
timing?: { shift: number; diff: number };
onBlocks?: number;
frame?: string;
@@ -539,6 +542,28 @@ export async function sweepSource(
...(block ? { block } : {}),
};
});
+ // Thin bars barely move a frame's mean, so they are read off the grids.
+ const bands = scored.map((sc) => edgeBand(grids[sc.i], SWEEP_TARGET));
+ const edges = worstStretches(
+ scored.map((sc, k) => ({ at: sc.at, diff: bands[k]?.diff ?? 0 })),
+ step,
+ SWEEP_TARGET,
+ MAX_STRETCHES
+ ).map((w) => {
+ const peak = scored[w.peakIndex];
+ const block = blockAt(doc, peak.at);
+ return {
+ from: w.from,
+ to: round3(Math.min(w.to, to)),
+ diff: w.diff,
+ peak: w.peak,
+ at: peak.at,
+ source: samples[peak.i].source,
+ edges: bands[w.peakIndex]?.edges ?? [],
+ grid: grids[peak.i],
+ ...(block ? { block } : {}),
+ };
+ });
const timing = still ? undefined : runShift(near, SHIFT_REACH, 0, diffs.length - 1, step);
const onBlocks = scored.filter((s) => blockAt(doc, s.at)).length / scored.length;
const frame = frameNote(doc, scorer.letterboxed, area);
@@ -554,6 +579,7 @@ export async function sweepSource(
...stats,
target: SWEEP_TARGET,
worst,
+ ...(edges.length > 0 ? { edges } : {}),
...(timing ? { timing } : {}),
...(onBlocks > 0 ? { onBlocks: Math.round(onBlocks * 100) / 100 } : {}),
...(frame ? { frame } : {}),