🎥 Make videos programmatically with React
skill for displaying captions
Jonny Burgere304ea99ca64
diff --git a/packages/skills/skills/remotion/SKILL.md b/packages/skills/skills/remotion/SKILL.mdindex a00add14e0..a62028f946 100644--- a/packages/skills/skills/remotion/SKILL.md+++ b/packages/skills/skills/remotion/SKILL.md@@ -21,6 +21,7 @@ Read individual rule files for detailed explanations and code examples:- [rules/can-decode.md](rules/can-decode.md) - Check if a video can be decoded by the browser using Mediabunny- [rules/charts.md](rules/charts.md) - Chart and data visualization patterns for Remotion- [rules/compositions.md](rules/compositions.md) - Defining compositions, stills, folders, default props and dynamic metadata+- [rules/display-captions.md](rules/display-captions.md) - Displaying captions in Remotion with TikTok-style pages and word highlighting- [rules/extract-frames.md](rules/extract-frames.md) - Extract frames from videos at specific timestamps using Mediabunny- [rules/fonts.md](rules/fonts.md) - Loading Google Fonts and local fonts in Remotion- [rules/get-audio-duration.md](rules/get-audio-duration.md) - Getting the duration of an audio file in seconds with Mediabunnydiff --git a/packages/skills/skills/remotion/rules/display-captions.md b/packages/skills/skills/remotion/rules/display-captions.mdnew file mode 100644index 0000000000..1f70b702ae--- /dev/null+++ b/packages/skills/skills/remotion/rules/display-captions.md@@ -0,0 +1,126 @@+---+name: display-captions+description: Displaying captions in Remotion with TikTok-style pages and word highlighting+metadata:+ tags: captions, subtitles, display, tiktok, highlight+---++# Displaying captions in Remotion++This guide explains how to display captions in Remotion, assuming you already have captions in the `Caption` format.++## Prerequisites++First, the @remotion/captions package needs to be installed.+If it is not installed, use the following command:++```bash+npx remotion add @remotion/captions # If project uses npm+bunx remotion add @remotion/captions # If project uses bun+yarn remotion add @remotion/captions # If project uses yarn+pnpm exec remotion add @remotion/captions # If project uses pnpm+```++## Creating pages++Use `createTikTokStyleCaptions()` to group captions into pages. The `combineTokensWithinMilliseconds` option controls how many words appear at once:++```tsx+import {useMemo} from 'react';+import {createTikTokStyleCaptions} from '@remotion/captions';+import type {Caption} from '@remotion/captions';++// How often captions should switch (in milliseconds)+// Higher values = more words per page+// Lower values = fewer words (more word-by-word)+const SWITCH_CAPTIONS_EVERY_MS = 1200;++const {pages} = useMemo(() => {+ return createTikTokStyleCaptions({+ captions,+ combineTokensWithinMilliseconds: SWITCH_CAPTIONS_EVERY_MS,+ });+}, [captions]);+```++## Rendering with Sequences++Map over the pages and render each one in a `<Sequence>`. Calculate the start frame and duration from the page timing:++```tsx+import {Sequence, useVideoConfig, AbsoluteFill} from 'remotion';+import type {TikTokPage} from '@remotion/captions';++const CaptionedContent: React.FC = () => {+ const {fps} = useVideoConfig();++ return (+ <AbsoluteFill>+ {pages.map((page, index) => {+ const nextPage = pages[index + 1] ?? null;+ const startFrame = (page.startMs / 1000) * fps;+ const endFrame = Math.min(+ nextPage ? (nextPage.startMs / 1000) * fps : Infinity,+ startFrame + (SWITCH_CAPTIONS_EVERY_MS / 1000) * fps,+ );+ const durationInFrames = endFrame - startFrame;++ if (durationInFrames <= 0) {+ return null;+ }++ return (+ <Sequence+ key={index}+ from={startFrame}+ durationInFrames={durationInFrames}+ >+ <CaptionPage page={page} />+ </Sequence>+ );+ })}+ </AbsoluteFill>+ );+};+```++## Word highlighting++A caption page contains `tokens` which you can use to highlight the currently spoken word:++```tsx+import {AbsoluteFill, useCurrentFrame, useVideoConfig} from 'remotion';+import type {TikTokPage} from '@remotion/captions';++const HIGHLIGHT_COLOR = '#39E508';++const CaptionPage: React.FC<{page: TikTokPage}> = ({page}) => {+ const frame = useCurrentFrame();+ const {fps} = useVideoConfig();++ // Current time relative to the start of the sequence+ const currentTimeMs = (frame / fps) * 1000;+ // Convert to absolute time by adding the page start+ const absoluteTimeMs = page.startMs + currentTimeMs;++ return (+ <AbsoluteFill style={{justifyContent: 'center', alignItems: 'center'}}>+ <div style={{fontSize: 80, fontWeight: 'bold', whiteSpace: 'pre'}}>+ {page.tokens.map((token) => {+ const isActive =+ token.fromMs <= absoluteTimeMs && token.toMs > absoluteTimeMs;++ return (+ <span+ key={token.fromMs}+ style={{color: isActive ? HIGHLIGHT_COLOR : 'white'}}+ >+ {token.text}+ </span>+ );+ })}+ </div>+ </AbsoluteFill>+ );+};+```