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(cli): place captions in the editor's bottom-anchored caption box

EtienneLescoteea646e04e91
diff --git a/src/cli/captionAnnotations.test.ts b/src/cli/captionAnnotations.test.ts
index c595cebc1..79f336a69 100644
--- a/src/cli/captionAnnotations.test.ts
+++ b/src/cli/captionAnnotations.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import { DEFAULT_CAPTION_SETTINGS } from "@/lib/ai-edition/captions/settings";
import type { CaptionSegment } from "@/lib/captioning/transcribe";
-import { CAPTION_PLATE_HEIGHT, captionSegmentsToAnnotationRegions } from "./captionAnnotations";
+import { captionSegmentsToAnnotationRegions } from "./captionAnnotations";
const words = (...texts: string[]): CaptionSegment[] =>
texts.map((text, i) => ({ text, startSec: i * 0.5, endSec: i * 0.5 + 0.4 }));
@@ -42,13 +42,12 @@ describe("captionSegmentsToAnnotationRegions", () => {
color: DEFAULT_CAPTION_SETTINGS.color,
backgroundColor: "rgba(0, 0, 0, 0.55)",
});
- // Inside the 16:9 safe column.
+ // The editor's caption box: 16:9 safe column, bottom edge 1.5% off the frame's, room for
+ // a caption that wraps. The text is bottom-anchored in it (see sceneDescription).
expect(region.position.x).toBe(16);
expect(region.size.width).toBe(68);
- // The PLATE, centred in the region, ends 1.5% off the frame's bottom edge, like the
- // editor's inset. The region itself extends below it.
- const plateBottom = region.position.y + region.size.height / 2 + CAPTION_PLATE_HEIGHT / 2;
- expect(plateBottom).toBeCloseTo(98.5);
- expect(region.position.y + region.size.height).toBeGreaterThan(98.5);
+ expect(region.position.y + region.size.height).toBeCloseTo(98.5);
+ // Three 48 px lines of 1.5 em plus the plate padding: a wrapped caption is not clipped.
+ expect(region.size.height).toBeCloseTo(((48 * (3 * 1.5 + 0.2)) / 1080) * 100);
});
});
diff --git a/src/cli/captionAnnotations.ts b/src/cli/captionAnnotations.ts
index 651f48eca..91e3baa96 100644
--- a/src/cli/captionAnnotations.ts
+++ b/src/cli/captionAnnotations.ts
@@ -6,9 +6,8 @@
import type { AnnotationRegion, AnnotationTextStyle } from "@/components/video-editor/types";
import {
captionBackgroundCss,
- captionSafeColumn,
+ captionBoxRect,
DEFAULT_CAPTION_SETTINGS,
- defaultCaptionInsetY,
} from "@/lib/ai-edition/captions/settings";
import {
type CaptionSegmentLayoutOptions,
@@ -24,28 +23,15 @@ import type { CaptionSegment } from "@/lib/captioning/transcribe";
// used to write 24 px regular text on no plate, which read as faint over any bright frame.
// `.openscreen` projects carry no transcript, so the CLI cannot write the editor's transcript-
// driven captions; matching their appearance is what keeps the two from looking different.
-const LANDSCAPE = 16 / 9;
-const CAPTION_HEIGHT = 12;
-const CAPTION_COLUMN = captionSafeColumn(LANDSCAPE);
+//
+// Placed with the editor's own caption box: room for three lines, pinned by its BOTTOM edge
+// 1.5% off the frame's. The scene description bottom-anchors `auto-caption` text in it, so
+// the plate ends at the inset whether the caption holds one line or wraps to three.
+const CAPTION_BOX = captionBoxRect(DEFAULT_CAPTION_SETTINGS, 16 / 9);
-// The inset measures to the PLATE's bottom edge, as the editor's does, not to the region's.
-// An annotation centres its text block (and the plate that hugs it) in its region, so the
-// region is placed to put a one-line plate's bottom there. The region stays 12% tall for
-// headroom: a line that wraps grows its plate both ways instead of being clipped.
-const ANNOTATION_REFERENCE_HEIGHT = 1080;
-/** One line plus the plate's vertical padding (`text_plate.rs::PAD_Y_EM` twice), in em.
- * 1.3 em is DirectWrite's line box for bold Inter: a Windows export measured the plate at
- * 1.51 em. Other rasterizers differ slightly; cosmic-text (Linux) lays out 1.4 em. */
-const PLATE_HEIGHT_EM = 1.3 + 2 * 0.1;
-export const CAPTION_PLATE_HEIGHT =
- ((DEFAULT_CAPTION_SETTINGS.fontSize * PLATE_HEIGHT_EM) / ANNOTATION_REFERENCE_HEIGHT) * 100;
+const CAPTION_POSITION = { x: CAPTION_BOX.x, y: CAPTION_BOX.y };
-const CAPTION_POSITION = {
- x: CAPTION_COLUMN.x,
- y: 100 - defaultCaptionInsetY(LANDSCAPE) - (CAPTION_HEIGHT + CAPTION_PLATE_HEIGHT) / 2,
-};
-
-const CAPTION_SIZE = { width: CAPTION_COLUMN.width, height: CAPTION_HEIGHT };
+const CAPTION_SIZE = { width: CAPTION_BOX.width, height: CAPTION_BOX.height };
const CAPTION_STYLE: AnnotationTextStyle = {
color: DEFAULT_CAPTION_SETTINGS.color,
diff --git a/src/native/sceneDescription.test.ts b/src/native/sceneDescription.test.ts
index 15775b19e..2dcf9d7ba 100644
--- a/src/native/sceneDescription.test.ts
+++ b/src/native/sceneDescription.test.ts
@@ -1842,6 +1842,32 @@ describe("buildSceneDescription.annotations", () => {
});
});
+ it("bottom-anchors caption annotations and leaves every other annotation centred", () => {
+ // `openscreen captions` writes these into the editor's caption box; pinned by the bottom
+ // edge, a caption that wraps grows upward from the inset instead of off the frame.
+ const text = (id: string, extra: object) => ({
+ id,
+ startMs: 0,
+ endMs: 1000,
+ type: "text" as const,
+ content: id,
+ position: { x: 0, y: 0 },
+ size: { width: 10, height: 10 },
+ style,
+ zIndex: 0,
+ ...extra,
+ });
+ const scene = buildSceneDescription(
+ docWithAnnotations([
+ text("caption", { annotationSource: "auto-caption" }),
+ text("plain", {}),
+ ]),
+ );
+ const byId = Object.fromEntries(scene.annotations.map((a) => [a.id, a.text]));
+ expect(byId.caption?.verticalAlign).toBe("bottom");
+ expect(byId.plain).not.toHaveProperty("verticalAlign");
+ });
+
it("falls back to textContent when content is empty", () => {
// Le cas qui casse avec `??` : `content` vaut "" (ni null ni undefined), donc seul un
// `||` retombe sur l'autre champ.
diff --git a/src/native/sceneDescription.ts b/src/native/sceneDescription.ts
index 2c9cc6758..7a2d53f4b 100644
--- a/src/native/sceneDescription.ts
+++ b/src/native/sceneDescription.ts
@@ -1163,8 +1163,13 @@ export function buildSceneDescription(
// Only captions carry a space; annotations must keep emitting the exact same keys
// they always have, so the field is omitted rather than sent as null/undefined.
const space = (region as { space?: "frame" }).space;
- // Same treatment, same reason: only captions pin an edge.
- const verticalAlign = (region as { verticalAlign?: "top" | "bottom" }).verticalAlign;
+ // Same treatment, same reason: only captions pin an edge. That includes the
+ // caption annotations `openscreen captions` writes, whose box is the editor's
+ // caption box: bottom-anchored, a caption that wraps grows upward from the inset
+ // instead of past the frame's edge.
+ const verticalAlign =
+ (region as { verticalAlign?: "top" | "bottom" }).verticalAlign ??
+ (region.annotationSource === "auto-caption" ? "bottom" : undefined);
const base = {
id: region.id,
startSec: region.startMs / 1000,