Skip to content

Open source CapCut alternative.

TypeScript

fix(site): filmstrip tiles stay even between scene cuts

David Le481d0899e575
diff --git a/site/src/cut/lib/filmstrip.test.ts b/site/src/cut/lib/filmstrip.test.ts
index 1baf4e30..af8146d1 100644
--- a/site/src/cut/lib/filmstrip.test.ts
+++ b/site/src/cut/lib/filmstrip.test.ts
@@ -130,14 +130,12 @@ describe("planFilmstrip", () => {
expect(plan({ thumbs: [] })).toEqual([]);
});
- test("a scene cut inside a refining tile splits it at the cut", () => {
+ test("a scene cut becomes a tile edge, and each side captures its own scene", () => {
const pps = 300;
const cut = 3.0;
const tiles = plan({ pps, cuts: [cut] });
- // Some tile boundary sits exactly on the cut...
const edges = tiles.map((t) => t.left);
expect(edges.some((x) => Math.abs(x - cut * pps) < 1e-6)).toBe(true);
- // ...and each side captures strictly on its own side of it.
for (const t of tiles) {
const a = t.left / pps;
const b = (t.left + t.width) / pps;
@@ -146,19 +144,31 @@ describe("planFilmstrip", () => {
}
});
- test("split tiles still tile the strip without gaps", () => {
+ test("cut-aligned tiles still tile the strip without gaps", () => {
const tiles = plan({ pps: 300, cuts: [1.7, 3.0, 9.13] });
for (let i = 1; i < tiles.length; i++) {
expect(tiles[i].left).toBeCloseTo(tiles[i - 1].left + tiles[i - 1].width, 6);
}
});
- test("a cut a sliver from a tile boundary does not split off a sub-pixel tile", () => {
+ test("tiles between two cuts share one width", () => {
const pps = 300;
- const stepT = plan({ pps })[1].width / pps;
- const sliver = stepT + MIN_SUB_TILE_PX / pps / 2; // just inside tile 1
- const tiles = plan({ pps, cuts: [sliver] });
+ const tiles = plan({ pps, cuts: [3.0, 9.13] });
+ const widthsIn = (lo: number, hi: number) =>
+ tiles
+ .filter((t) => t.left >= lo * pps - 1e-6 && t.left + t.width <= hi * pps + 1e-6)
+ .map((t) => t.width);
+ for (const ws of [widthsIn(0, 3), widthsIn(3, 9.13), widthsIn(9.13, DURATION)]) {
+ expect(ws.length).toBeGreaterThan(1);
+ for (const w of ws) expect(w).toBeCloseTo(ws[0], 6);
+ }
+ });
+
+ test("cuts crowding the ends or each other merge instead of leaving slivers", () => {
+ const pps = 300;
+ const tiles = plan({ pps, cuts: [0.001, 3.0, 3.005, 23.659] });
for (const t of tiles) expect(t.width).toBeGreaterThanOrEqual(MIN_SUB_TILE_PX - 1e-6);
+ expect(tiles.map((t) => t.left).some((x) => Math.abs(x - 3.0 * pps) < 1e-6)).toBe(true);
});
test("zoomed out, cuts split nothing", () => {
diff --git a/site/src/cut/lib/filmstrip.ts b/site/src/cut/lib/filmstrip.ts
index 56fd158f..164cb89c 100644
--- a/site/src/cut/lib/filmstrip.ts
+++ b/site/src/cut/lib/filmstrip.ts
@@ -19,10 +19,12 @@
* answers with captures already in hand; a tile without one keeps the nearest
* pre-sampled thumb until its capture lands.
*
- * Detected scene cuts (`cuts`, measured at import) split any refining tile
- * they pass through: each side captures its own scene, so the strip changes
- * picture at the cut's own pixel and the boundary area under the pointer
- * shows what actually plays there.
+ * 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
+ * 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.
*/
export type FilmTile = {
@@ -47,7 +49,8 @@ export const FILM_TILE_CAP = 120;
* a strip re-planned by a scroll or a trim asks for the same times again. */
export const WANT_GRID_S = 0.1;
-/** Narrowest sub-tile a scene cut may split off. */
+/** Narrowest tile a scene cut may leave behind; cuts closer than this to the
+ * source's ends or to each other merge away. */
export const MIN_SUB_TILE_PX = 5;
/** A probe of the source: a moment, and whether the picture changed scene
@@ -108,9 +111,9 @@ export function planFilmstrip(p: {
speed: number;
tileH: number;
minTileW: number;
- /** Scene changes in the source, seconds. A refining tile a cut passes
- * through splits at it, so the strip changes picture at the cut's own
- * pixel; the boundary area then shows what actually plays there. */
+ /** Scene changes in the source, seconds, ascending. While refining they
+ * become tile edges, so the strip changes picture at the cut's own pixel;
+ * the boundary area then shows what actually plays there. */
cuts?: number[];
/** Synchronous lookup of a captured true frame at a `wantT`. */
exactFrame?: (wantT: number) => string | null;
@@ -121,23 +124,32 @@ 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 first = Math.max(0, Math.floor(p.filmIn / stepT));
- const last = Math.max(first, Math.ceil(filmOut / stepT) - 1);
const refine = stepT < p.thumbStep;
- // A sub-tile narrower than a few pixels reads as a rendering artifact; a
- // cut that close to a tile boundary already flips within that distance.
+ // 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;
+ // Scene seams partition the whole source, so the tiling depends only on
+ // 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);
+ }
+ }
+ seams.push(end);
const tiles: FilmTile[] = [];
- for (let k = first; k <= last; k++) {
- const t0 = k * stepT;
- const t1 = (k + 1) * stepT;
- const inner = refine
- ? (p.cuts ?? []).filter((c) => c > t0 + minSubT && c < t1 - minSubT).slice(0, 2)
- : [];
- const bounds = [t0, ...inner, t1];
- for (let s = 0; s + 1 < bounds.length; s++) {
- const a = bounds[s];
- const b = bounds[s + 1];
+ for (let s = 0; s + 1 < seams.length; s++) {
+ const segA = seams[s];
+ const segB = seams[s + 1];
+ if (segB <= p.filmIn || segA >= filmOut) continue;
+ const n = Math.max(1, Math.round((segB - segA) / stepT));
+ const tw = (segB - segA) / n;
+ const i0 = Math.max(0, Math.floor((p.filmIn - segA) / tw));
+ const i1 = Math.min(n - 1, Math.ceil((filmOut - segA) / tw) - 1);
+ for (let i = i0; i <= i1; i++) {
+ const a = segA + i * tw;
+ const b = segA + (i + 1) * tw;
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];
@@ -148,8 +160,8 @@ export function planFilmstrip(p: {
// (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 sub-tile too narrow for a grid center inside it
- // captures at its own midpoint.
+ // 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));