Skip to content

Open source CapCut alternative.

TypeScript
17.7 KB
Raw Blame
import {
  contrastText as kitContrastText,
  textRoom,
  wrapTextToRoom,
  wordDrawsAt,
  wordFaceDraws,
  WORD_ACCENT_DEFAULT,
  wordEffect,
  wordSampleWindows,
  type OverlayWords,
  type TextWeight,
  type WordDraw,
  type WordFace,
} from "@donkeycut/effects-kit";
import { cueEmphasis } from "./captionEmphasis";
import { measureLine, textFontOf } from "./textFit";
import { formatTime } from "./time";
import type {
  CaptionStyleId,
  FontId,
  SubtitleCue,
  SubtitlesBlock,
  TextOverlay,
  WordEffectId,
} from "./types";

export type { CaptionStyleId } from "./types";

/** How many subtitle tracks (languages) the block carries: its track metas,
 * or any higher lane a cue still sits on; at least one. */
export function subtitleLaneCount(subs: SubtitlesBlock): number {
  let n = Math.max(1, subs.tracks?.length ?? 0);
  for (const c of subs.cues) n = Math.max(n, (c.lane ?? 0) + 1);
  return n;
}

/** One track's cues, in start order. */
export function laneCues(subs: SubtitlesBlock, lane: number): SubtitleCue[] {
  return subs.cues.filter((c) => (c.lane ?? 0) === lane);
}

/** Whether one subtitle track is hidden from the played/exported picture.
 * Preview and export both read this, so a hidden language never renders in
 * one and burns in the other. */
export function laneHidden(subs: SubtitlesBlock, lane: number): boolean {
  return subs.tracks?.[lane]?.hidden === true;
}

/** A track's language: its own meta, then the block-level legacy locale (which
 * described the first track only), then English. The panel's language select
 * and every generation path read this same chain, so the language shown is
 * always the language sent. */
export function trackLocale(subs: SubtitlesBlock, lane: number): string {
  return subs.tracks?.[lane]?.locale ?? (lane === 0 ? subs.locale : undefined) ?? "en-US";
}

/** The block-level word-effect overrides a caption track carries: which
 * effect, and the two settings an effect may have. Absent = the caption
 * style's own. */
export interface CaptionWordOverrides {
  accentMode?: WordEffectId;
  accentColor?: string;
  accentScale?: number;
  accentDim?: number;
}

/** The block-level emphasis style: how a caption's emphasized words are set
 * apart. Absent fields = CAPTION_EMPHASIS_DEFAULT's. */
export interface CaptionEmphasisOverrides {
  emphasisFont?: FontId;
  emphasisColor?: string;
  emphasisItalic?: boolean;
  emphasisWeight?: TextWeight;
  emphasisScale?: number;
}

/** A track's effective caption anchor plus the block's look overrides — what
 * `cueOverlay` reads to place and dress one cue. */
export interface CaptionPos extends CaptionWordOverrides, CaptionEmphasisOverrides {
  x: number;
  y: number;
  size?: number;
  font?: FontId;
}

/** One track's anchor, filled in: the track's own dragged spot wins, then the
 * block-level legacy spot (first track only), then the style's default
 * stacked upward per track so simultaneous languages never overlap. */
export function trackPos(
  subs: SubtitlesBlock,
  style: CaptionStyle,
  lane: number
): CaptionPos {
  const meta = subs.tracks?.[lane];
  return {
    x: meta?.x ?? (lane === 0 ? subs.x : undefined) ?? style.x,
    y: meta?.y ?? (lane === 0 ? subs.y : undefined) ?? Math.max(0.08, style.y - lane * 0.1),
    size: subs.size,
    font: subs.font,
    accentMode: subs.accentMode,
    accentColor: subs.accentColor,
    accentScale: subs.accentScale,
    accentDim: subs.accentDim,
    emphasisFont: subs.emphasisFont,
    emphasisColor: subs.emphasisColor,
    emphasisItalic: subs.emphasisItalic,
    emphasisWeight: subs.emphasisWeight,
    emphasisScale: subs.emphasisScale,
  };
}

