Skip to content

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

fix(auto-zoom): keep the video's first and last seconds wide

EtienneLescot0a517418fc3f
diff --git a/src/components/ai-edition/recordingImport.test.ts b/src/components/ai-edition/recordingImport.test.ts
index c98cbb32e..6ef1a4c62 100644
--- a/src/components/ai-edition/recordingImport.test.ts
+++ b/src/components/ai-edition/recordingImport.test.ts
@@ -359,8 +359,8 @@ describe("fresh-recording auto-zoom", () => {
});
expect(next.zoomRanges).toHaveLength(1);
expect(next.zoomRanges[0]).toMatchObject({
- startMs: 3000,
- endMs: 5000,
+ startMs: 3424,
+ endMs: 5424,
focusMode: "auto",
});
expect(await applyPendingFreshRecordingAutoZooms(next)).toBe(next);
diff --git a/src/lib/ai-edition/timeline/apply-auto-zooms.test.ts b/src/lib/ai-edition/timeline/apply-auto-zooms.test.ts
index 484c79fc9..99226150b 100644
--- a/src/lib/ai-edition/timeline/apply-auto-zooms.test.ts
+++ b/src/lib/ai-edition/timeline/apply-auto-zooms.test.ts
@@ -63,8 +63,9 @@ describe("collectAutoZoomSuggestionsForDocument", () => {
dwell(4000, 0.4, 0.6),
);
expect(suggestions).toHaveLength(1);
- expect(suggestions[0].span.start).toBe(3000);
- expect(suggestions[0].span.end).toBe(5000);
+ // Centred on the dwell would be 3000..5000; the wide opening holds it to 3424.
+ expect(suggestions[0].span.start).toBe(3424);
+ expect(suggestions[0].span.end).toBe(5424);
expect(suggestions[0].focus.cx).toBeCloseTo(0.4, 5);
});
diff --git a/src/lib/ai-edition/timeline/zoom-suggestions.test.ts b/src/lib/ai-edition/timeline/zoom-suggestions.test.ts
index 3cff335ef..d8ba654ff 100644
--- a/src/lib/ai-edition/timeline/zoom-suggestions.test.ts
+++ b/src/lib/ai-edition/timeline/zoom-suggestions.test.ts
@@ -506,7 +506,7 @@ describe("buildAutoZoomSuggestionsForClips", () => {
defaultDurationMs: 2000,
});
expect(suggestions.map((s) => s.span)).toEqual([
- { start: 3000, end: 5000 }, // clip_1: source 4s sits at ruler 4s
+ { start: 3424, end: 5424 }, // clip_1: source 4s sits at ruler 4s, after the wide opening
{ start: 13000, end: 15000 }, // clip_2: the SAME source 4s sits at ruler 14s
]);
for (const suggestion of suggestions) {
@@ -525,7 +525,7 @@ describe("buildAutoZoomSuggestionsForClips", () => {
existingRegions: [],
defaultDurationMs: 2000,
});
- expect(suggestions.map((s) => s.span)).toEqual([{ start: 3000, end: 5000 }]);
+ expect(suggestions.map((s) => s.span)).toEqual([{ start: 3424, end: 5424 }]);
});
it("ignores a dwell that falls outside every clip's source window", () => {
@@ -594,7 +594,7 @@ describe("buildAutoZoomSuggestionsForClips", () => {
existingRegions: [],
defaultDurationMs: 2000,
});
- expect(suggestions.map((s) => s.span)).toEqual([{ start: 3000, end: 5000 }]);
+ expect(suggestions.map((s) => s.span)).toEqual([{ start: 3424, end: 5424 }]);
expect(suggestions[0].focus).toEqual({ cx: 0.5, cy: 0.5 });
});
@@ -608,7 +608,7 @@ describe("buildAutoZoomSuggestionsForClips", () => {
defaultDurationMs: 2000,
});
expect(suggestions.map((s) => s.span)).toEqual([
- { start: 3000, end: 5000 },
+ { start: 3424, end: 5424 },
{ start: 13000, end: 15000 },
]);
for (const suggestion of suggestions) {
@@ -619,18 +619,101 @@ describe("buildAutoZoomSuggestionsForClips", () => {
it("zooms a clip whose source window trims the telemetry down to a single click", () => {
// A clip covering 30..40s of the recording; the take's telemetry holds one move
- // long before the window and one click inside it. The per-clip filter hands the
- // detector a SINGLE sample — still a zoom, correctly projected (source 34s is
- // ruler 4s).
+ // long before the window, one click inside it and one move long after. The per-clip
+ // filter hands the detector a SINGLE sample — still a zoom, correctly projected
+ // (source 34s is ruler 4s).
const clips = [clip("clip_1", "a1", 30, 40, 0)];
const suggestions = buildAutoZoomSuggestionsForClips({
- cursorTelemetry: [{ timeMs: 5000, cx: 0.1, cy: 0.9 }, click(34000, 0.5, 0.5)],
+ cursorTelemetry: [
+ { timeMs: 5000, cx: 0.1, cy: 0.9 },
+ click(34000, 0.5, 0.5),
+ { timeMs: 60000, cx: 0.1, cy: 0.9 },
+ ],
assetId: "a1",
clips,
existingRegions: [],
defaultDurationMs: 2000,
});
- expect(suggestions.map((s) => s.span)).toEqual([{ start: 3000, end: 5000 }]);
+ expect(suggestions.map((s) => s.span)).toEqual([{ start: 3424, end: 5424 }]);
expect(suggestions[0].focus).toEqual({ cx: 0.5, cy: 0.5 });
});
+
+ // The video opens and closes on the whole screen. At depth 3 a ramp lasts 923 ms, so a
+ // zoom is fully in at 2500 + 923 → 3424 ms at the earliest, and on a 10 s edit it must be
+ // fully in by 10000 − 1000 − 923 → 8076 ms at the latest, to be back out a second early.
+ describe("wide opening and ending", () => {
+ const restOfTake = { timeMs: 30000, cx: 0.5, cy: 0.5 };
+
+ it("keeps the first seconds of the edit wide: an early click gets no zoom", () => {
+ const suggestions = buildAutoZoomSuggestionsForClips({
+ cursorTelemetry: [click(1000, 0.3, 0.3), restOfTake],
+ assetId: "a1",
+ clips: [clip("clip_1", "a1", 0, 10, 0)],
+ existingRegions: [],
+ defaultDurationMs: 2000,
+ });
+ expect(suggestions).toEqual([]);
+ });
+
+ it("holds a zoom back until the opening is over when it still covers its click", () => {
+ const suggestions = buildAutoZoomSuggestionsForClips({
+ cursorTelemetry: [click(4000, 0.3, 0.3), restOfTake],
+ assetId: "a1",
+ clips: [clip("clip_1", "a1", 0, 10, 0)],
+ existingRegions: [],
+ defaultDurationMs: 2000,
+ });
+ expect(suggestions.map((s) => s.span)).toEqual([{ start: 3424, end: 5424 }]);
+ });
+
+ it("brings the last zoom back out a second before the end of the edit", () => {
+ // The recording goes on to 30 s, so this is the edit's end at work, not the stop click.
+ const zoomed = buildAutoZoomSuggestionsForClips({
+ cursorTelemetry: [click(7500, 0.3, 0.3), restOfTake],
+ assetId: "a1",
+ clips: [clip("clip_1", "a1", 0, 10, 0)],
+ existingRegions: [],
+ defaultDurationMs: 2000,
+ });
+ expect(zoomed.map((s) => s.span)).toEqual([{ start: 6076, end: 8076 }]);
+ const tooLate = buildAutoZoomSuggestionsForClips({
+ cursorTelemetry: [click(8500, 0.3, 0.3), restOfTake],
+ assetId: "a1",
+ clips: [clip("clip_1", "a1", 0, 10, 0)],
+ existingRegions: [],
+ defaultDurationMs: 2000,
+ });
+ expect(tooLate).toEqual([]);
+ });
+
+ it("lets a clip after a cut open on a zoom: the bounds belong to the edit", () => {
+ // One recording laid down twice: the click at source 1 s is inside the opening
+ // on the first clip, and at ruler 11 s, clear of it, on the second.
+ const suggestions = buildAutoZoomSuggestionsForClips({
+ cursorTelemetry: [click(1000, 0.3, 0.3), { timeMs: 9500, cx: 0.5, cy: 0.5 }],
+ assetId: "a1",
+ clips: [clip("clip_1", "a1", 0, 10, 0), clip("clip_2", "a1", 0, 10, 10)],
+ existingRegions: [],
+ defaultDurationMs: 2000,
+ });
+ expect(suggestions.map((s) => s.span)).toEqual([{ start: 10000, end: 12000 }]);
+ });
+
+ it("ignores the click that stopped the recording", () => {
+ // The take's last click, 100 ms before its telemetry ends: the HUD's Stop button.
+ // Another clip follows on the ruler, so the edit's own ending does not reach it.
+ const suggestions = buildAutoZoomSuggestionsForClips({
+ cursorTelemetry: [
+ click(6000, 0.3, 0.3),
+ click(19800, 0.5, 0.92),
+ { timeMs: 19900, cx: 0.5, cy: 0.92 },
+ ],
+ assetId: "a1",
+ clips: [clip("clip_1", "a1", 0, 20, 0), clip("clip_2", "a2", 0, 20, 20)],
+ existingRegions: [],
+ defaultDurationMs: 2000,
+ });
+ expect(suggestions.map((s) => s.focus)).toEqual([{ cx: 0.3, cy: 0.3 }]);
+ });
+ });
});
diff --git a/src/lib/ai-edition/timeline/zoom-suggestions.ts b/src/lib/ai-edition/timeline/zoom-suggestions.ts
index 3e8418015..312308337 100644
--- a/src/lib/ai-edition/timeline/zoom-suggestions.ts
+++ b/src/lib/ai-edition/timeline/zoom-suggestions.ts
@@ -8,13 +8,25 @@
// and so do recorded clicks, focused on the click itself (issue #699).
import type { CursorTelemetryPoint, ZoomFocus } from "@/components/video-editor/types";
+import { zoomTransitionMs } from "@/lib/zoomMath/constants";
import type { AxcutClip } from "../schema";
+import { DEFAULT_ZOOM_DEPTH, ZOOM_DEPTH_SCALES } from "./zoom-scale";
export const MIN_DWELL_DURATION_MS = 450;
export const MAX_DWELL_DURATION_MS = 2600;
export const DWELL_MOVE_THRESHOLD = 0.02;
/** Minimum spacing between two accepted suggestion centres. */
export const SUGGESTION_SPACING_MS = 1800;
+/** The video opens on the whole screen: no zoom starts moving in before this. */
+export const OPENING_WIDE_MS = 2500;
+/** And it ends on it: the last zoom is fully back out this long before the end. */
+export const CLOSING_WIDE_MS = 1000;
+/**
+ * A click this close to the end of the recording is the one that stopped it, on the HUD's
+ * Stop button, which the video never shows. Any other click there has a result the video
+ * never shows either.
+ */
+export const STOP_CLICK_GUARD_MS = 1000;
export interface ZoomDwellCandidate {
centerTimeMs: number;
@@ -180,12 +192,21 @@ export interface AutoZoomSuggestion {
* region rejects leaves its nearby dwells standing as the fallback. One recorded
* click still yields ONE zoom anchored on the click's own time and position
* (issue #699). Pure, shared by the magic-wand toggle and the on-load auto-suggest pass.
+ *
+ * `wideUntilMs` and `wideFromMs` keep the ends of the video on the whole screen: a zoom
+ * starts moving in no earlier than the first and is back out by the second, ramps
+ * included. A zoom those bounds push off its own moment is dropped rather than shown
+ * after the fact. `ignoreClicksFromMs` drops the click that stopped the recording.
+ * All three are on the telemetry's axis, and default to no bound.
*/
export function buildAutoZoomSuggestions(options: {
cursorTelemetry: CursorTelemetryPoint[];
totalMs: number;
existingRegions: { startMs: number; endMs: number }[];
defaultDurationMs: number;
+ wideUntilMs?: number;
+ wideFromMs?: number;
+ ignoreClicksFromMs?: number;
}): AutoZoomSuggestion[] {
const { cursorTelemetry, totalMs, existingRegions, defaultDurationMs } = options;
if (totalMs <= 0 || cursorTelemetry.length === 0) {
@@ -197,13 +218,24 @@ export function buildAutoZoomSuggestions(options: {
return [];
}
+ // The ramps play outside the region, so the region itself keeps one ramp clear of each bound.
+ const ramp = zoomTransitionMs(ZOOM_DEPTH_SCALES[DEFAULT_ZOOM_DEPTH]);
+ const earliestStart = Math.max(0, Math.ceil((options.wideUntilMs ?? -Infinity) + ramp));
+ const latestEnd = Math.min(totalMs, Math.floor((options.wideFromMs ?? Infinity) - ramp));
+ if (latestEnd - earliestStart < defaultDuration) {
+ return [];
+ }
+
const normalizedSamples = normalizeCursorTelemetry(cursorTelemetry, totalMs);
if (normalizedSamples.length === 0) {
return [];
}
+ const ignoreClicksFromMs = options.ignoreClicksFromMs ?? Infinity;
const dwellCandidates = detectZoomDwellCandidates(normalizedSamples);
- const clickCandidates = detectZoomClickCandidates(normalizedSamples);
+ const clickCandidates = detectZoomClickCandidates(normalizedSamples).filter(
+ (candidate) => candidate.centerTimeMs < ignoreClicksFromMs,
+ );
if (dwellCandidates.length === 0 && clickCandidates.length === 0) {
return [];
}
@@ -233,8 +265,14 @@ export function buildAutoZoomSuggestions(options: {
}
const centeredStart = Math.round(candidate.centerTimeMs - defaultDuration / 2);
- const candidateStart = Math.max(0, Math.min(centeredStart, totalMs - defaultDuration));
+ const candidateStart = Math.max(
+ earliestStart,
+ Math.min(centeredStart, latestEnd - defaultDuration),
+ );
const candidateEnd = candidateStart + defaultDuration;
+ if (candidate.centerTimeMs < candidateStart || candidate.centerTimeMs > candidateEnd) {
+ continue;
+ }
const hasOverlap = reservedSpans.some(
(span) => candidateEnd > span.start && candidateStart < span.end,
);
@@ -281,6 +319,11 @@ export function buildAutoZoomSuggestions(options: {
* Clips of other assets are skipped, as are clips with no probed source window.
* `buildAutoZoomSuggestions` is reused verbatim per clip — spacing, ranking and the
* reserve rule keep their single definition.
+ *
+ * The wide opening and ending belong to the whole edit, not to each clip: a cut may land
+ * straight on a zoom, but the video's first and last seconds show the whole screen. The
+ * stop click belongs to the recording: its last second, read off the telemetry, which runs
+ * until the take stops.
*/
export function buildAutoZoomSuggestionsForClips(options: {
/** Samples in the asset's own SOURCE time. */
@@ -292,6 +335,12 @@ export function buildAutoZoomSuggestionsForClips(options: {
defaultDurationMs: number;
}): AutoZoomSuggestion[] {
const { cursorTelemetry, assetId, clips, existingRegions, defaultDurationMs } = options;
+ const editStartMs = Math.min(...clips.map((clip) => clip.timelineStartSec)) * 1000;
+ const editEndMs = Math.max(...clips.map((clip) => clip.timelineEndSec)) * 1000;
+ let recordingEndMs = 0;
+ for (const sample of cursorTelemetry) {
+ if (Number.isFinite(sample.timeMs)) recordingEndMs = Math.max(recordingEndMs, sample.timeMs);
+ }
const suggestions: AutoZoomSuggestion[] = [];
for (const clip of clips) {
if (clip.assetId !== assetId) continue;
@@ -313,6 +362,9 @@ export function buildAutoZoomSuggestionsForClips(options: {
endMs: region.endMs - timelineOffsetMs,
})),
defaultDurationMs,
+ wideUntilMs: editStartMs + OPENING_WIDE_MS - timelineOffsetMs,
+ wideFromMs: editEndMs - CLOSING_WIDE_MS - timelineOffsetMs,
+ ignoreClicksFromMs: recordingEndMs - STOP_CLICK_GUARD_MS - sourceOffsetMs,
});
suggestions.push(
...clipSuggestions.map((suggestion) => ({
diff --git a/src/lib/zoomMath/constants.ts b/src/lib/zoomMath/constants.ts
index f45e4fd08..1be35b4af 100644
--- a/src/lib/zoomMath/constants.ts
+++ b/src/lib/zoomMath/constants.ts
@@ -6,6 +6,10 @@ export const TRANSITION_WINDOW_MS = 1015.05;
// default 1.8×, ~1.5 s at 5×. Mirror of `zoom_transition_s` (crates/compositor/src/regions.rs).
export const ZOOM_TRANSITION_BASE_MS = 600;
export const ZOOM_TRANSITION_PER_LN_MS = 550;
+
+export function zoomTransitionMs(scale: number): number {
+ return ZOOM_TRANSITION_BASE_MS + ZOOM_TRANSITION_PER_LN_MS * Math.log(Math.max(1, scale));
+}
export const SMOOTHING_FACTOR = 0.12;
export const ZOOM_TRANSLATION_DEADZONE_PX = 1.25;
export const ZOOM_SCALE_DEADZONE = 0.002;
diff --git a/src/lib/zoomMath/zoomRegionUtils.ts b/src/lib/zoomMath/zoomRegionUtils.ts
index fd0fc9856..79de91088 100644
--- a/src/lib/zoomMath/zoomRegionUtils.ts
+++ b/src/lib/zoomMath/zoomRegionUtils.ts
@@ -12,7 +12,7 @@ import {
} from "@/components/video-editor/types";
import { type SpeedRegion, screenTimeMs } from "@/lib/ai-edition/timeline/speed";
import { clamp01 } from "@/utils/math";
-import { ZOOM_TRANSITION_BASE_MS, ZOOM_TRANSITION_PER_LN_MS } from "./constants";
+import { zoomTransitionMs } from "./constants";
import { interpolateCursorAt } from "./cursorFollowUtils";
import { clampFocusToScale } from "./focusUtils";
import { cubicBezier, easeSpring, scaleLerp } from "./mathUtils";
@@ -69,9 +69,7 @@ export function computeRegionStrength(
const start = toScreen(region.startMs);
const end = toScreen(region.endMs);
const t = toScreen(timeMs);
- const window =
- ZOOM_TRANSITION_BASE_MS +
- ZOOM_TRANSITION_PER_LN_MS * Math.log(Math.max(1, getZoomScale(region)));
+ const window = zoomTransitionMs(getZoomScale(region));
if (t < start - window || t > end + window) {
return 0;