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(schema): migrate 'native' using CROPPED dims, and unbreak the main build

Etienne Lescot2672971eaf72
diff --git a/src/components/ai-edition/WebcamOverlay.test.tsx b/src/components/ai-edition/WebcamOverlay.test.tsx
index 08e573f03..64386fab7 100644
--- a/src/components/ai-edition/WebcamOverlay.test.tsx
+++ b/src/components/ai-edition/WebcamOverlay.test.tsx
@@ -36,7 +36,7 @@ const CLIP_WITHOUT_CAMERA: AxcutClip = {
function makeDocument(): AxcutDocument {
return {
- schemaVersion: 5,
+ schemaVersion: 6,
project: {
id: "proj_test",
title: "Test",
diff --git a/src/lib/ai-edition/schema/index.test.ts b/src/lib/ai-edition/schema/index.test.ts
index 83a11ab6a..fdbd76ae2 100644
--- a/src/lib/ai-edition/schema/index.test.ts
+++ b/src/lib/ai-edition/schema/index.test.ts
@@ -617,26 +617,51 @@ describe("v5 -> v6 native AspectRatio migration", () => {
expect(doc.legacyEditor).toBeNull();
});
- it("is a no-op for non-v5 documents (the upgrader gates on schemaVersion === 5)", () => {
- // A v4 document is handled by the v4→v5 upgrader; v5→v6 must not touch it.
- const v4 = {
- schemaVersion: 4,
+ it("is idempotent — re-parsing an already-v6 document changes nothing", () => {
+ const once = documentSchema.parse(makeV5Doc({ legacyEditor: { aspectRatio: "16:9" } }));
+ const twice = documentSchema.parse(once);
+ expect(twice).toEqual(once);
+ });
+
+ it("bakes the CROPPED dimensions, not the raw ones", () => {
+ // "native" resolved to the cropped clip at runtime. A 3840x2160 asset cropped
+ // to its left half is effectively 1920x2160 → 8:9. Reading the raw dims would
+ // wrongly yield 16:9 and silently reframe the project.
+ const doc = documentSchema.parse({
+ schemaVersion: 5,
project: {
id: "p1",
- title: "v4",
+ title: "cropped",
createdAt: "2024-01-01T00:00:00.000Z",
updatedAt: "2024-01-01T00:00:00.000Z",
},
- assets: [],
- timeline: { clips: [] },
- };
- // v4 is rejected by the documentSchema (which expects 6 after the chain),
- // but the upgrader itself must not transform an input that isn't v5. We
- // round-trip through a fully-valid v5 doc instead: parse it, then verify the
- // second pass (which is a v6 doc) is unchanged.
- const once = documentSchema.parse(makeV5Doc({ legacyEditor: { aspectRatio: "16:9" } }));
- const twice = documentSchema.parse(once);
- expect(twice).toEqual(once);
- expect(v4.schemaVersion).toBe(4); // the test's input doc is untouched
+ assets: [
+ {
+ id: "asset_c",
+ kind: "video",
+ label: "A",
+ originalPath: "/a.mp4",
+ cameraTrack: null,
+ video: { width: 3840, height: 2160 },
+ },
+ ],
+ timeline: {
+ clips: [
+ {
+ id: "clip_a",
+ assetId: "asset_c",
+ sourceStartSec: 0,
+ sourceEndSec: 10,
+ timelineStartSec: 0,
+ timelineEndSec: 10,
+ origin: "user",
+ cropRegion: { x: 0, y: 0, width: 0.5, height: 1 },
+ },
+ ],
+ },
+ legacyEditor: { aspectRatio: "native" },
+ });
+ expect(doc.schemaVersion).toBe(6);
+ expect((doc.legacyEditor as Record<string, unknown>).aspectRatio).toBe("8:9");
});
});
diff --git a/src/lib/ai-edition/schema/index.ts b/src/lib/ai-edition/schema/index.ts
index 35d48b18f..177d0929a 100644
--- a/src/lib/ai-edition/schema/index.ts
+++ b/src/lib/ai-edition/schema/index.ts
@@ -14,7 +14,9 @@ import { z } from "zod";
// Cycle-safe: `aspectRatioUtils` has no schema dependency, and `document/outputFormat`
// is downstream of schema. We import the leaf tokeniser directly so the v5→v6 upgrader
// can rewrite `"native"` without dragging in the whole output-format module.
-import { toAspectRatioToken } from "@/utils/aspectRatioUtils";
+// Relative, not `@/`: the Electron main bundle imports this module and
+// vite-plugin-electron builds it without the root resolve.alias.
+import { toAspectRatioToken } from "../../../utils/aspectRatioUtils";
// Cycle-safe: `document/ids` only pulls `uuid`, and `timeline/timelineMap`'s
// transitive value-imports (region-ventilation, virtual-preview) import from this
// module TYPE-ONLY, so requiring them here never re-enters schema at runtime.
@@ -533,10 +535,15 @@ function upgradeV4DocumentToV5(raw: unknown): unknown {
* `referenceClipDims` pick from `document/outputFormat` — duplicated here so the schema
* package doesn't import from a module that itself imports from this one (cycle), and so
* the upgrader doesn't need the runtime `probedAssetDims` map (it runs at load time, before
- * any asset is probed). Crop is intentionally ignored: v5 docs store the crop on the
- * clip, but a v5→v6 rewrite only needs *some* reasonable concrete token, and the runtime
- * bridge in `resolveAspectRatioValue` already accepted this same approximation.
+ * any asset is probed). Crop IS applied: `"native"` resolved to the *cropped* clip
+ * dimensions at runtime (`clipEffectiveDims` → `calculateEffectiveSourceDimensions`),
+ * so ignoring it here would silently reframe every cropped project.
*/
+/** Mirrors `atLeastEven` in mp4ExportSettings — H.264 4:2:0 rejects odd dims. */
+function atLeastEven(value: number): number {
+ return Math.max(2, Math.floor(value / 2) * 2);
+}
+
function largestClipDims(doc: Record<string, unknown>): { width: number; height: number } | null {
const timeline = (doc.timeline ?? {}) as Record<string, unknown>;
const clips = Array.isArray(timeline.clips) ? timeline.clips : [];
@@ -557,9 +564,19 @@ function largestClipDims(doc: Record<string, unknown>): { width: number; height:
const asset = assetById.get(assetId);
if (!asset) continue;
const video = (asset.video ?? {}) as Record<string, unknown>;
- const w = typeof video.width === "number" ? video.width : 0;
- const h = typeof video.height === "number" ? video.height : 0;
- if (w <= 0 || h <= 0) continue;
+ const rawW = typeof video.width === "number" ? video.width : 0;
+ const rawH = typeof video.height === "number" ? video.height : 0;
+ if (rawW <= 0 || rawH <= 0) continue;
+ // Apply the clip's crop, exactly as `calculateEffectiveSourceDimensions`
+ // (mp4ExportSettings) does at runtime — including the snap to even pixels,
+ // so the reduced token matches the size the encoder is actually handed.
+ // "native" meant "the cropped source's shape"; ignoring crop here would
+ // bake the wrong ratio into every cropped project.
+ const crop = (c.cropRegion ?? {}) as Record<string, unknown>;
+ const cropW = typeof crop.width === "number" && crop.width > 0 ? crop.width : 1;
+ const cropH = typeof crop.height === "number" && crop.height > 0 ? crop.height : 1;
+ const w = atLeastEven(Math.round(rawW * cropW));
+ const h = atLeastEven(Math.round(rawH * cropH));
const area = w * h;
if (area > bestArea) {
bestArea = area;
diff --git a/src/native/sceneDescription.test.ts b/src/native/sceneDescription.test.ts
index 7262356e0..7fbb49129 100644
--- a/src/native/sceneDescription.test.ts
+++ b/src/native/sceneDescription.test.ts
@@ -64,7 +64,7 @@ function makeDoc(
const createdAt = "2024-01-01T00:00:00.000Z";
const baseProject = { id: "p1", title: "Test", createdAt, updatedAt: createdAt };
return {
- schemaVersion: 5,
+ schemaVersion: 6,
project: {
...baseProject,
...(overrides.project ?? {}),