Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply best practices for creating programmatic videos with Remotion and React
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/import-srt-captions.md
1---2name: import-srt-captions3description: Importing .srt subtitle files into Remotion using @remotion/captions4metadata:5tags: captions, subtitles, srt, import, parse6---78# Importing .srt subtitles into Remotion910If you have an existing `.srt` subtitle file, you can import it into Remotion using `parseSrt()` from `@remotion/captions`.1112If you don't have a .srt file, read [Transcribing audio](transcribe-captions.md) for how to generate captions instead.1314## Prerequisites1516First, the @remotion/captions package needs to be installed.17If it is not installed, use the following command:1819```bash20npx remotion add @remotion/captions # If project uses npm21bunx remotion add @remotion/captions # If project uses bun22yarn remotion add @remotion/captions # If project uses yarn23pnpm exec remotion add @remotion/captions # If project uses pnpm24```2526## Reading an .srt file2728Use `staticFile()` to reference an `.srt` file in your `public` folder, then fetch and parse it:2930```tsx31import { useState, useEffect, useCallback } from "react";32import { AbsoluteFill, staticFile, useDelayRender } from "remotion";33import { parseSrt } from "@remotion/captions";34import type { Caption } from "@remotion/captions";3536export const MyComponent: React.FC = () => {37const [captions, setCaptions] = useState<Caption[] | null>(null);38const { delayRender, continueRender, cancelRender } = useDelayRender();39const [handle] = useState(() => delayRender());4041const fetchCaptions = useCallback(async () => {42try {43const response = await fetch(staticFile("subtitles.srt"));44const text = await response.text();45const { captions: parsed } = parseSrt({ input: text });46setCaptions(parsed);47continueRender(handle);48} catch (e) {49cancelRender(e);50}51}, [continueRender, cancelRender, handle]);5253useEffect(() => {54fetchCaptions();55}, [fetchCaptions]);5657if (!captions) {58return null;59}6061return <AbsoluteFill>{/* Use captions here */}</AbsoluteFill>;62};63```6465Remote URLs are also supported - you can `fetch()` a remote file via URL instead of using `staticFile()`.6667## Using imported captions6869Once parsed, the captions are in the `Caption` format and can be used with all `@remotion/captions` utilities.70