Skip to content

Open source CapCut alternative.

TypeScript

fix(site): filmstrip tiles carry true frames at every zoom

David Le38a786086ee4
diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsx
index 14f8f9b8..54ba2a9c 100644
--- a/site/src/cut/components/Timeline.tsx
+++ b/site/src/cut/components/Timeline.tsx
@@ -4203,9 +4203,8 @@ function seedThumb(
* tiles at each end). Tiles keep the asset's aspect until the box would blow
* the tile cap; past that they widen by whole grid cells, still on the grid.
* The first and last tiles pin to the segment's exact boundary frames once
- * captured; middle tiles show the nearest pre-sampled thumb until the zoom
- * outruns the strip's sampling, when each carries a true frame at its own
- * moment as its capture lands (see `planFilmstrip`). */
+ * captured; middle tiles show the nearest pre-sampled thumb until each one's
+ * true frame at its own moment lands (see `planFilmstrip`). */
function filmstripFrames(
asset: MediaAsset | undefined,
filmIn: number,
@@ -4276,7 +4275,7 @@ function useTileFrames(
useEffect(() => {
tilesRef.current = tiles;
});
- const wanted = tiles.some((t) => t.wantT !== null);
+ const wanted = tiles.length > 0;
useEffect(() => {
const box = boxRef.current;
const url = asset?.type === "video" ? asset.url : null;
@@ -4308,7 +4307,7 @@ function useTileFrames(
hi = view.right - boxR.left + TILE_CAPTURE_MARGIN;
}
tilesRef.current.forEach((t, i) => {
- if (t.wantT === null || t.exact) return;
+ if (t.exact) return;
if (t.left + t.width < lo || t.left > hi) return;
const key = `${i}:${t.wantT.toFixed(2)}`;
if (asked.has(key)) return;
diff --git a/site/src/cut/lib/filmstrip.test.ts b/site/src/cut/lib/filmstrip.test.ts
index af8146d1..d7624886 100644
--- a/site/src/cut/lib/filmstrip.test.ts
+++ b/site/src/cut/lib/filmstrip.test.ts
@@ -52,21 +52,15 @@ describe("planFilmstrip", () => {
}
});
- test("zoomed out past the strip's sampling, no tile asks for a capture", () => {
- const tiles = plan({ pps: 30 });
- expect(tiles.every((t) => t.wantT === null && !t.exact)).toBe(true);
- });
-
- test("zoomed in, every tile asks for a capture at its own midpoint", () => {
+ test("every tile asks for a capture at its own midpoint, at every zoom", () => {
// Half the capture grid is the whole allowance: a capture pulled further
// toward a tile's edge lands on the neighboring scene whenever a cut sits
// inside the tile, and the strip flips a whole tile early.
- for (const pps of [120, 200, 300, 480, 800]) {
+ for (const pps of [30, 60, 120, 200, 300, 480, 800]) {
const tiles = plan({ pps });
for (const t of tiles) {
- expect(t.wantT === null).toBe(false);
- if (t.wantT! <= 0 || t.wantT! >= DURATION - 0.05) continue;
- expect(Math.abs(t.wantT! - t.srcT)).toBeLessThanOrEqual(WANT_GRID_S / 2 + 1e-9);
+ if (t.wantT <= 0 || t.wantT >= DURATION - 0.05) continue;
+ expect(Math.abs(t.wantT - t.srcT)).toBeLessThanOrEqual(WANT_GRID_S / 2 + 1e-9);
expect(t.exact).toBe(false);
}
}
@@ -78,7 +72,7 @@ describe("planFilmstrip", () => {
// where scene cuts live, so a straddling tile stays on its midpoint's
// side of the cut.
for (const t of plan({ pps: 300 })) {
- if (t.wantT === null || t.wantT <= 0 || t.wantT >= DURATION - 0.05) continue;
+ if (t.wantT <= 0 || t.wantT >= DURATION - 0.05) continue;
const cells = t.wantT / WANT_GRID_S - 0.5;
expect(Math.abs(cells - Math.round(cells))).toBeLessThan(1e-6);
}
@@ -171,9 +165,22 @@ describe("planFilmstrip", () => {
expect(tiles.map((t) => t.left).some((x) => Math.abs(x - 3.0 * pps) < 1e-6)).toBe(true);
});
- test("zoomed out, cuts split nothing", () => {
- const zoomedOut = plan({ pps: 30, cuts: [3.0, 6.0, 9.0] });
- expect(zoomedOut).toEqual(plan({ pps: 30 }));
+ test("zoomed out, cuts still become tile edges and each side captures its own scene", () => {
+ // Fast-cut footage keeps its scenes visible at the widest zoom: a scene
+ // shorter than the tile grid still gets its own tile and its own frame.
+ const pps = 30;
+ const cuts = [1.0, 3.0, 6.0, 9.0];
+ const tiles = plan({ pps, cuts });
+ const edges = tiles.map((t) => t.left);
+ for (const c of cuts) expect(edges.some((x) => Math.abs(x - c * pps) < 1e-6)).toBe(true);
+ for (const t of tiles) {
+ const a = t.left / pps;
+ const b = (t.left + t.width) / pps;
+ for (const c of cuts) {
+ if (b <= c + 1e-9) expect(t.wantT).toBeLessThan(c);
+ else if (a >= c - 1e-9) expect(t.wantT).toBeGreaterThan(c);
+ }
+ }
});
test("no cuts leaves the plan unchanged", () => {
diff --git a/site/src/cut/lib/filmstrip.ts b/site/src/cut/lib/filmstrip.ts
index 164cb89c..eb84afdd 100644
--- a/site/src/cut/lib/filmstrip.ts
+++ b/site/src/cut/lib/filmstrip.ts
@@ -4,27 +4,28 @@
* Tiles sit on a source-time grid, so the strip holds still while a trimmed
* edge sweeps across it. Each tile spans `stepT` seconds of source; the asset
* carries a pre-sampled strip with one thumb every `thumbStep` seconds
- * (roughly two). While a tile spans at least that much, the nearest
- * pre-sampled thumb is a faithful picture of the tile's whole span.
+ * (roughly two). While a tile spans at least that much, it shows the thumb
+ * from the scene that owns most of its span, so a cut inside a wide tile
+ * leaves the tile picturing what mostly plays there.
*
- * Zoomed past that, a tile spans a fraction of the sampling interval, and on
- * footage that cuts between scenes the nearest thumb can show a moment up to
- * `thumbStep / 2` away — a visibly wrong scene under the playhead. Those
- * tiles each name a capture time (`wantT`) for a true frame at their own
+ * Every tile also names a capture time (`wantT`) for a true frame at its own
* moment: the tile's midpoint, on a fine fixed grid (tenths of a second) so
* cache keys hold still through scrolls and trims. The midpoint is the one
* time that keeps a tile on its own side of a scene cut — a capture pulled
* toward the tile's edge lands on the neighboring scene whenever a cut sits
* inside the tile, and the strip then flips a whole tile early. `exactFrame`
* answers with captures already in hand; a tile without one keeps the nearest
- * pre-sampled thumb until its capture lands.
+ * pre-sampled thumb until its capture lands, so the strip sharpens in place
+ * at every zoom.
*
- * Detected scene cuts (`cuts`, measured at import) become tile edges while
- * refining: each scene divides into even tiles as close to the grid width as
- * its span allows, so the strip changes picture at the cut's own pixel, the
+ * Detected scene cuts (`cuts`, measured at import) become tile edges at every
+ * zoom: each scene divides into even tiles as close to the grid width as its
+ * span allows, so the strip changes picture at the cut's own pixel, the
* boundary area under the pointer shows what actually plays there, and tiles
* between two cuts all share one width — a scene shorter than a tile is the
- * one place a narrow tile is the truth.
+ * one place a narrow tile is the truth. Fast-cut footage keeps its scenes
+ * visible zoomed all the way out; only a cut within a few pixels of another
+ * edge merges away.
*/
export type FilmTile = {
@@ -34,9 +35,8 @@ export type FilmTile = {
width: number;
/** The tile's midpoint in source seconds. */
srcT: number;
- /** Capture time for a true frame; null while the pre-sampled strip is
- * already finer than the tile grid. */
- wantT: number | null;
+ /** Capture time for the tile's true frame. */
+ wantT: number;
/** `src` is a true frame at `wantT`. */
exact: boolean;
};
@@ -124,7 +124,6 @@ export function planFilmstrip(p: {
const imgW = natural * Math.ceil(cells / FILM_TILE_CAP);
const stepT = (imgW / p.pps) * p.speed;
const filmOut = p.filmIn + (p.w / p.pps) * p.speed;
- const refine = stepT < p.thumbStep;
// A tile narrower than a few pixels reads as a rendering artifact; a cut
// that close to an edge already flips within that distance.
const minSubT = (MIN_SUB_TILE_PX / p.pps) * p.speed;
@@ -132,10 +131,8 @@ export function planFilmstrip(p: {
// the source and the zoom: scrolls and trims re-plan onto the same tiles.
const end = Math.max(p.duration, filmOut);
const seams = [0];
- if (refine) {
- for (const c of p.cuts ?? []) {
- if (c >= seams[seams.length - 1] + minSubT && c <= end - minSubT) seams.push(c);
- }
+ for (const c of p.cuts ?? []) {
+ if (c >= seams[seams.length - 1] + minSubT && c <= end - minSubT) seams.push(c);
}
seams.push(end);
const tiles: FilmTile[] = [];
@@ -153,23 +150,20 @@ export function planFilmstrip(p: {
const mid = (a + b) / 2;
const idx = Math.min(p.thumbs.length - 1, Math.max(0, Math.floor(mid / p.thumbStep)));
let src = p.thumbs[idx];
- let wantT: number | null = null;
let exact = false;
- if (refine) {
- // Grid centers, never grid boundaries: cuts sit on frame boundaries
- // (whole and half seconds above all), and a capture that lands
- // exactly on a cut decodes the next scene's first frame — a tile
- // whose midpoint sits a hair before a cut would flip to the wrong
- // side of it. A tile too narrow for a grid center inside it captures
- // at its own midpoint.
- const snapped = (Math.floor(mid / WANT_GRID_S) + 0.5) * WANT_GRID_S;
- wantT = snapped > a && snapped < b ? snapped : mid;
- wantT = Math.max(0, Math.min(p.duration - 0.05, wantT));
- const hit = p.exactFrame?.(wantT) ?? null;
- if (hit) {
- src = hit;
- exact = true;
- }
+ // Grid centers, never grid boundaries: cuts sit on frame boundaries
+ // (whole and half seconds above all), and a capture that lands
+ // exactly on a cut decodes the next scene's first frame — a tile
+ // whose midpoint sits a hair before a cut would flip to the wrong
+ // side of it. A tile too narrow for a grid center inside it captures
+ // at its own midpoint.
+ const snapped = (Math.floor(mid / WANT_GRID_S) + 0.5) * WANT_GRID_S;
+ let wantT = snapped > a && snapped < b ? snapped : mid;
+ wantT = Math.max(0, Math.min(p.duration - 0.05, wantT));
+ const hit = p.exactFrame?.(wantT) ?? null;
+ if (hit) {
+ src = hit;
+ exact = true;
}
tiles.push({
src,
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index 3c1b60de..01288049 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -2166,6 +2166,7 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
if (patch.thumbs !== undefined) runtime.thumbs = patch.thumbs;
if (patch.thumbStep !== undefined) runtime.thumbStep = patch.thumbStep;
if (patch.peaks !== undefined) runtime.peaks = patch.peaks;
+ if (patch.sceneCuts !== undefined) runtime.sceneCuts = patch.sceneCuts;
if (Object.keys(runtime).length === 0) return;
hydrating = true;
try {
@@ -4838,7 +4839,7 @@ export const docOverlays = (() => {
export function storedAssets(assets: MediaAsset[]): StoredAsset[] {
return assets
.filter((a) => !tabOnlyUpload(a))
- .map(({ id, fileName, name, type, duration, width, height, origin, chatId, language, watch, speech, beats }) => ({
+ .map(({ id, fileName, name, type, duration, width, height, origin, chatId, language, watch, speech, beats, sceneCuts }) => ({
id,
fileName,
name,
@@ -4852,6 +4853,7 @@ export function storedAssets(assets: MediaAsset[]): StoredAsset[] {
...(watch !== undefined ? { watch } : {}),
...(speech !== undefined ? { speech } : {}),
...(beats !== undefined ? { beats } : {}),
+ ...(sceneCuts !== undefined ? { sceneCuts } : {}),
}));
}