Importing .srt or .vtt subtitles into Remotion
If you have an existing subtitle file, you can import it into Remotion:
- using
parseSrt()for.srtfiles - using
parseVtt()for.vttWebVTT files.
These APIs take a string. Read the subtitle file as text before calling either function.
Both APIs return captions in the standard Remotion Caption[] format.
Reading an .srt file
Use staticFile() to reference an .srt file in your public folder, then fetch and parse it:
Importing captions from .srtimport {parseSrt } from '@remotion/captions'; import {staticFile } from 'remotion'; constresponse = awaitfetch (staticFile ('subtitles.srt')); consttext = awaitresponse .text (); const {captions } =parseSrt ({input :text });
Reading a .vtt filev4.0.537
Use staticFile() to reference a .vtt file in your public folder, then fetch and parse it.
Importing captions from .vttimport {parseVtt } from '@remotion/captions'; import {staticFile } from 'remotion'; constresponse = awaitfetch (staticFile ('subtitles.vtt')); consttext = awaitresponse .text (); const {captions } =parseVtt ({input :text });
Fetching captions in a component
Use useDelayRender() to wait for the captions to be successfully loaded, before rendering the component.
Importing captions from .srtimport {useState ,useEffect ,useCallback } from 'react'; import {staticFile ,useDelayRender } from 'remotion'; import {parseSrt } from '@remotion/captions'; import type {Caption } from '@remotion/captions'; export constMyComponent :React .FC = () => { const [captions ,setCaptions ] =useState <Caption [] | null>(null); const {delayRender ,continueRender ,cancelRender } =useDelayRender (); const [handle ] =useState (() =>delayRender ()); constfetchCaptions =useCallback (async () => { try { constresponse = awaitfetch (staticFile ('subtitles.srt')); consttext = awaitresponse .text (); const {captions :parsed } =parseSrt ({input :text });setCaptions (parsed );continueRender (handle ); } catch (e ) {cancelRender (e ); } }, [continueRender ,cancelRender ,handle ]);useEffect (() => {fetchCaptions (); }, [fetchCaptions ]); if (!captions ) { return null; } return <>{/* Use captions here */}</>; };
You can also fetch() a remote file via URL.