/** Where one caption sits: its own anchor where it has one, else its track's
 * (see `trackPos`). Preview, drag and every burn-in read this. */
export function cueAnchor(
  cue: Pick<SubtitleCue, "x" | "y">,
  track: { x?: number; y?: number } | undefined,
  style: CaptionStyle
): { x: number; y: number } {
  return { x: cue.x ?? track?.x ?? style.x, y: cue.y ?? track?.y ?? style.y };
}

/** Where one caption sits in `subs`, its track and style filled in. */
export function cueSpot(
  subs: SubtitlesBlock,
  cue: Pick<SubtitleCue, "x" | "y" | "lane">
): { x: number; y: number } {
  const style = captionStyle(subs.style);
  return cueAnchor(cue, trackPos(subs, style, cue.lane ?? 0), style);
}

/** How emphasized caption words look when the track names nothing: a bold
 * italic serif in yellow, at the caption's own size. */
export const CAPTION_EMPHASIS_DEFAULT: Readonly<Required<WordFace>> = {
  font: "playfair",
  color: "#FFFF00",
  italic: true,
  weight: 700,
  scale: 1,
};

/** The bounds an emphasis scale is held to. */
export const EMPHASIS_SCALE_MIN = 0.8;
export const EMPHASIS_SCALE_MAX = 1.6;

/** A track's emphasis style with its defaults filled in. */
export function captionEmphasis(subs?: CaptionEmphasisOverrides): Required<WordFace> {
  return {
    font: subs?.emphasisFont ?? CAPTION_EMPHASIS_DEFAULT.font,
    color: subs?.emphasisColor ?? CAPTION_EMPHASIS_DEFAULT.color,
    italic: subs?.emphasisItalic ?? CAPTION_EMPHASIS_DEFAULT.italic,
    weight: subs?.emphasisWeight ?? CAPTION_EMPHASIS_DEFAULT.weight,
    scale: subs?.emphasisScale ?? CAPTION_EMPHASIS_DEFAULT.scale,
  };
}

/** Caption look shared by the preview layer and the export burn-in —
 * TikTok-style bold white on a translucent plate, low center. */
export const SUB_STYLE = {
  x: 0.5,
  y: 0.8,
  size: 42,
  font: "sf",
  weight: 700,
  color: "#FFFFFF",
  shadow: true,
  plate: true,
} as const;

/** A caption preset: the shared subtitle look plus optional opener emphasis
 * (a bigger, punchier first line). */
export interface CaptionStyle {
  id: CaptionStyleId;
  label: string;
  x: number;
  y: number;
  size: number;
  font: FontId;
  weight: TextWeight;
  color: string;
  shadow: boolean;
  plate: boolean;
  plateColor?: string;
  plateOpacity?: number;
  /** Multiply the opening cue's size, so the hook lands bigger. */
  openerScale?: number;
  /** Accent color for the word effect; absent = DEFAULT_ACCENT. */
  accent?: string;
  /** Which word effect the style's captions run: an accent box behind the
   * spoken word (plate styles), a line building itself up, and so on. Absent
   * = underline. */
  accentMode?: WordEffectId;
}

/** Spoken-word accent for styles that don't pick their own. */
export const DEFAULT_ACCENT = WORD_ACCENT_DEFAULT;

/** Black or white, whichever reads on the given hex fill. */
export const contrastText = kitContrastText;

/** The word effect a caption track runs, as the element model carries it:
 * user overrides on the subtitles block win over the caption style's
 * defaults, and an effect that says everything with size keeps the color the
 * rest of the caption is written in when nobody named an accent. */
export function captionWords(style: CaptionStyle, subs?: CaptionWordOverrides): OverlayWords {
  const id = subs?.accentMode ?? style.accentMode ?? "underline";
  const color =
    subs?.accentColor ??
    style.accent ??
    (wordEffect(id).accentDefault === "text" ? style.color : DEFAULT_ACCENT);
  return {
    style: id,
    color,
    ...(subs?.accentScale !== undefined ? { scale: subs.accentScale } : {}),
    ...(subs?.accentDim !== undefined ? { dim: subs.accentDim } : {}),
  };
}

