Production-ready animations, transitions, backgrounds, and scenes for Remotion
TypeScript ai-agentsclaude-codedemo-videomcpmcp-servermodel-context-protocolmotion-designproduct-demoreactremotionshadcnvideo
{
"type": "remotion-element",
"version": 1,
"element": {
"assets": [],
"dependencies": [
{
"name": "@remotion/captions",
"version": null
}
],
"dimensions": null,
"displayName": "Caption Speaker",
"initialProps": null,
"slug": "caption-speaker",
"sourceCode": "import { Fragment, forwardRef, useImperativeHandle, useLayoutEffect, useMemo, useRef, useState, type ComponentProps } from \"react\";\nimport { Easing, Interactive, Sequence, interpolate, useCurrentFrame, useVideoConfig, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\nimport { createTikTokStyleCaptions, type Caption, type TikTokPage } from \"@remotion/captions\";\n\ntype SpeakerCaption = Caption & { speaker?: string };\n\ninterface CaptionBaseProps {\n captions: Caption[];\n combineTokensWithinMilliseconds?: number;\n holdMs?: number;\n fontSize?: number;\n fontWeight?: number;\n color?: string;\n className?: string;\n}\n\nconst DEFAULT_HOLD_MS = 600;\nconst MIN_SILENCE_BREAK_MS = 300;\n\ninterface CaptionPageWindow {\n page: TikTokPage;\n index: number;\n speaker: string | undefined;\n startMs: number;\n speechEndMs: number;\n endMs: number;\n endsInSilence: boolean;\n}\n\ntype CaptionTokenStatus = \"upcoming\" | \"active\" | \"spoken\";\n\ninterface CaptionTokenState {\n text: string;\n spaceBefore: boolean;\n fromMs: number;\n toMs: number;\n progress: number;\n status: CaptionTokenStatus;\n}\n\ninterface CaptionPreviousPage {\n page: TikTokPage;\n pageIndex: number;\n tokens: CaptionTokenState[];\n}\n\ninterface CaptionsState {\n page: TikTokPage;\n pageIndex: number;\n speaker: string | undefined;\n timeMs: number;\n activeIndex: number;\n tokens: CaptionTokenState[];\n sinceStartMs: number;\n untilEndMs: number;\n holdRoomMs: number;\n endsInSilence: boolean;\n previous: CaptionPreviousPage | null;\n}\n\ninterface CaptionPagingOptions {\n combineTokensWithinMilliseconds: number;\n holdMs?: number;\n}\n\nconst SENTENCE_END = /[.!?…][\"')\\]]*$/;\n\nfunction markPageBreaks(captions: SpeakerCaption[]): Caption[] {\n return captions.map((caption, i) => {\n if (caption.pageBreakAfter !== undefined) return caption;\n const next = captions[i + 1];\n const speakerChanges =\n next !== undefined && next.speaker !== caption.speaker;\n return speakerChanges || SENTENCE_END.test(caption.text.trim())\n ? { ...caption, pageBreakAfter: true }\n : caption;\n });\n}\n\nfunction buildCaptionPages(\n captions: SpeakerCaption[],\n {\n combineTokensWithinMilliseconds,\n holdMs = DEFAULT_HOLD_MS,\n }: CaptionPagingOptions,\n): CaptionPageWindow[] {\n if (captions.length === 0) return [];\n const { pages } = createTikTokStyleCaptions({\n captions: markPageBreaks(captions),\n combineTokensWithinMilliseconds,\n breakOnSilenceAfterMilliseconds: Math.max(holdMs, MIN_SILENCE_BREAK_MS),\n });\n const speakers = new Map<number, string | undefined>();\n for (const caption of captions) {\n if (!speakers.has(caption.startMs)) {\n speakers.set(caption.startMs, caption.speaker);\n }\n }\n const spoken = pages.filter((page) => page.tokens.length > 0);\n return spoken.map((page, index) => {\n const last = page.tokens[page.tokens.length - 1];\n const speechEndMs = last ? last.toMs : page.startMs;\n const heldUntil = speechEndMs + Math.max(0, holdMs);\n const next = spoken[index + 1];\n const endsInSilence = !next || next.startMs > heldUntil;\n const endMs = endsInSilence\n ? heldUntil\n : Math.max(page.startMs, next.startMs);\n return {\n page,\n index,\n speaker: speakers.get(page.tokens[0].fromMs),\n startMs: page.startMs,\n speechEndMs,\n endMs,\n endsInSilence,\n };\n });\n}\n\nfunction findWindowIndex(windows: CaptionPageWindow[], timeMs: number): number {\n let lo = 0;\n let hi = windows.length - 1;\n let found = -1;\n while (lo <= hi) {\n const mid = (lo + hi) >> 1;\n if (windows[mid].startMs <= timeMs) {\n found = mid;\n lo = mid + 1;\n } else {\n hi = mid - 1;\n }\n }\n if (found === -1) return -1;\n return timeMs < windows[found].endMs ? found : -1;\n}\n\nfunction tokenProgress(fromMs: number, toMs: number, timeMs: number): number {\n if (timeMs < fromMs) return 0;\n if (toMs <= fromMs) return 1;\n return Math.min(1, (timeMs - fromMs) / (toMs - fromMs));\n}\n\nfunction getTokenStates(\n page: TikTokPage,\n timeMs: number,\n): { activeIndex: number; tokens: CaptionTokenState[] } {\n let activeIndex = -1;\n page.tokens.forEach((token, i) => {\n if (token.fromMs <= timeMs) activeIndex = i;\n });\n const tokens = page.tokens.map(\n (token, i): CaptionTokenState => ({\n text: token.text.trim(),\n spaceBefore: i > 0 && /^\\s/.test(token.text),\n fromMs: token.fromMs,\n toMs: token.toMs,\n progress: tokenProgress(token.fromMs, token.toMs, timeMs),\n status:\n i < activeIndex ? \"spoken\" : i === activeIndex ? \"active\" : \"upcoming\",\n }),\n );\n return { activeIndex, tokens };\n}\n\nfunction getCaptionsState(\n windows: CaptionPageWindow[],\n timeMs: number,\n): CaptionsState | null {\n const found = findWindowIndex(windows, timeMs);\n if (found === -1) return null;\n const window = windows[found];\n const { activeIndex, tokens } = getTokenStates(window.page, timeMs);\n const before = found > 0 ? windows[found - 1] : null;\n const previous =\n before && !before.endsInSilence\n ? {\n page: before.page,\n pageIndex: before.index,\n tokens: getTokenStates(before.page, before.endMs).tokens,\n }\n : null;\n return {\n page: window.page,\n pageIndex: window.index,\n speaker: window.speaker,\n timeMs,\n activeIndex,\n tokens,\n sinceStartMs: timeMs - window.startMs,\n untilEndMs: window.endMs - timeMs,\n holdRoomMs: window.endMs - window.speechEndMs,\n endsInSilence: window.endsInSilence,\n previous,\n };\n}\n\nfunction getExitWindowMs(state: CaptionsState, exitMs: number): number {\n return Math.max(1, Math.min(exitMs, state.holdRoomMs));\n}\n\nfunction groupCaptionWords(\n tokens: CaptionTokenState[],\n): { index: number; token: CaptionTokenState }[][] {\n const words: { index: number; token: CaptionTokenState }[][] = [];\n tokens.forEach((token, index) => {\n const current = words[words.length - 1];\n if (!current || token.spaceBefore) {\n words.push([{ index, token }]);\n } else {\n current.push({ index, token });\n }\n });\n return words;\n}\n\nfunction normalizeCaptionWord(text: string): string {\n return text\n .toLowerCase()\n .replace(/[\\u2018\\u2019]/g, \"'\")\n .replace(/[^\\p{L}\\p{N}']/gu, \"\");\n}\n\nfunction createKeywordMatcher(\n keywords: readonly string[] = [],\n): (text: string) => boolean {\n const set = new Set(keywords.map(normalizeCaptionWord).filter(Boolean));\n return (text) => set.has(normalizeCaptionWord(text));\n}\n\nfunction captionWordText(word: { token: CaptionTokenState }[]): string {\n return word.map(({ token }) => token.text).join(\"\");\n}\n\ninterface CaptionWordRect {\n left: number;\n top: number;\n width: number;\n height: number;\n}\n\nfunction sameRects(a: CaptionWordRect[], b: CaptionWordRect[]): boolean {\n return (\n a.length === b.length &&\n a.every(\n (r, i) =>\n r.left === b[i].left &&\n r.top === b[i].top &&\n r.width === b[i].width &&\n r.height === b[i].height,\n )\n );\n}\n\nfunction useCaptionWordRects(\n pageIndex: number,\n count: number,\n): {\n wordRef: (index: number) => (el: HTMLElement | null) => void;\n rects: CaptionWordRect[] | null;\n} {\n const elements = useRef<(HTMLElement | null)[]>([]);\n const [measured, setMeasured] = useState<{\n pageIndex: number;\n rects: CaptionWordRect[];\n } | null>(null);\n\n useLayoutEffect(() => {\n if (pageIndex === -1) return;\n const rects = elements.current.slice(0, count).map((el) =>\n el\n ? {\n left: el.offsetLeft,\n top: el.offsetTop,\n width: el.offsetWidth,\n height: el.offsetHeight,\n }\n : { left: 0, top: 0, width: 0, height: 0 },\n );\n setMeasured((prev) =>\n prev && prev.pageIndex === pageIndex && sameRects(prev.rects, rects)\n ? prev\n : { pageIndex, rects },\n );\n });\n\n return {\n wordRef: (index) => (el) => {\n elements.current[index] = el;\n },\n rects: measured && measured.pageIndex === pageIndex ? measured.rects : null,\n };\n}\n\nfunction useCaptions({\n captions,\n combineTokensWithinMilliseconds,\n holdMs = DEFAULT_HOLD_MS,\n}: {\n captions: SpeakerCaption[];\n combineTokensWithinMilliseconds: number;\n holdMs?: number;\n}): CaptionsState | null {\n const frame = useCurrentFrame();\n const { fps } = useVideoConfig();\n const windows = useMemo(\n () =>\n buildCaptionPages(captions, { combineTokensWithinMilliseconds, holdMs }),\n [captions, combineTokensWithinMilliseconds, holdMs],\n );\n return getCaptionsState(windows, (frame / fps) * 1000);\n}\n\ninterface CaptionSpeakerProps extends CaptionBaseProps {\n captions: SpeakerCaption[];\n speakerColors?: Record<string, string>;\n upcomingOpacity?: number;\n shadow?: boolean;\n}\n\nconst SPEAKER_PALETTE = [\"#facc15\", \"#38bdf8\", \"#f472b6\", \"#4ade80\"];\n\nconst ENTER_MS = 160;\nconst EXIT_MS = 220;\nconst RISE_EM = 0.14;\nconst REVEAL_MS = 100;\nconst LEGIBILITY_SHADOW =\n \"0 2px 3px rgba(0, 0, 0, 0.5), 0 4px 16px rgba(0, 0, 0, 0.35)\";\n\nconst enterEasing = Easing.bezier(0.2, 0.8, 0.2, 1);\n\nconst clamp = {\n extrapolateLeft: \"clamp\",\n extrapolateRight: \"clamp\",\n} as const;\n\nfunction CaptionSpeakerBase({\n captions,\n combineTokensWithinMilliseconds = 1200,\n holdMs = DEFAULT_HOLD_MS,\n fontSize = 64,\n fontWeight = 800,\n color = \"#ffffff\",\n speakerColors,\n upcomingOpacity = 0.4,\n shadow = true,\n className,\n}: CaptionSpeakerProps) {\n const state = useCaptions({\n captions,\n combineTokensWithinMilliseconds,\n holdMs,\n });\n const autoColors = useMemo(() => {\n const pinned = new Set(Object.values(speakerColors ?? {}));\n const free = SPEAKER_PALETTE.filter((c) => !pinned.has(c));\n const palette = free.length > 0 ? free : SPEAKER_PALETTE;\n const assigned = new Map<string, string>();\n for (const caption of captions) {\n const { speaker } = caption;\n if (\n speaker === undefined ||\n speakerColors?.[speaker] !== undefined ||\n assigned.has(speaker)\n ) {\n continue;\n }\n assigned.set(speaker, palette[assigned.size % palette.length]);\n }\n return assigned;\n }, [captions, speakerColors]);\n if (!state) return null;\n\n const enter = interpolate(\n state.previous ? ENTER_MS : state.sinceStartMs,\n [0, ENTER_MS],\n [0, 1],\n {\n ...clamp,\n easing: enterEasing,\n },\n );\n const exit = state.endsInSilence\n ? interpolate(\n state.untilEndMs,\n [0, getExitWindowMs(state, EXIT_MS)],\n [0, 1],\n clamp,\n )\n : 1;\n\n const { speaker } = state;\n const speakerColor =\n speaker === undefined\n ? color\n : (speakerColors?.[speaker] ?? autoColors.get(speaker) ?? color);\n\n return (\n <div\n className={className}\n style={{\n fontSize,\n fontWeight,\n color: speakerColor,\n lineHeight: 1.2,\n textAlign: \"center\",\n opacity: Math.min(enter, exit),\n transform: `translateY(${(1 - enter) * RISE_EM}em)`,\n }}\n >\n {groupCaptionWords(state.tokens).map((word, w) => (\n <Fragment key={`${state.pageIndex}-${word[0].index}`}>\n {w > 0 && \" \"}\n <span\n style={{\n display: \"inline-block\",\n whiteSpace: \"nowrap\",\n textShadow: shadow ? LEGIBILITY_SHADOW : undefined,\n }}\n >\n {word.map(({ index, token }) => (\n <span\n key={index}\n style={{\n opacity: interpolate(\n state.timeMs - token.fromMs,\n [0, REVEAL_MS],\n [upcomingOpacity, 1],\n clamp,\n ),\n }}\n >\n {token.text}\n </span>\n ))}\n </span>\n </Fragment>\n ))}\n </div>\n );\n}\n\nconst elementSchema = {\n ...Interactive.baseSchema,\n captions: {\n type: \"remotion-captions\",\n default: [{\n text: \" So\",\n startMs: 300,\n endMs: 460,\n timestampMs: 380,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" what\",\n startMs: 460,\n endMs: 640,\n timestampMs: 550,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" made\",\n startMs: 640,\n endMs: 860,\n timestampMs: 750,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" you\",\n startMs: 860,\n endMs: 980,\n timestampMs: 920,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" add\",\n startMs: 980,\n endMs: 1180,\n timestampMs: 1080,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" captions?\",\n startMs: 1180,\n endMs: 1800,\n timestampMs: 1490,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" Most\",\n startMs: 2300,\n endMs: 2560,\n timestampMs: 2430,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" people\",\n startMs: 2560,\n endMs: 2900,\n timestampMs: 2730,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" watch\",\n startMs: 2900,\n endMs: 3160,\n timestampMs: 3030,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" with\",\n startMs: 3160,\n endMs: 3280,\n timestampMs: 3220,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" the\",\n startMs: 3280,\n endMs: 3380,\n timestampMs: 3330,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" sound\",\n startMs: 3380,\n endMs: 3720,\n timestampMs: 3550,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" off.\",\n startMs: 3720,\n endMs: 4240,\n timestampMs: 3980,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" Really?\",\n startMs: 4800,\n endMs: 5400,\n timestampMs: 5100,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" Almost\",\n startMs: 5800,\n endMs: 6140,\n timestampMs: 5970,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" everyone.\",\n startMs: 6140,\n endMs: 6760,\n timestampMs: 6450,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" Captions\",\n startMs: 7000,\n endMs: 7480,\n timestampMs: 7240,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" keep\",\n startMs: 7480,\n endMs: 7700,\n timestampMs: 7590,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" them\",\n startMs: 7700,\n endMs: 7860,\n timestampMs: 7780,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" watching.\",\n startMs: 7860,\n endMs: 8400,\n timestampMs: 8130,\n confidence: 0.96,\n speaker: \"guest\",\n }],\n description: \"Transcript\",\n keyframable: false,\n },\n fontSize: {\n type: \"number\",\n min: 24,\n max: 140,\n step: 1,\n default: 64,\n description: \"Font size\",\n hiddenFromList: false,\n },\n fontWeight: {\n type: \"number\",\n min: 400,\n max: 900,\n step: 100,\n default: 800,\n description: \"Font weight\",\n hiddenFromList: false,\n },\n upcomingOpacity: {\n type: \"number\",\n min: 0,\n max: 1,\n step: 0.05,\n default: 0.4,\n description: \"Upcoming opacity\",\n hiddenFromList: false,\n },\n combineTokensWithinMilliseconds: {\n type: \"number\",\n min: 0,\n max: 3000,\n step: 100,\n default: 1200,\n description: \"Page window (ms)\",\n hiddenFromList: false,\n },\n holdMs: {\n type: \"number\",\n min: 0,\n max: 2000,\n step: 50,\n default: 600,\n description: \"Hold (ms)\",\n hiddenFromList: false,\n },\n shadow: {\n type: \"boolean\",\n default: true,\n description: \"Shadow\",\n },\n ...Interactive.transformSchema,\n} as const satisfies InteractivitySchema;\n\nconst ELEMENT_PROP_KEYS = new Set([\"captions\",\"fontSize\",\"fontWeight\",\"upcomingOpacity\",\"combineTokensWithinMilliseconds\",\"holdMs\",\"shadow\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n captions: [{\n text: \" So\",\n startMs: 300,\n endMs: 460,\n timestampMs: 380,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" what\",\n startMs: 460,\n endMs: 640,\n timestampMs: 550,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" made\",\n startMs: 640,\n endMs: 860,\n timestampMs: 750,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" you\",\n startMs: 860,\n endMs: 980,\n timestampMs: 920,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" add\",\n startMs: 980,\n endMs: 1180,\n timestampMs: 1080,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" captions?\",\n startMs: 1180,\n endMs: 1800,\n timestampMs: 1490,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" Most\",\n startMs: 2300,\n endMs: 2560,\n timestampMs: 2430,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" people\",\n startMs: 2560,\n endMs: 2900,\n timestampMs: 2730,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" watch\",\n startMs: 2900,\n endMs: 3160,\n timestampMs: 3030,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" with\",\n startMs: 3160,\n endMs: 3280,\n timestampMs: 3220,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" the\",\n startMs: 3280,\n endMs: 3380,\n timestampMs: 3330,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" sound\",\n startMs: 3380,\n endMs: 3720,\n timestampMs: 3550,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" off.\",\n startMs: 3720,\n endMs: 4240,\n timestampMs: 3980,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" Really?\",\n startMs: 4800,\n endMs: 5400,\n timestampMs: 5100,\n confidence: 0.96,\n speaker: \"host\",\n }, {\n text: \" Almost\",\n startMs: 5800,\n endMs: 6140,\n timestampMs: 5970,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" everyone.\",\n startMs: 6140,\n endMs: 6760,\n timestampMs: 6450,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" Captions\",\n startMs: 7000,\n endMs: 7480,\n timestampMs: 7240,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" keep\",\n startMs: 7480,\n endMs: 7700,\n timestampMs: 7590,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" them\",\n startMs: 7700,\n endMs: 7860,\n timestampMs: 7780,\n confidence: 0.96,\n speaker: \"guest\",\n }, {\n text: \" watching.\",\n startMs: 7860,\n endMs: 8400,\n timestampMs: 8130,\n confidence: 0.96,\n speaker: \"guest\",\n }],\n fontSize: 64,\n fontWeight: 800,\n upcomingOpacity: 0.4,\n combineTokensWithinMilliseconds: 1200,\n holdMs: 600,\n shadow: true,\n};\n\ntype CaptionSpeakerElementProps = InteractiveBaseProps &\n InteractiveTransformProps &\n ComponentProps<typeof CaptionSpeakerBase>;\n\nconst CaptionSpeakerInner = forwardRef<\n HTMLDivElement,\n CaptionSpeakerElementProps & { readonly controls: SequenceControls | undefined }\n>(({ controls, name, style, ...rest }, ref) => {\n const outlineRef = useRef<HTMLDivElement>(null);\n useImperativeHandle(ref, () => outlineRef.current as HTMLDivElement, []);\n const componentProps: Record<string, unknown> = { ...ELEMENT_PROP_DEFAULTS };\n const sequenceProps: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(rest)) {\n if (ELEMENT_PROP_KEYS.has(key)) {\n if (value !== undefined) componentProps[key] = value;\n } else {\n sequenceProps[key] = value;\n }\n }\n return (\n <Sequence\n layout=\"none\"\n {...sequenceProps}\n controls={controls}\n name={name ?? \"<CaptionSpeaker>\"}\n outlineRef={outlineRef}\n >\n <div\n ref={outlineRef}\n style={{\n position: \"relative\",\n width: \"100%\",\n height: \"100%\",\n display: \"flex\",\n alignItems: \"center\",\n justifyContent: \"center\",\n ...style,\n }}\n >\n <CaptionSpeakerBase\n {...(componentProps as unknown as ComponentProps<typeof CaptionSpeakerBase>)}\n />\n </div>\n </Sequence>\n );\n});\n\nexport const CaptionSpeaker = Interactive.withSchema({\n Component: CaptionSpeakerInner,\n componentName: \"<CaptionSpeaker>\",\n componentIdentity: null,\n schema: elementSchema,\n supportsEffects: false,\n});\n",
"durationInFrames": 282,
"installationMode": "component-owned-sequence",
"isCaptionStyle": true
},
"durationInFrames": 282
}