Skip to content

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

refactor(timeline): one auto-zoom collect/append path for both callers

超超超超超级喜欢你的达妮娅708135dcc540
diff --git a/src/lib/ai-edition/document/applyProbedDuration.test.ts b/src/lib/ai-edition/document/applyProbedDuration.test.ts
index 0147f5f69..64081a666 100644
--- a/src/lib/ai-edition/document/applyProbedDuration.test.ts
+++ b/src/lib/ai-edition/document/applyProbedDuration.test.ts
@@ -89,6 +89,30 @@ describe("applyProbedDuration — the v1.7 import gap", () => {
expect(next.timeline.clips[0].timelineEndSec).toBe(12);
});
+ it("overwrites a leftover 60s fallback on the asset when the real duration arrives", () => {
+ const doc = documentSchema.parse(migrateProjectDataToAxcutDocument(legacyProjectWithZoom()));
+ const assetId = doc.assets[0].id;
+ const seeded: AxcutDocument = {
+ ...doc,
+ assets: doc.assets.map((asset) =>
+ asset.id === assetId ? { ...asset, durationSec: PLACEHOLDER_DURATION_SEC } : asset,
+ ),
+ timeline: {
+ ...doc.timeline,
+ clips: [
+ {
+ ...doc.timeline.clips[0],
+ sourceEndSec: PLACEHOLDER_DURATION_SEC,
+ timelineEndSec: PLACEHOLDER_DURATION_SEC,
+ },
+ ],
+ },
+ };
+ const next = applyProbedDuration(seeded, assetId, 90);
+ expect(next.timeline.clips[0].sourceEndSec).toBe(90);
+ expect(next.assets[0].durationSec).toBe(90);
+ });
+
it("leaves a clip the user has already trimmed alone", () => {
const doc = documentSchema.parse(migrateProjectDataToAxcutDocument(legacyProjectWithZoom()));
const assetId = doc.assets[0].id;
diff --git a/src/lib/ai-edition/document/timeline.ts b/src/lib/ai-edition/document/timeline.ts
index 35a8b42bb..4754f4561 100644
--- a/src/lib/ai-edition/document/timeline.ts
+++ b/src/lib/ai-edition/document/timeline.ts
@@ -539,9 +539,7 @@ export function applyProbedDuration(
const withClips: AxcutDocument = {
...document,
assets: document.assets.map((asset) =>
- asset.id === assetId && asset.durationSec == null
- ? { ...asset, durationSec: durationSec }
- : asset,
+ asset.id === assetId ? { ...asset, durationSec: durationSec } : asset,
),
timeline: { ...document.timeline, clips: nextClips },
};
diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts
index a16605f32..5241bb591 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -28,6 +28,7 @@ import {
withClipsChanged,
} from "../document/timeline";
import type { AxcutAudioTrack, AxcutClipCropRegion, AxcutDocument } from "../schema";
+import { appendAutoZoomSuggestions } from "../timeline/apply-auto-zooms";
import { hasAnyClipWithCamera } from "../timeline/camera";
import { probeAudioDuration, probeVideoDimensions, probeVideoDuration } from "../timeline/duration";
import {
@@ -350,29 +351,11 @@ export function useTimeline() {
// after the switch, not across it -- which is the same reason
// `documentAfterProbedDuration` carries an `originatingProjectId`.
if (useProjectStore.getState().projectId !== projectId) return 0;
- const anchored = suggestions.flatMap((s) =>
- anchorRegionsWithDerivedMs(
- [
- {
- id: createId("zoom"),
- startMs: Math.round(s.span.start),
- endMs: Math.round(s.span.end),
- depth: 3 as const,
- focus: { cx: s.focus.cx, cy: s.focus.cy },
- focusMode: "auto" as const,
- },
- ],
- // Anchored against the SAME document the write is built from: anchoring
- // on the stale clips and saving the fresh document would place regions
- // against a timeline that no longer exists.
- doc.timeline.clips,
- () => createId("zoom"),
- ),
- );
- const next: AxcutDocument = {
- ...doc,
- zoomRanges: [...doc.zoomRanges, ...anchored] as AxcutDocument["zoomRanges"],
- };
+ // One append shared with the fresh-recording import path, so the wand and the
+ // import cannot drift apart. It anchors against the SAME document the write is
+ // built from: anchoring on stale clips and saving the fresh document would
+ // place the regions against a timeline that no longer exists.
+ const next = appendAutoZoomSuggestions(doc, suggestions);
if (!(await saveDocument(next, { history: true }))) return 0;
return suggestions.length;
},
diff --git a/src/lib/ai-edition/timeline/apply-auto-zooms.test.ts b/src/lib/ai-edition/timeline/apply-auto-zooms.test.ts
new file mode 100644
index 000000000..c00124f19
--- /dev/null
+++ b/src/lib/ai-edition/timeline/apply-auto-zooms.test.ts
@@ -0,0 +1,112 @@
+import { describe, expect, it } from "vitest";
+import type { CursorTelemetryPoint } from "@/components/video-editor/types";
+import { type AxcutDocument, createEmptyDocument } from "../schema";
+import {
+ appendAutoZoomSuggestions,
+ collectAutoZoomSuggestionsForDocument,
+} from "./apply-auto-zooms";
+
+function dwell(
+ centerMs: number,
+ cx: number,
+ cy: number,
+ count = 6,
+ spanMs = 900,
+): CursorTelemetryPoint[] {
+ const step = spanMs / (count - 1);
+ return Array.from({ length: count }, (_, i) => ({
+ timeMs: centerMs - spanMs / 2 + i * step,
+ cx,
+ cy,
+ }));
+}
+
+function documentWithClip(durationSec = 10): AxcutDocument {
+ const doc = createEmptyDocument({ projectId: "p1", title: "Recording" });
+ return {
+ ...doc,
+ assets: [
+ {
+ id: "asset_1",
+ kind: "video",
+ label: "rec.mp4",
+ originalPath: "C:\\recordings\\rec.mp4",
+ cameraTrack: null,
+ durationSec,
+ },
+ ],
+ project: { ...doc.project, primaryAssetId: "asset_1" },
+ timeline: {
+ ...doc.timeline,
+ clips: [
+ {
+ id: "clip_1",
+ assetId: "asset_1",
+ sourceStartSec: 0,
+ sourceEndSec: durationSec,
+ timelineStartSec: 0,
+ timelineEndSec: durationSec,
+ wordRefs: [],
+ origin: "system",
+ reason: "",
+ },
+ ],
+ },
+ };
+}
+
+describe("collectAutoZoomSuggestionsForDocument", () => {
+ it("builds suggestions from the asset's cursor sidecar", async () => {
+ const document = documentWithClip();
+ const suggestions = await collectAutoZoomSuggestionsForDocument(document, async () =>
+ dwell(4000, 0.4, 0.6),
+ );
+ expect(suggestions).toHaveLength(1);
+ expect(suggestions[0].span.start).toBe(3000);
+ expect(suggestions[0].span.end).toBe(5000);
+ expect(suggestions[0].focus.cx).toBeCloseTo(0.4, 5);
+ });
+
+ it("asks for telemetry on the asset path, not a file URL", async () => {
+ const document = documentWithClip();
+ const paths: string[] = [];
+ await collectAutoZoomSuggestionsForDocument(document, async (videoPath) => {
+ paths.push(videoPath);
+ return [];
+ });
+ expect(paths).toEqual(["C:\\recordings\\rec.mp4"]);
+ });
+
+ it("returns nothing without a clip window", async () => {
+ const document = createEmptyDocument({ projectId: "p1", title: "Empty" });
+ const suggestions = await collectAutoZoomSuggestionsForDocument(document, async () =>
+ dwell(1000, 0.5, 0.5),
+ );
+ expect(suggestions).toEqual([]);
+ });
+});
+
+describe("appendAutoZoomSuggestions", () => {
+ it("leaves the document unchanged when there is nothing to add", () => {
+ const document = documentWithClip();
+ expect(appendAutoZoomSuggestions(document, [])).toBe(document);
+ });
+
+ it("anchors each suggestion onto the clip", () => {
+ const document = documentWithClip();
+ const next = appendAutoZoomSuggestions(
+ document,
+ [{ span: { start: 3000, end: 5000 }, focus: { cx: 0.4, cy: 0.6 } }],
+ (prefix) => `${prefix}_fixed`,
+ );
+ expect(next).not.toBe(document);
+ expect(next.zoomRanges).toHaveLength(1);
+ expect(next.zoomRanges[0]).toMatchObject({
+ startMs: 3000,
+ endMs: 5000,
+ depth: 3,
+ focusMode: "auto",
+ clipId: "clip_1",
+ });
+ });
+});
diff --git a/src/lib/ai-edition/timeline/apply-auto-zooms.ts b/src/lib/ai-edition/timeline/apply-auto-zooms.ts
new file mode 100644
index 000000000..f1f1c82c0
--- /dev/null
+++ b/src/lib/ai-edition/timeline/apply-auto-zooms.ts
@@ -0,0 +1,68 @@
+// Shared auto-zoom apply path: the HUD toggle / fresh-recording import and the
+// timeline wand both end here. Suggestion math stays in zoom-suggestions.ts;
+// this file only collects per-asset telemetry and appends the resulting regions.
+
+import type { CursorTelemetryPoint } from "@/components/video-editor/types";
+import { createId } from "../document/ids";
+import type { AxcutDocument } from "../schema";
+import { anchorRegionsWithDerivedMs } from "./timelineMap";
+import { type AutoZoomSuggestion, buildAutoZoomSuggestionsForClips } from "./zoom-suggestions";
+
+export const AUTO_ZOOM_DEFAULT_DURATION_MS = 2000;
+
+export async function collectAutoZoomSuggestionsForDocument(
+ document: AxcutDocument,
+ getTelemetry: (videoPath: string) => Promise<CursorTelemetryPoint[] | null | undefined>,
+): Promise<AutoZoomSuggestion[]> {
+ const existingRegions = document.zoomRanges.map((region) => ({
+ startMs: region.startMs,
+ endMs: region.endMs,
+ }));
+ const assetsWithClips = document.assets.filter(
+ (asset) =>
+ asset.kind === "video" &&
+ asset.originalPath &&
+ document.timeline.clips.some((clip) => clip.assetId === asset.id),
+ );
+ const perSource = await Promise.all(
+ assetsWithClips.map(async (asset) => {
+ const telemetry = (await getTelemetry(asset.originalPath)) ?? [];
+ return buildAutoZoomSuggestionsForClips({
+ cursorTelemetry: telemetry,
+ assetId: asset.id,
+ clips: document.timeline.clips,
+ existingRegions,
+ defaultDurationMs: AUTO_ZOOM_DEFAULT_DURATION_MS,
+ });
+ }),
+ );
+ return perSource.flat();
+}
+
+export function appendAutoZoomSuggestions(
+ document: AxcutDocument,
+ suggestions: AutoZoomSuggestion[],
+ makeId: (prefix: string) => string = createId,
+): AxcutDocument {
+ if (suggestions.length === 0) return document;
+ const anchored = suggestions.flatMap((suggestion) =>
+ anchorRegionsWithDerivedMs(
+ [
+ {
+ id: makeId("zoom"),
+ startMs: Math.round(suggestion.span.start),
+ endMs: Math.round(suggestion.span.end),
+ depth: 3 as const,
+ focus: { cx: suggestion.focus.cx, cy: suggestion.focus.cy },
+ focusMode: "auto" as const,
+ },
+ ],
+ document.timeline.clips,
+ () => makeId("zoom"),
+ ),
+ );
+ return {
+ ...document,
+ zoomRanges: [...document.zoomRanges, ...anchored] as AxcutDocument["zoomRanges"],
+ };
+}