export const CAPTION_STYLES: Record<CaptionStyleId, CaptionStyle> = {
  clean: {
    id: "clean",
    label: "Clean",
    x: 0.5,
    y: 0.8,
    size: 42,
    font: "sf",
    weight: 700,
    color: "#FFFFFF",
    shadow: true,
    plate: true,
    accentMode: "box",
  },
  hook: {
    id: "hook",
    label: "Curiosity hook",
    x: 0.5,
    y: 0.78,
    size: 46,
    font: "rounded",
    weight: 700,
    color: "#FFFFFF",
    shadow: true,
    plate: true,
    plateColor: "#0A84FF",
    plateOpacity: 0.9,
    openerScale: 1.28,
    accentMode: "box",
  },
  punchy: {
    id: "punchy",
    label: "Big & punchy",
    x: 0.5,
    y: 0.74,
    size: 50,
    font: "impact",
    weight: 700,
    color: "#FFE94A",
    shadow: true,
    plate: false,
    openerScale: 1.34,
    accent: "#FFFFFF",
  },
  minimal: {
    id: "minimal",
    label: "Minimal",
    x: 0.5,
    y: 0.82,
    size: 36,
    font: "sf",
    weight: 400,
    color: "#FFFFFF",
    shadow: true,
    plate: false,
  },
  editorial: {
    id: "editorial",
    label: "Editorial",
    x: 0.5,
    y: 0.8,
    size: 42,
    font: "serif",
    weight: 700,
    color: "#FFFFFF",
    shadow: true,
    plate: false,
  },
  typewriter: {
    id: "typewriter",
    label: "Typewriter",
    x: 0.5,
    y: 0.8,
    size: 38,
    font: "mono",
    weight: 400,
    color: "#FFFFFF",
    shadow: false,
    plate: true,
    accentMode: "box",
  },
  block: {
    id: "block",
    label: "Block",
    x: 0.5,
    y: 0.8,
    size: 44,
    font: "sf",
    weight: 700,
    color: "#FFFFFF",
    shadow: false,
    plate: true,
    plateColor: "#111114",
    plateOpacity: 1,
    accentMode: "box",
  },
  highlight: {
    id: "highlight",
    label: "Highlighter",
    x: 0.5,
    y: 0.8,
    size: 42,
    font: "sf",
    weight: 700,
    color: "#111114",
    shadow: false,
    plate: true,
    plateColor: "#FFE94A",
    plateOpacity: 0.95,
    accent: "#FF375F",
    accentMode: "box",
  },
  bubble: {
    id: "bubble",
    label: "Bubblegum",
    x: 0.5,
    y: 0.8,
    size: 42,
    font: "rounded",
    weight: 700,
    color: "#FFFFFF",
    shadow: false,
    plate: true,
    plateColor: "#FF375F",
    plateOpacity: 0.92,
    accentMode: "box",
  },
  neon: {
    id: "neon",
    label: "Neon",
    x: 0.5,
    y: 0.78,
    size: 46,
    font: "impact",
    weight: 700,
    color: "#30D158",
    shadow: true,
    plate: false,
    openerScale: 1.2,
    accent: "#FFFFFF",
  },
};

/** The active caption preset for a subtitles block. */
export function captionStyle(style: CaptionStyleId | undefined): CaptionStyle {
  return CAPTION_STYLES[style ?? "clean"] ?? CAPTION_STYLES.clean;
}

/**
 * Wrap a caption into as many lines as it takes to keep every line inside the
 * frame width at font `size` in `font`. The export burn-in renders each
 * `\n`-separated line centered without wrapping, so this is what decides how
 * a cue sits in the frame.
 *
 * The line is measured on the face it will be drawn in. A character count has
 * to assume the widest glyph that face could hold, which broke short cues onto
 * two lines with most of the frame still empty beside them; a measured line
 * puts a cue on one line whenever one line fits. A cue that needs more than
 * one gets its words spread evenly across them.
 */
