Skip to content

🎥 Make videos programmatically with React

cool

Jonny Burger87eeda1ad3f3
diff --git a/packages/captions/package.json b/packages/captions/package.json
index ae59afaed6..f5b2a7c243 100644
--- a/packages/captions/package.json
+++ b/packages/captions/package.json
@@ -19,7 +19,7 @@
"dist"
],
"author": "Jonny Burger <[email protected]>",
- "license": "SEE LICENSE IN LICENSE.md",
+ "license": "MIT",
"dependencies": {},
"peerDependencies": {},
"devDependencies": {},
diff --git a/packages/captions/src/index.ts b/packages/captions/src/index.ts
index 2512290121..03ab477927 100644
--- a/packages/captions/src/index.ts
+++ b/packages/captions/src/index.ts
@@ -4,4 +4,4 @@ export {
TikTokToken,
createTikTokStyleCaptions,
} from './create-tiktok-style-captions';
-export {ParsedSrt, ParsedSrtLine, parseSrt} from './parse-srt';
+export {parseSrt} from './parse-srt';
diff --git a/packages/captions/src/parse-srt.ts b/packages/captions/src/parse-srt.ts
index 4dcb6d9071..1de3fe43d0 100644
--- a/packages/captions/src/parse-srt.ts
+++ b/packages/captions/src/parse-srt.ts
@@ -1,11 +1,4 @@
-export type ParsedSrtLine = {
- index: number;
- text: string;
- startInSeconds: number;
- endInSeconds: number;
-};
-
-export type ParsedSrt = ParsedSrtLine[];
+import {Caption} from './captions';
function toSeconds(time: string) {
const [first, second, third] = time.split(':');
@@ -40,12 +33,12 @@ export type ParseSrtInput = {
};
export type ParseSrtOutput = {
- lines: ParsedSrt;
+ lines: Caption[];
};
export const parseSrt = ({input}: ParseSrtInput): ParseSrtOutput => {
const inputLines = input.split('\n');
- const lines: ParsedSrtLine[] = [];
+ const lines: Caption[] = [];
for (let i = 0; i < inputLines.length; i++) {
const line = inputLines[i];
@@ -55,21 +48,29 @@ export const parseSrt = ({input}: ParseSrtInput): ParseSrtOutput => {
const start = toSeconds(nextLineSplit[0] as string);
const end = toSeconds(nextLineSplit[1] as string);
lines.push({
- index: Number(line),
text: '',
- startInSeconds: start,
- endInSeconds: end,
+ startMs: start * 1000,
+ endMs: end * 1000,
+ confidence: 1,
+ timestampMs: ((start + end) / 2) * 1000,
});
} else if (line?.includes(' --> ')) {
continue;
} else if (line?.trim() === '') {
- (lines[lines.length - 1] as ParsedSrtLine).text = (
- lines[lines.length - 1] as ParsedSrtLine
+ (lines[lines.length - 1] as Caption).text = (
+ lines[lines.length - 1] as Caption
).text.trim();
} else {
- (lines[lines.length - 1] as ParsedSrtLine).text += line + '\n';
+ (lines[lines.length - 1] as Caption).text += line + '\n';
}
}
- return {lines};
+ return {
+ lines: lines.map((l) => {
+ return {
+ ...l,
+ text: l.text.trimEnd(),
+ };
+ }),
+ };
};
diff --git a/packages/captions/src/test/srt.test.ts b/packages/captions/src/test/srt.test.ts
index 5adfc24062..2fe01ab473 100644
--- a/packages/captions/src/test/srt.test.ts
+++ b/packages/captions/src/test/srt.test.ts
@@ -2,7 +2,6 @@ import {expect, test} from 'bun:test';
import {parseSrt} from '../parse-srt';
const input = `
-
1
00:00:00,000 --> 00:00:02,500
Welcome to the Example Subtitle File!
@@ -15,88 +14,31 @@ This is a demonstration of SRT subtitles.
00:00:07,000 --> 00:00:10,500
You can use SRT files to add subtitles to your videos.
-4
-00:00:12,000 --> 00:00:15,000
-Each subtitle entry consists of a number, a timecode,
-and the subtitle text.
-
-5
-00:00:16,000 --> 00:00:20,000
-The timecode format is hours:minutes:seconds,milliseconds.
-
-6
-00:00:21,000 --> 00:00:25,000
-You can adjust the timing to match your video.
-
-7
-00:00:26,000 --> 00:00:30,000
-Make sure the subtitle text is clear and readable.
-
-8
-00:00:31,000 --> 00:00:35,000
-And that's how you create an SRT subtitle file!
-
-9
-00:00:36,000 --> 00:00:40,000
-Enjoy adding subtitles to your videos!
`.trim();
test('Should create captions', () => {
const {lines} = parseSrt({input});
expect(lines).toEqual([
{
- endInSeconds: 2.5,
- index: 1,
- startInSeconds: 0,
+ confidence: 1,
+ endMs: 2500,
+ startMs: 0,
text: 'Welcome to the Example Subtitle File!',
+ timestampMs: 1250,
},
{
- endInSeconds: 6,
- index: 2,
- startInSeconds: 3,
+ confidence: 1,
+ endMs: 6000,
+ startMs: 3000,
text: 'This is a demonstration of SRT subtitles.',
+ timestampMs: 4500,
},
{
- endInSeconds: 10.5,
- index: 3,
- startInSeconds: 7,
+ confidence: 1,
+ endMs: 10500,
+ startMs: 7000,
text: 'You can use SRT files to add subtitles to your videos.',
- },
- {
- endInSeconds: 15,
- index: 4,
- startInSeconds: 12,
- text: 'Each subtitle entry consists of a number, a timecode,\nand the subtitle text.',
- },
- {
- endInSeconds: 20,
- index: 5,
- startInSeconds: 16,
- text: 'The timecode format is hours:minutes:seconds,milliseconds.',
- },
- {
- endInSeconds: 25,
- index: 6,
- startInSeconds: 21,
- text: 'You can adjust the timing to match your video.',
- },
- {
- endInSeconds: 30,
- index: 7,
- startInSeconds: 26,
- text: 'Make sure the subtitle text is clear and readable.',
- },
- {
- endInSeconds: 35,
- index: 8,
- startInSeconds: 31,
- text: "And that's how you create an SRT subtitle file!",
- },
- {
- endInSeconds: 40,
- index: 9,
- startInSeconds: 36,
- text: 'Enjoy adding subtitles to your videos!\n',
+ timestampMs: 8750,
},
]);
});
diff --git a/packages/docs/docs/captions/TableOfContents.tsx b/packages/docs/docs/captions/TableOfContents.tsx
index 1dfa15f259..cc970f4b5e 100644
--- a/packages/docs/docs/captions/TableOfContents.tsx
+++ b/packages/docs/docs/captions/TableOfContents.tsx
@@ -6,6 +6,12 @@ export const TableOfContents: React.FC = () => {
return (
<div>
<Grid>
+ <TOCItem link="/docs/captions/parse-srt">
+ <strong>{'parseSrt()'}</strong>
+ <div>
+ Parse a .srt file into a <code>Caption</code> array
+ </div>
+ </TOCItem>
<TOCItem link="/docs/captions/create-tiktok-style-captions">
<strong>{'createTikTokStyleCaptions()'}</strong>
<div>Structure the captions for TikTok-style display</div>
diff --git a/packages/docs/docs/captions/parse-srt.mdx b/packages/docs/docs/captions/parse-srt.mdx
new file mode 100644
index 0000000000..9ec5317857
--- /dev/null
+++ b/packages/docs/docs/captions/parse-srt.mdx
@@ -0,0 +1,63 @@
+---
+title: parseSrt()
+crumb: '@remotion/captions'
+---
+
+Parses the contents of a SubRip file (`.srt`) and returns an array of [`Caption`](/docs/captions) items.
+
+```tsx twoslash title="Example usage"
+import {parseSrt} from '@remotion/captions';
+
+const input = `
+1
+00:00:00,000 --> 00:00:02,500
+Welcome to the Example Subtitle File!
+
+2
+00:00:03,000 --> 00:00:06,000
+This is a demonstration of SRT subtitles.
+
+3
+00:00:07,000 --> 00:00:10,500
+You can use SRT files to add subtitles to your videos.
+
+`.trim();
+
+const {lines} = parseSrt({input});
+
+/* lines = [
+ {
+ endInSeconds: 2.5,
+ index: 1,
+ startInSeconds: 0,
+ text: 'Welcome to the Example Subtitle File!',
+ },
+ {
+ endInSeconds: 6,
+ index: 2,
+ startInSeconds: 3,
+ text: 'This is a demonstration of SRT subtitles.',
+ },
+ {
+ endInSeconds: 10.5,
+ index: 3,
+ startInSeconds: 7,
+ text: 'You can use SRT files to add subtitles to your videos.',
+ }
+]
+*/
+```
+
+## API
+
+### `input`
+
+The contents of a `.srt` file as a `string`.
+
+## Return value
+
+An object with the following properties:
+
+### `lines`
+
+An array of [`Caption`](/docs/captions) items.
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 64f420511b..d775b485aa 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -574,7 +574,11 @@ module.exports = {
type: 'doc',
id: 'captions/index',
},
- items: ['captions/caption', 'captions/create-tiktok-style-captions'],
+ items: [
+ 'captions/caption',
+ 'captions/parse-srt',
+ 'captions/create-tiktok-style-captions',
+ ],
},
{
type: 'category',
diff --git a/packages/template-tiktok/src/CaptionedVideo/Subtitle.tsx b/packages/template-tiktok/src/CaptionedVideo/SubtitlePage.tsx
similarity index 83%
rename from packages/template-tiktok/src/CaptionedVideo/Subtitle.tsx
rename to packages/template-tiktok/src/CaptionedVideo/SubtitlePage.tsx
index 9c00274d0b..a7d30b0137 100644
--- a/packages/template-tiktok/src/CaptionedVideo/Subtitle.tsx
+++ b/packages/template-tiktok/src/CaptionedVideo/SubtitlePage.tsx
@@ -8,7 +8,7 @@ import {
import { Page } from "./Page";
import { TikTokPage } from "@remotion/captions";
-const Subtitle: React.FC<{ page: TikTokPage }> = ({ page }) => {
+const SubtitlePage: React.FC<{ page: TikTokPage }> = ({ page }) => {
const frame = useCurrentFrame();
const { fps } = useVideoConfig();
@@ -28,4 +28,4 @@ const Subtitle: React.FC<{ page: TikTokPage }> = ({ page }) => {
);
};
-export default Subtitle;
+export default SubtitlePage;
diff --git a/packages/template-tiktok/src/CaptionedVideo/index.tsx b/packages/template-tiktok/src/CaptionedVideo/index.tsx
index 777ca6124e..d5756ca711 100644
--- a/packages/template-tiktok/src/CaptionedVideo/index.tsx
+++ b/packages/template-tiktok/src/CaptionedVideo/index.tsx
@@ -12,7 +12,7 @@ import {
watchStaticFile,
} from "remotion";
import { z } from "zod";
-import Subtitle from "./Subtitle";
+import SubtitlePage from "./SubtitlePage";
import { getVideoMetadata } from "@remotion/media-utils";
import { loadFont } from "../load-font";
import { NoCaptionFile } from "./NoCaptionFile";
@@ -90,10 +90,10 @@ export const CaptionedVideo: React.FC<{
};
}, [fetchSubtitles, src, subtitlesFile]);
- const { captions } = useMemo(() => {
+ const { pages } = useMemo(() => {
return createTikTokStyleCaptions({
combineTokensWithinMilliseconds: SWITCH_CAPTIONS_EVERY_MS,
- transcription: subtitles ?? [],
+ captions: subtitles ?? [],
});
}, [subtitles]);
@@ -107,11 +107,11 @@ export const CaptionedVideo: React.FC<{
src={src}
/>
</AbsoluteFill>
- {captions.map((subtitle, index) => {
- const nextSubtitle = captions[index + 1] ?? null;
- const subtitleStartFrame = (subtitle.startMs / 1000) * fps;
+ {pages.map((page, index) => {
+ const nextPage = pages[index + 1] ?? null;
+ const subtitleStartFrame = (page.startMs / 1000) * fps;
const subtitleEndFrame = Math.min(
- nextSubtitle ? (nextSubtitle.startMs / 1000) * fps : Infinity,
+ nextPage ? (nextPage.startMs / 1000) * fps : Infinity,
subtitleStartFrame + SWITCH_CAPTIONS_EVERY_MS,
);
const durationInFrames = subtitleEndFrame - subtitleStartFrame;
@@ -125,7 +125,7 @@ export const CaptionedVideo: React.FC<{
from={subtitleStartFrame}
durationInFrames={durationInFrames}
>
- <Subtitle key={index} page={subtitle} />;
+ <SubtitlePage key={index} page={page} />;
</Sequence>
);
})}