Skills: Simple caption request does not lead to good markup
Author: JonnyBurgerCreated Sep 28, 2026Updated Sep 28, 2026
/remotion-best-practices please import this video and caption it /Users/jonathanburger/Downloads/whats1\ \(1\).mov
Leads to this markup:
import { Video } from "@remotion/media";
import { ALL_FORMATS, Input, UrlSource } from "mediabunny";
import {
AbsoluteFill,
CalculateMetadataFunction,
Composition,
staticFile,
} from "remotion";
import { Captions } from "./Captions";
type Props = {
videoSrc: string;
captionsSrc: string;
};
const FPS = 30;
const calculateMetadata: CalculateMetadataFunction<Props> = async ({
props,
}) => {
const input = new Input({
formats: ALL_FORMATS,
source: new UrlSource(staticFile(props.videoSrc), {
getRetryDelay: () => null,
}),
});
const durationInSeconds = await input.computeDuration();
const videoTrack = await input.getPrimaryVideoTrack();
return {
durationInFrames: Math.round(durationInSeconds * FPS),
width: videoTrack?.displayWidth ?? 1920,
height: videoTrack?.displayHeight ?? 1080,
};
};
export const MyComposition = () => {
return (
<Composition
id="MyComp"
component={MyComponent}
durationInFrames={180}
fps={FPS}
width={1920}
height={1080}
defaultProps={{
videoSrc: "video.mov",
captionsSrc: "captions.json",
}}
calculateMetadata={calculateMetadata}
/>
);
};
export const MyComponent: React.FC<Props> = ({ videoSrc, captionsSrc }) => {
return (
<AbsoluteFill style={{ backgroundColor: "black" }}>
<Video src={staticFile(videoSrc)} />
<Captions src={captionsSrc} />
</AbsoluteFill>
);
};import type { Caption, TikTokPage } from "@remotion/captions";
import { createTikTokStyleCaptions } from "@remotion/captions";
import { useCallback, useEffect, useMemo, useState } from "react";
import {
AbsoluteFill,
Sequence,
staticFile,
useCurrentFrame,
useDelayRender,
useVideoConfig,
} from "remotion";
// How often captions should switch (in milliseconds)
const SWITCH_CAPTIONS_EVERY_MS = 1200;
const HIGHLIGHT_COLOR = "#39E508";
const CaptionPage: React.FC<{ page: TikTokPage }> = ({ page }) => {
const frame = useCurrentFrame();
const { fps } = useVideoConfig();
const currentTimeMs = (frame / fps) * 1000;
const absoluteTimeMs = page.startMs + currentTimeMs;
return (
<AbsoluteFill
style={{
justifyContent: "flex-end",
alignItems: "center",
paddingBottom: 140,
}}
>
<div
style={{
fontFamily: "sans-serif",
fontSize: 80,
fontWeight: "bold",
whiteSpace: "pre",
textAlign: "center",
maxWidth: "80%",
textShadow: "0 6px 20px rgba(0, 0, 0, 0.8)",
WebkitTextStroke: "6px black",
paintOrder: "stroke fill",
}}
>
{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>
);
};
export const Captions: React.FC<{ src: string }> = ({ src }) => {
const { fps } = useVideoConfig();
const [captions, setCaptions] = useState<Caption[] | null>(null);
const { delayRender, continueRender, cancelRender } = useDelayRender();
const [handle] = useState(() => delayRender());
const fetchCaptions = useCallback(async () => {
try {
const response = await fetch(staticFile(src));
const data = await response.json();
setCaptions(data);
continueRender(handle);
} catch (e) {
cancelRender(e);
}
}, [src, continueRender, cancelRender, handle]);
useEffect(() => {
fetchCaptions();
}, [fetchCaptions]);
const { pages } = useMemo(() => {
if (!captions) {
return { pages: [] as TikTokPage[] };
}
return createTikTokStyleCaptions({
captions,
combineTokensWithinMilliseconds: SWITCH_CAPTIONS_EVERY_MS,
});
}, [captions]);
if (!captions) {
return null;
}
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>
);
};
Source: remotion-dev/remotion