export function wrapCaptionForSize(
  text: string,
  size: number,
  frameW: number,
  font: FontId = "sf",
  weight: TextWeight = 700,
  x = 0.5
): string {
  const flat = text.trim().replace(/\s+/g, " ");
  if (!flat) return "";
  const css = textFontOf({ font, weight, italic: undefined }, size);
  return balanceCaption(flat, textRoom(x, frameW), (line) => measureLine(line, css, size));
}

/**
 * The fewest lines the caption fits in, evened out.
 *
 * Filling each line to the edge and letting the rest fall through leaves the
 * last line holding a word or two under a full one — the orphan that makes a
 * caption look broken. So the line count is settled first (fill greedily
 * against the real room), and then the same fill is run again at the
 * narrowest width that still needs no extra line, which spreads the words
 * across the lines it already committed to. Nothing here can widen a line
 * past the room: every candidate width is inside it.
 */
function balanceCaption(text: string, room: number, measure: (line: string) => number): string {
  const greedy = wrapTextToRoom(text, room, measure);
  const lines = greedy.split("\n").length;
  if (lines < 2 || !(room > 0)) return greedy;
  let lo = 0;
  let hi = room;
  for (let i = 0; i < 12; i++) {
    const mid = (lo + hi) / 2;
    if (wrapTextToRoom(text, mid, measure).split("\n").length <= lines) hi = mid;
    else lo = mid;
  }
  return wrapTextToRoom(text, hi, measure);
}

/** A cue as a synthetic overlay, so captions ride the title pipeline. The style
 * preset drives the look; a user font or font size overrides the preset's
 * (the opener emphasis still multiplies the size); a dragged caption position
 * (frame fractions) overrides the preset's spot; `at` resolves the track's
 * word effect at that moment on the timeline. */
export function cueOverlay(
  cue: SubtitleCue,
  style: CaptionStyle = CAPTION_STYLES.clean,
  isOpener = false,
  pos: Partial<CaptionPos> | undefined,
  at: number | undefined,
  frameW: number
): TextOverlay {
  const words = at !== undefined ? cueWords(cue, style, pos) : null;
  const size = Math.round(
    (pos?.size ?? style.size) * (isOpener && style.openerScale ? style.openerScale : 1)
  );
  const { x, y } = cueAnchor(cue, pos, style);
  const text = wrapCaptionForSize(
    cue.text,
    size,
    frameW,
    pos?.font ?? style.font,
    style.weight,
    x
  );
  return {
    id: `sub-${cue.id}`,
    text,
    start: cue.start,
    end: cue.end,
    x,
    y,
    size,
    font: pos?.font ?? style.font,
    weight: style.weight,
    color: style.color,
    shadow: style.shadow,
    plate: style.plate,
    ...(style.plateColor ? { plateColor: style.plateColor } : {}),
    ...(style.plateOpacity !== undefined ? { plateOpacity: style.plateOpacity } : {}),
    ...(words && at !== undefined
      ? {
          wordDraw: wordDrawsAt(
            text,
            words,
            style.color,
            Math.max(0, cue.end - cue.start),
            at - cue.start,
            cueFaces(cue, pos) ?? undefined
          ),
        }
      : cueFaces(cue, pos)
        ? { wordDraw: restingDraws(cue, text, style.color, pos) }
        : {}),
  };
}

/** One cue's emphasis, resolved: the face list its words wear and, for a
 * caption with no word effect running, the still picture of them. Both are
 * the same from frame to frame, so they are held per cue and handed back
 * until the cue or the style changes — the preview asks every frame. */
interface FaceEntry {
  face: Required<WordFace>;
  faces: (WordFace | undefined)[];
  restText?: string;
  restColor?: string;
  rest?: WordDraw[];
}
const faceCache = new WeakMap<SubtitleCue, FaceEntry>();

