Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

feat(timeline): measure zoom transition windows on the timeline (#1028)

EtienneLescotebe693541864
diff --git a/src/lib/ai-edition/timeline/speed.ts b/src/lib/ai-edition/timeline/speed.ts
index e90eb922f..446ad2df5 100644
--- a/src/lib/ai-edition/timeline/speed.ts
+++ b/src/lib/ai-edition/timeline/speed.ts
@@ -36,3 +36,24 @@ export function screenTimeMs(regions: readonly SpeedRegion[], timeMs: number): n
}
return screen;
}
+
+/**
+ * Inverse of `screenTimeMs`: the timeline time shown at screen time `screenMs`. Mirror of
+ * `ScreenClock::source_at` in `crates/compositor/src/regions.rs`.
+ */
+export function timelineTimeMs(regions: readonly SpeedRegion[], screenMs: number): number {
+ let lead = 0;
+ let coveredTo = Number.NEGATIVE_INFINITY;
+ for (const region of [...regions].sort((a, b) => a.startMs - b.startMs)) {
+ const start = Math.max(region.startMs, coveredTo);
+ if (region.endMs <= start) continue;
+ const screenStart = start - lead;
+ if (screenMs <= screenStart) break;
+ coveredTo = region.endMs;
+ const speed = Number.isFinite(region.speed) && region.speed > 0 ? region.speed : 1;
+ const screenEnd = screenStart + (region.endMs - start) / speed;
+ if (screenMs < screenEnd) return start + (screenMs - screenStart) * speed;
+ lead += (region.endMs - start) * (1 - 1 / speed);
+ }
+ return screenMs + lead;
+}
diff --git a/src/lib/ai-edition/timeline/zoom-transitions.test.ts b/src/lib/ai-edition/timeline/zoom-transitions.test.ts
new file mode 100644
index 000000000..2d7bab314
--- /dev/null
+++ b/src/lib/ai-edition/timeline/zoom-transitions.test.ts
@@ -0,0 +1,67 @@
+import { describe, expect, it } from "vitest";
+import { zoomTransitionMs } from "../../zoomMath/constants";
+import { type SpeedRegion, screenTimeMs, timelineTimeMs } from "./speed";
+import { transitionCutsMs, zoomTransitions } from "./zoom-transitions";
+
+const W = zoomTransitionMs(1.8); // ≈ 923 ms
+const zoom = { startMs: 10_000, endMs: 12_000, scale: 1.8 };
+
+describe("timelineTimeMs", () => {
+ it("inverts screenTimeMs, overlapping regions included", () => {
+ const regions: SpeedRegion[] = [
+ { id: "a", startMs: 2000, endMs: 6000, speed: 2 },
+ { id: "b", startMs: 4000, endMs: 9000, speed: 4 },
+ { id: "c", startMs: 12_000, endMs: 13_000, speed: 0.5 },
+ ];
+ for (const t of [0, 1999, 2000, 3000, 6000, 7500, 9000, 12_500, 20_000]) {
+ expect(timelineTimeMs(regions, screenTimeMs(regions, t))).toBeCloseTo(t, 6);
+ }
+ });
+});
+
+describe("zoomTransitions", () => {
+ it("puts the moves just outside the pill, deeper zooms longer", () => {
+ const t = zoomTransitions(zoom, []);
+ expect(t.inFromMs).toBeCloseTo(10_000 - W, 6);
+ expect(t.outUntilMs).toBeCloseTo(12_000 + W, 6);
+ expect(zoomTransitions({ ...zoom, scale: 5 }, []).durationMs).toBeGreaterThan(t.durationMs);
+ });
+
+ it("stretches a move across the timeline inside a speed region (issue #1028 variant)", () => {
+ const t = zoomTransitions(zoom, [{ id: "s", startMs: 12_000, endMs: 20_000, speed: 3 }]);
+ expect(t.outUntilMs).toBeCloseTo(12_000 + 3 * W, 6);
+ expect(t.inFromMs).toBeCloseTo(10_000 - W, 6);
+ });
+
+ it("never starts a zoom-in before the timeline", () => {
+ expect(zoomTransitions({ startMs: 300, endMs: 2000, scale: 1.8 }, []).inFromMs).toBe(0);
+ });
+});
+
+describe("transitionCutsMs", () => {
+ const t = zoomTransitions(zoom, []);
+
+ it("reports the whole zoom-out lost to a trim starting at the pill's end (issue #1028)", () => {
+ const cut = transitionCutsMs(zoom, t, [{ startMs: 12_000, endMs: 14_000 }]);
+ expect(cut.inMs).toBe(0);
+ expect(cut.outMs).toBeCloseTo(W, 6);
+ });
+
+ it("measures a partial cut from the first trimmed instant", () => {
+ expect(transitionCutsMs(zoom, t, [{ startMs: 12_500, endMs: 13_000 }]).outMs).toBeCloseTo(
+ 12_000 + W - 12_500,
+ 6,
+ );
+ expect(transitionCutsMs(zoom, t, [{ startMs: 9000, endMs: 9500 }]).inMs).toBeCloseTo(
+ 9500 - (10_000 - W),
+ 6,
+ );
+ });
+
+ it("ignores trims clear of the windows, inside the pill, or swallowing the whole zoom", () => {
+ const none = { inMs: 0, outMs: 0 };
+ expect(transitionCutsMs(zoom, t, [{ startMs: 13_000, endMs: 14_000 }])).toEqual(none);
+ expect(transitionCutsMs(zoom, t, [{ startMs: 10_500, endMs: 11_000 }])).toEqual(none);
+ expect(transitionCutsMs(zoom, t, [{ startMs: 9000, endMs: 14_000 }])).toEqual(none);
+ });
+});
diff --git a/src/lib/ai-edition/timeline/zoom-transitions.ts b/src/lib/ai-edition/timeline/zoom-transitions.ts
new file mode 100644
index 000000000..6d0440ea4
--- /dev/null
+++ b/src/lib/ai-edition/timeline/zoom-transitions.ts
@@ -0,0 +1,77 @@
+// Where a zoom's camera moves sit on the timeline (#1028). A zoom pill shows the HOLD: the
+// zoom-in runs entirely before its start and the zoom-out entirely after its end, each
+// lasting `zoomTransitionMs(scale)` of SCREEN time (`computeRegionStrength`, mirror of
+// `zoom_region_strength` in crates/compositor/src/regions.rs). These helpers turn those
+// windows into timeline ms, through the same speed-aware clock, and measure what a trim cuts
+// off them: the render drops the trimmed frames, so the camera jumps at the cut instead of
+// easing.
+//
+// Chained zooms (closer than 1.5 s, panned between) are measured as if apart. That is the
+// right answer for trims: a trim between two zooms splits them into separate segments, which
+// breaks the chain, and each then needs its own windows untrimmed.
+
+import { zoomTransitionMs } from "../../zoomMath/constants";
+import { type SpeedRegion, screenTimeMs, timelineTimeMs } from "./speed";
+import { ZOOM_DEPTH_SCALES } from "./zoom-scale";
+
+export interface Span {
+ startMs: number;
+ endMs: number;
+}
+
+export interface ZoomTransitions {
+ /** Timeline ms where the zoom-in starts. It lands on the zoom's start. */
+ inFromMs: number;
+ /** Timeline ms where the zoom-out, which leaves from the zoom's end, is over. */
+ outUntilMs: number;
+ /** Screen ms each move lasts, whatever the speed regions do to the footage. */
+ durationMs: number;
+}
+
+/** "0.72 s at 1.25×, …": every depth's move duration, for the agent's tool descriptions. */
+export const ZOOM_TRANSITION_LEGEND = Object.values(ZOOM_DEPTH_SCALES)
+ .sort((a, b) => a - b)
+ .map((scale) => `${(zoomTransitionMs(scale) / 1000).toFixed(2)} s at ${scale.toFixed(2)}×`)
+ .join(", ");
+
+export function zoomTransitions(
+ zoom: Span & { scale: number },
+ speedRegions: readonly SpeedRegion[],
+): ZoomTransitions {
+ const durationMs = zoomTransitionMs(zoom.scale);
+ return {
+ inFromMs: Math.max(
+ 0,
+ timelineTimeMs(speedRegions, screenTimeMs(speedRegions, zoom.startMs) - durationMs),
+ ),
+ outUntilMs: timelineTimeMs(speedRegions, screenTimeMs(speedRegions, zoom.endMs) + durationMs),
+ durationMs,
+ };
+}
+
+/**
+ * Timeline ms of each move the trims cut off (0 when untouched). The zoom-in only plays after
+ * the last trimmed instant before the zoom, the zoom-out only until the first one after it.
+ * A zoom entirely under a trim never plays and the compositor renders it without envelopes
+ * (`under_trim`), so it has nothing to cut.
+ */
+export function transitionCutsMs(
+ zoom: Span,
+ transitions: ZoomTransitions,
+ trims: readonly Span[],
+): { inMs: number; outMs: number } {
+ if (trims.some((t) => t.startMs <= zoom.startMs && t.endMs >= zoom.endMs)) {
+ return { inMs: 0, outMs: 0 };
+ }
+ let resumesAt = transitions.inFromMs;
+ let jumpsAt = transitions.outUntilMs;
+ for (const t of trims) {
+ if (t.startMs < zoom.startMs && t.endMs > transitions.inFromMs) {
+ resumesAt = Math.max(resumesAt, Math.min(t.endMs, zoom.startMs));
+ }
+ if (t.endMs > zoom.endMs && t.startMs < transitions.outUntilMs) {
+ jumpsAt = Math.min(jumpsAt, Math.max(t.startMs, zoom.endMs));
+ }
+ }
+ return { inMs: resumesAt - transitions.inFromMs, outMs: transitions.outUntilMs - jumpsAt };
+}