Skip to content

🎥 Make videos programmatically with React

parse srt

Jonny Burger260b68a01abe
diff --git a/packages/captions/src/parse-srt.ts b/packages/captions/src/parse-srt.ts
index 1de3fe43d0..8644f6ce77 100644
--- a/packages/captions/src/parse-srt.ts
+++ b/packages/captions/src/parse-srt.ts
@@ -33,12 +33,12 @@ export type ParseSrtInput = {
};
export type ParseSrtOutput = {
- lines: Caption[];
+ captions: Caption[];
};
export const parseSrt = ({input}: ParseSrtInput): ParseSrtOutput => {
const inputLines = input.split('\n');
- const lines: Caption[] = [];
+ const captions: Caption[] = [];
for (let i = 0; i < inputLines.length; i++) {
const line = inputLines[i];
@@ -47,7 +47,7 @@ export const parseSrt = ({input}: ParseSrtInput): ParseSrtOutput => {
const nextLineSplit = nextLine.split(' --> ');
const start = toSeconds(nextLineSplit[0] as string);
const end = toSeconds(nextLineSplit[1] as string);
- lines.push({
+ captions.push({
text: '',
startMs: start * 1000,
endMs: end * 1000,
@@ -57,16 +57,16 @@ export const parseSrt = ({input}: ParseSrtInput): ParseSrtOutput => {
} else if (line?.includes(' --> ')) {
continue;
} else if (line?.trim() === '') {
- (lines[lines.length - 1] as Caption).text = (
- lines[lines.length - 1] as Caption
+ (captions[captions.length - 1] as Caption).text = (
+ captions[captions.length - 1] as Caption
).text.trim();
} else {
- (lines[lines.length - 1] as Caption).text += line + '\n';
+ (captions[captions.length - 1] as Caption).text += line + '\n';
}
}
return {
- lines: lines.map((l) => {
+ captions: captions.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 2fe01ab473..24abea8cf5 100644
--- a/packages/captions/src/test/srt.test.ts
+++ b/packages/captions/src/test/srt.test.ts
@@ -17,7 +17,7 @@ You can use SRT files to add subtitles to your videos.
`.trim();
test('Should create captions', () => {
- const {lines} = parseSrt({input});
+ const {captions: lines} = parseSrt({input});
expect(lines).toEqual([
{
confidence: 1,
diff --git a/packages/docs/docs/captions/caption.mdx b/packages/docs/docs/captions/caption.mdx
index 168bd60ed9..606e1ea1c6 100644
--- a/packages/docs/docs/captions/caption.mdx
+++ b/packages/docs/docs/captions/caption.mdx
@@ -16,4 +16,5 @@ By establishing a standard data structure, we allow many operations that involve
- Transcribing (using the [`@remotion/install-whisper-cpp`](/docs/install-whisper-cpp) package)
- Formatting (for example, creating pages using [`createTikTokStyleCaptions()`](/docs/captions/create-tiktok-style-captions))
+- Parsing (using the [`parseSrt()`](/docs/captions/parse-srt) function)
- Serializing (for example to a .srt file)
diff --git a/packages/docs/docs/captions/create-tiktok-style-captions.mdx b/packages/docs/docs/captions/create-tiktok-style-captions.mdx
index 479d194b64..138b48b9ab 100644
--- a/packages/docs/docs/captions/create-tiktok-style-captions.mdx
+++ b/packages/docs/docs/captions/create-tiktok-style-captions.mdx
@@ -91,7 +91,7 @@ const {pages} = createTikTokStyleCaptions({
### `captions`
-An array of `Caption` objects.
+An array of [`Caption`](/docs/captions/caption) objects.
### `combineTokensWithinMilliseconds`
diff --git a/packages/docs/docs/captions/parse-srt.mdx b/packages/docs/docs/captions/parse-srt.mdx
index 9ec5317857..e5ab2f68b4 100644
--- a/packages/docs/docs/captions/parse-srt.mdx
+++ b/packages/docs/docs/captions/parse-srt.mdx
@@ -3,6 +3,8 @@ title: parseSrt()
crumb: '@remotion/captions'
---
+# parseSrt()<AvailableFrom v="4.0.216 "/>
+
Parses the contents of a SubRip file (`.srt`) and returns an array of [`Caption`](/docs/captions) items.
```tsx twoslash title="Example usage"
@@ -23,27 +25,30 @@ You can use SRT files to add subtitles to your videos.
`.trim();
-const {lines} = parseSrt({input});
+const {captions} = parseSrt({input});
-/* lines = [
+/* captions = [
{
- 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.',
- }
+ timestampMs: 8750,
+ },
]
*/
```
@@ -58,6 +63,11 @@ The contents of a `.srt` file as a `string`.
An object with the following properties:
-### `lines`
+### `captions`
+
+An array of [`Caption`](/docs/captions/captions) items.
+
+## See also
-An array of [`Caption`](/docs/captions) items.
+- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/captions/src/parse-srt.ts)
+- [`@remotion/captions`](/docs/captions)