const sameFace = (a: Required<WordFace>, b: CaptionEmphasisOverrides | undefined) => {
  const c = CAPTION_EMPHASIS_DEFAULT;
  return (
    a.font === (b?.emphasisFont ?? c.font) &&
    a.color === (b?.emphasisColor ?? c.color) &&
    a.italic === (b?.emphasisItalic ?? c.italic) &&
    a.weight === (b?.emphasisWeight ?? c.weight) &&
    a.scale === (b?.emphasisScale ?? c.scale)
  );
};

function faceEntry(cue: SubtitleCue, pos: CaptionEmphasisOverrides | undefined): FaceEntry | null {
  if (!cue.emphasis?.length) return null;
  const held = faceCache.get(cue);
  if (held && sameFace(held.face, pos)) return held;
  const marked = cueEmphasis(cue);
  if (marked.length === 0) return null;
  const face = captionEmphasis(pos);
  const faces: (WordFace | undefined)[] = [];
  for (const i of marked) faces[i] = face;
  const entry: FaceEntry = { face, faces };
  faceCache.set(cue, entry);
  return entry;
}

/** Per display word: the emphasis face, or nothing. Null when no word in the
 * cue is emphasized. */
function cueFaces(
  cue: SubtitleCue,
  pos: CaptionEmphasisOverrides | undefined
): (WordFace | undefined)[] | null {
  return faceEntry(cue, pos)?.faces ?? null;
}

function restingDraws(
  cue: SubtitleCue,
  text: string,
  color: string,
  pos: CaptionEmphasisOverrides | undefined
): WordDraw[] {
  const entry = faceEntry(cue, pos)!;
  if (!entry.rest || entry.restText !== text || entry.restColor !== color) {
    entry.rest = wordFaceDraws(text, color, entry.faces);
    entry.restText = text;
    entry.restColor = color;
  }
  return entry.rest;
}

/** A caption's pictures are full frames, one per span, and a cut carries one
 * of them for every word of speech in it. A ramp is sampled at this rate, and
 * never at more than this many steps however long the ramp runs, which is
 * enough to read an arrival while holding a subtitled export to a few
 * pictures per word. */
const CAPTION_WORD_FPS = 12;
const CAPTION_WORD_SLICES = 2;

/**
 * One cue's word effect with the transcriber's own onsets on it. The clock is
 * the measured speech wherever there is any, so the effect walks the line at
 * the pace it is actually said; a cue with no timings shares its span by word
 * length instead. Everything that draws or samples a cue reads this one
 * function, so the picture a burn-in span stands for is resolved against the
 * same windows the span was cut from.
 */
export function cueWords(
  cue: SubtitleCue,
  style: CaptionStyle,
  subs?: CaptionWordOverrides
): OverlayWords {
  const times = cue.words?.map((w) => w.t0 - cue.start);
  return { ...captionWords(style, subs), ...(times ? { times } : {}) };
}

/**
 * The moments a cue has to be drawn at to carry its word effect: one span per
 * stretch the picture holds still. An effect that lands on the beat comes
 * back as one span per word, which is what the caption burn-in has always
 * written; one that ramps is cut finer inside a word's window.
 */
export function cueWordFrames(
  cue: SubtitleCue,
  style: CaptionStyle,
  subs?: CaptionWordOverrides
): { start: number; end: number }[] {
  const dur = Math.max(0, cue.end - cue.start);
  return wordSampleWindows(cue.text, cueWords(cue, style, subs), dur, CAPTION_WORD_FPS, CAPTION_WORD_SLICES).map((w) => ({
    start: cue.start + w.start,
    end: cue.start + w.end,
  }));
}

/** The cue under a given time, if any. */
export function cueAt(cues: SubtitleCue[], t: number): SubtitleCue | undefined {
  return cues.find((c) => t >= c.start && t < c.end);
}

/** Compact panel timestamp (m:ss.s) — the same convention as the transport
 * readout, so a cue and the playhead never show different times for one
 * instant, and a rounded 59.97s can't render as an invalid ":60". */
export const fmtCueTime = formatTime;