Skip to content

πŸŽ₯ Make videos programmatically with React

@remotion/studio: Limit caption imports to Remotion JSON

Igor Samokhovetsab4151a2349f
diff --git a/bun.lock b/bun.lock
index f5ece55fd3..6582230b33 100644
--- a/bun.lock
+++ b/bun.lock
@@ -1805,10 +1805,8 @@
"@jridgewell/trace-mapping": "0.3.31",
"@remotion/canvas": "workspace:*",
"@remotion/captions": "workspace:*",
- "@remotion/elevenlabs": "workspace:*",
"@remotion/media": "workspace:*",
"@remotion/media-utils": "workspace:*",
- "@remotion/openai-whisper": "workspace:*",
"@remotion/player": "workspace:*",
"@remotion/renderer": "workspace:*",
"@remotion/studio-codemods": "workspace:*",
diff --git a/packages/docs/docs/elevenlabs/TableOfContents.tsx b/packages/docs/docs/elevenlabs/TableOfContents.tsx
index 251b106f10..069651aee2 100644
--- a/packages/docs/docs/elevenlabs/TableOfContents.tsx
+++ b/packages/docs/docs/elevenlabs/TableOfContents.tsx
@@ -13,10 +13,6 @@ export const TableOfContents: React.FC = () => {
<code>Caption</code>
</div>
</TOCItem>
- <TOCItem link="/docs/elevenlabs/is-elevenlabs-transcript-json">
- <strong>{'isElevenLabsTranscriptJson()'}</strong>
- <div>Detect ElevenLabs Speech to Text JSON</div>
- </TOCItem>
</Grid>
</div>
);
diff --git a/packages/docs/docs/elevenlabs/elevenlabs-transcript-to-captions.mdx b/packages/docs/docs/elevenlabs/elevenlabs-transcript-to-captions.mdx
index f968d501fe..2a3e06608c 100644
--- a/packages/docs/docs/elevenlabs/elevenlabs-transcript-to-captions.mdx
+++ b/packages/docs/docs/elevenlabs/elevenlabs-transcript-to-captions.mdx
@@ -63,7 +63,6 @@ An array of [`Caption`](/docs/captions/caption) objects.
## See also
-- [`isElevenLabsTranscriptJson()`](/docs/elevenlabs/is-elevenlabs-transcript-json)
- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/elevenlabs/src/elevenlabs-transcript-to-captions.ts)
- [`@remotion/elevenlabs`](/docs/elevenlabs)
- [`@remotion/openai-whisper`](/docs/openai-whisper)
diff --git a/packages/docs/docs/elevenlabs/index.mdx b/packages/docs/docs/elevenlabs/index.mdx
index 94649d6148..d665ed253a 100644
--- a/packages/docs/docs/elevenlabs/index.mdx
+++ b/packages/docs/docs/elevenlabs/index.mdx
@@ -6,7 +6,7 @@ crumb: 'Subtitle videos'
# @remotion/elevenlabs<AvailableFrom v="4.0.443" />
-The `@remotion/elevenlabs` package detects and converts the output of the [ElevenLabs Speech to Text API](https://elevenlabs.io/docs/api-reference/speech-to-text/convert) into an array of [`Caption`](/docs/captions/caption) objects, making them compatible with other caption-related Remotion APIs.
+The `@remotion/elevenlabs` package converts the output of the [ElevenLabs Speech to Text API](https://elevenlabs.io/docs/api-reference/speech-to-text/convert) into an array of [`Caption`](/docs/captions/caption) objects, making them compatible with other caption-related Remotion APIs.
import {TableOfContents} from './TableOfContents';
diff --git a/packages/docs/docs/elevenlabs/is-elevenlabs-transcript-json.mdx b/packages/docs/docs/elevenlabs/is-elevenlabs-transcript-json.mdx
deleted file mode 100644
index c5a8653e50..0000000000
--- a/packages/docs/docs/elevenlabs/is-elevenlabs-transcript-json.mdx
+++ /dev/null
@@ -1,52 +0,0 @@
----
-image: /generated/articles-docs-elevenlabs-is-elevenlabs-transcript-json.png
-title: isElevenLabsTranscriptJson()
-crumb: '@remotion/elevenlabs'
----
-
-# isElevenLabsTranscriptJson()<AvailableFrom v="4.0.521" />
-
-Returns whether a value has the top-level fields of an ElevenLabs Speech to Text JSON response.
-
-Use it before [`elevenLabsTranscriptToCaptions()`](/docs/elevenlabs/elevenlabs-transcript-to-captions) when accepting JSON whose source is unknown.
-
-This function detects the format but does not validate every word. The converter validates the fields it uses and throws an actionable error if the recognized input is malformed.
-
-## Example
-
-```ts twoslash title="convert-upload.ts"
-import {
- elevenLabsTranscriptToCaptions,
- isElevenLabsTranscriptJson,
-} from '@remotion/elevenlabs';
-
-const contents = '{"language_code":"eng","text":"Hello","words":[]}';
-const json = JSON.parse(contents);
-
-if (isElevenLabsTranscriptJson(json)) {
- const {captions} = elevenLabsTranscriptToCaptions({transcript: json});
- console.log(captions);
-}
-```
-
-## API
-
-### `value`
-
-The value to inspect. It may have any JavaScript type.
-
-### Return value
-
-A boolean. It is `true` when the value is an object with `language_code` and either `words` or `text` as top-level fields.
-
-Additional fields do not affect detection.
-
-## Compatibility
-
-<CompatibilityTable chrome firefox safari nodejs bun serverlessFunctions clientSideRendering serverSideRendering player studio />
-
-## See also
-
-- [`elevenLabsTranscriptToCaptions()`](/docs/elevenlabs/elevenlabs-transcript-to-captions)
-- [`isOpenAiWhisperJson()`](/docs/openai-whisper/is-open-ai-whisper-json)
-- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/elevenlabs/src/elevenlabs-transcript-to-captions.ts)
diff --git a/packages/docs/docs/openai-whisper/TableOfContents.tsx b/packages/docs/docs/openai-whisper/TableOfContents.tsx
index a82c9a7932..0488a2a185 100644
--- a/packages/docs/docs/openai-whisper/TableOfContents.tsx
+++ b/packages/docs/docs/openai-whisper/TableOfContents.tsx
@@ -6,10 +6,6 @@ export const TableOfContents: React.FC = () => {
return (
<div>
<Grid>
- <TOCItem link="/docs/openai-whisper/is-open-ai-whisper-json">
- <strong>{'isOpenAiWhisperJson()'}</strong>
- <div>Detect OpenAI Whisper verbose JSON</div>
- </TOCItem>
<TOCItem link="/docs/openai-whisper/openai-whisper-api-to-captions">
<strong>{'openAiWhisperApiToCaptions()'}</strong>
<div>
diff --git a/packages/docs/docs/openai-whisper/is-open-ai-whisper-json.mdx b/packages/docs/docs/openai-whisper/is-open-ai-whisper-json.mdx
deleted file mode 100644
index 8829011cb2..0000000000
--- a/packages/docs/docs/openai-whisper/is-open-ai-whisper-json.mdx
+++ /dev/null
@@ -1,52 +0,0 @@
----
-image: /generated/articles-docs-openai-whisper-is-open-ai-whisper-json.png
-title: isOpenAiWhisperJson()
-crumb: '@remotion/openai-whisper'
----
-
-# isOpenAiWhisperJson()<AvailableFrom v="4.0.521" />
-
-Returns whether a value has the top-level fields of an OpenAI Whisper verbose JSON response.
-
-Use it before [`openAiWhisperApiToCaptions()`](/docs/openai-whisper/openai-whisper-api-to-captions) when accepting JSON whose source is unknown.
-
-This function detects the format but does not validate every word. The converter validates the fields it uses and throws an actionable error if the recognized input is malformed.
-
-## Example
-
-```ts twoslash title="convert-upload.ts"
-import {
- isOpenAiWhisperJson,
- openAiWhisperApiToCaptions,
-} from '@remotion/openai-whisper';
-
-const contents = '{"language":"english","text":"Hello","words":[]}';
-const json = JSON.parse(contents);
-
-if (isOpenAiWhisperJson(json)) {
- const {captions} = openAiWhisperApiToCaptions({transcription: json});
- console.log(captions);
-}
-```
-
-## API
-
-### `value`
-
-The value to inspect. It may have any JavaScript type.
-
-### Return value
-
-A boolean. It is `true` when the value is an object without `language_code`, with `text`, and with at least one of `task`, `language`, `duration`, `segments`, or `words` as top-level fields.
-
-Other additional fields do not affect detection.
-
-## Compatibility
-
-<CompatibilityTable chrome firefox safari nodejs bun serverlessFunctions clientSideRendering serverSideRendering player studio />
-
-## See also
-
-- [`openAiWhisperApiToCaptions()`](/docs/openai-whisper/openai-whisper-api-to-captions)
-- [`isElevenLabsTranscriptJson()`](/docs/elevenlabs/is-elevenlabs-transcript-json)
-- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/openai-whisper/src/openai-whisper-api-to-captions.ts)
diff --git a/packages/docs/docs/openai-whisper/openai-whisper-api-to-captions.mdx b/packages/docs/docs/openai-whisper/openai-whisper-api-to-captions.mdx
index 762cde20ee..a690eb6c30 100644
--- a/packages/docs/docs/openai-whisper/openai-whisper-api-to-captions.mdx
+++ b/packages/docs/docs/openai-whisper/openai-whisper-api-to-captions.mdx
@@ -32,7 +32,6 @@ const {captions} = openAiWhisperApiToCaptions({transcription});
## See also
-- [`isOpenAiWhisperJson()`](/docs/openai-whisper/is-open-ai-whisper-json)
- [Source code for this function](https://github.com/remotion-dev/remotion/blob/main/packages/openai-whisper/src/openai-whisper-api-to-captions.ts)
- [`@remotion/openai-whisper`](/docs/openai-whisper)
- [`@remotion/install-whisper-cpp`](/docs/install-whisper-cpp)
diff --git a/packages/docs/elements/captions/index.mdx b/packages/docs/elements/captions/index.mdx
index 25ef2cd6c5..94bda60e24 100644
--- a/packages/docs/elements/captions/index.mdx
+++ b/packages/docs/elements/captions/index.mdx
@@ -9,12 +9,12 @@ import {ElementLibrary} from '@site/src/components/Elements/ElementLibrary';
## Importing captions into Studio<AvailableFrom v="4.0.521" />
-<Step>1</Step> Install an Element from the [Captions category](/elements/captions), or add [`Interactive.captionsSchema`](/docs/interactivity-schema#interactivecaptionsschema) to an interactive component that accepts [`Caption[]`](/docs/captions/caption).<br />
-<Step>2</Step> Select its caption layer in Studio.<br />
-<Step>3</Step> In the Captions inspector, click **Import captions** and choose the JSON or SRT file exported by ElevenLabs or your Whisper transcription tool.
+<Step>1</Step> Install an Element from the [Captions category](/elements/captions), or add [`Interactive.captionsSchema`](/docs/interactivity-schema#interactivecaptionsschema) to an interactive component that accepts [`Caption[]`](/docs/captions/caption).
+<br />
+<Step>2</Step> Select its caption layer in Studio.
+<br />
+<Step>3</Step> In the Captions inspector, click **Import** and choose a JSON file containing a Remotion [`Caption[]`](/docs/captions/caption).
-Studio detects and converts Remotion [`Caption[]`](/docs/captions/caption) JSON, ElevenLabs Speech-to-Text JSON, ElevenLabs segmented export JSON, OpenAI Whisper verbose JSON, and [SRT subtitles](/docs/captions/parse-srt). JSON transcripts must include word-level timestamps. SRT cue timestamps work, but cannot provide true word-by-word animation.
-
-Files are processed locally.
+Files are processed locally. Importing SRT and transcription service JSON is tracked in [#11062](https://github.com/remotion-dev/remotion/issues/11062).
<ElementLibrary category="captions" />
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index a0d4cd4b3d..03e4a8385f 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -354,10 +354,7 @@ const sidebars: SidebarsConfig = {
type: 'doc',
id: 'elevenlabs/index',
},
- items: [
- 'elevenlabs/elevenlabs-transcript-to-captions',
- 'elevenlabs/is-elevenlabs-transcript-json',
- ],
+ items: ['elevenlabs/elevenlabs-transcript-to-captions'],
},
{
type: 'category',
@@ -581,10 +578,7 @@ const sidebars: SidebarsConfig = {
type: 'doc',
id: 'openai-whisper/index',
},
- items: [
- 'openai-whisper/is-open-ai-whisper-json',
- 'openai-whisper/openai-whisper-api-to-captions',
- ],
+ items: ['openai-whisper/openai-whisper-api-to-captions'],
},
{
type: 'category',
diff --git a/packages/docs/src/components/Elements/ElementPage.tsx b/packages/docs/src/components/Elements/ElementPage.tsx
index 378d11a9fd..7e960daa5d 100644
--- a/packages/docs/src/components/Elements/ElementPage.tsx
+++ b/packages/docs/src/components/Elements/ElementPage.tsx
@@ -1,10 +1,10 @@
import Head from '@docusaurus/Head';
import {
installInStudio,
- type InstallInStudioErrorCode,
isInsideStudio,
setStudioDragData,
StudioProtocolInternals,
+ type InstallInStudioErrorCode,
} from '@remotion/studio-protocol';
import React, {
useCallback,
@@ -345,9 +345,9 @@ export const ElementPage: React.FC<ElementPageProps> = ({
<p className={styles.description}>{description}</p>
{definition.category === 'captions' ? (
<p className={styles.description} style={{marginTop: 8}}>
- Have a transcript?{' '}
+ Have captions?{' '}
<a href="/elements/captions/#importing-captions-into-studio">
- Import JSON or SRT from the Captions inspector in Studio.
+ Import Remotion Caption[] JSON from Studio.
</a>
</p>
) : null}
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index ddb63e5b6e..a492a185f7 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -2528,15 +2528,6 @@ export const articles = [
noAi: false,
slug: 'elevenlabs/index',
},
- {
- id: 'elevenlabs/is-elevenlabs-transcript-json',
- title: 'isElevenLabsTranscriptJson()',
- relativePath: 'docs/elevenlabs/is-elevenlabs-transcript-json.mdx',
- compId: 'articles-docs-elevenlabs-is-elevenlabs-transcript-json',
- crumb: '@remotion/elevenlabs',
- noAi: false,
- slug: 'elevenlabs/is-elevenlabs-transcript-json',
- },
{
id: 'enable-scss/enable-scss',
title: 'enableScss()',
@@ -5075,15 +5066,6 @@ export const articles = [
noAi: false,
slug: 'openai-whisper/index',
},
- {
- id: 'openai-whisper/is-open-ai-whisper-json',
- title: 'isOpenAiWhisperJson()',
- relativePath: 'docs/openai-whisper/is-open-ai-whisper-json.mdx',
- compId: 'articles-docs-openai-whisper-is-open-ai-whisper-json',
- crumb: '@remotion/openai-whisper',
- noAi: false,
- slug: 'openai-whisper/is-open-ai-whisper-json',
- },
{
id: 'openai-whisper/openai-whisper-api-to-captions',
title: 'openAiWhisperApiToCaptions()',
diff --git a/packages/docs/static/generated/articles-docs-elevenlabs-is-elevenlabs-transcript-json.png b/packages/docs/static/generated/articles-docs-elevenlabs-is-elevenlabs-transcript-json.png
deleted file mode 100644
index e4abd628f3..0000000000
Binary files a/packages/docs/static/generated/articles-docs-elevenlabs-is-elevenlabs-transcript-json.png and /dev/null differ
diff --git a/packages/docs/static/generated/articles-docs-openai-whisper-is-open-ai-whisper-json.png b/packages/docs/static/generated/articles-docs-openai-whisper-is-open-ai-whisper-json.png
deleted file mode 100644
index d0c1aab130..0000000000
Binary files a/packages/docs/static/generated/articles-docs-openai-whisper-is-open-ai-whisper-json.png and /dev/null differ
diff --git a/packages/elevenlabs/src/elevenlabs-transcript-to-captions.ts b/packages/elevenlabs/src/elevenlabs-transcript-to-captions.ts
index 9df34fdea1..671644ba95 100644
--- a/packages/elevenlabs/src/elevenlabs-transcript-to-captions.ts
+++ b/packages/elevenlabs/src/elevenlabs-transcript-to-captions.ts
@@ -9,126 +9,34 @@ export type ElevenLabsTranscriptToCaptionsOutput = {
captions: Caption[];
};
-export const isElevenLabsTranscriptJson = (value: unknown): boolean => {
- return (
- typeof value === 'object' &&
- value !== null &&
- !Array.isArray(value) &&
- 'language_code' in value &&
- ('words' in value || 'text' in value)
- );
-};
-
export const elevenLabsTranscriptToCaptions = ({
transcript,
}: ElevenLabsTranscriptToCaptionsInput): ElevenLabsTranscriptToCaptionsOutput => {
const captions: Caption[] = [];
- if (
- typeof transcript !== 'object' ||
- transcript === null ||
- Array.isArray(transcript)
- ) {
+ if (!transcript || !transcript.words || !Array.isArray(transcript.words)) {
throw new Error(
- 'Invalid ElevenLabs transcript: transcript must be an object.',
+ 'Invalid ElevenLabs transcript. The transcript must be generated with `timestamps_granularity` set to `"word"`. See https://www.remotion.dev/docs/elevenlabs/elevenlabs-transcript-to-captions',
);
}
- const {words}: {words: unknown} = transcript;
- if (!Array.isArray(words)) {
- throw new Error(
- 'Invalid ElevenLabs transcript: words must be an array generated with `timestamps_granularity` set to `"word"`. See https://www.remotion.dev/docs/elevenlabs/elevenlabs-transcript-to-captions',
- );
- }
+ const {words} = transcript;
let isFirst = true;
for (let i = 0; i < words.length; i++) {
- const value: unknown = words[i];
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
- throw new Error(
- `Invalid ElevenLabs transcript: words[${i}] must be an object.`,
- );
- }
+ const entry = words[i];
- const entry = value as Record<string, unknown>;
if (entry.type !== 'word') {
continue;
}
- if (typeof entry.text !== 'string') {
- throw new Error(
- `Invalid ElevenLabs transcript: words[${i}].text must be a string.`,
- );
- }
-
- if (
- typeof entry.start !== 'number' ||
- !Number.isFinite(entry.start) ||
- entry.start < 0
- ) {
- throw new Error(
- `Invalid ElevenLabs transcript: words[${i}].start must be a finite, non-negative number.`,
- );
- }
-
- if (
- typeof entry.end !== 'number' ||
- !Number.isFinite(entry.end) ||
- entry.end < 0
- ) {
- throw new Error(
- `Invalid ElevenLabs transcript: words[${i}].end must be a finite, non-negative number.`,
- );
- }
-
- if (entry.end < entry.start) {
- throw new Error(
- `Invalid ElevenLabs transcript: words[${i}].end must not be earlier than words[${i}].start.`,
- );
- }
-
- let spacingStart: number | null = null;
- const previousValue: unknown = i > 0 ? words[i - 1] : null;
- if (
- !isFirst &&
- typeof previousValue === 'object' &&
- previousValue !== null &&
- !Array.isArray(previousValue)
- ) {
- const previousEntry = previousValue as Record<string, unknown>;
- if (previousEntry.type === 'spacing') {
- if (
- typeof previousEntry.start !== 'number' ||
- !Number.isFinite(previousEntry.start) ||
- previousEntry.start < 0
- ) {
- throw new Error(
- `Invalid ElevenLabs transcript: words[${i - 1}].start must be a finite, non-negative number.`,
- );
- }
-
- spacingStart = previousEntry.start;
- }
- }
+ const prevEntry = i > 0 ? words[i - 1] : null;
+ const hasSpacing = prevEntry !== null && prevEntry.type === 'spacing';
- const startMs = (spacingStart ?? entry.start) * 1000;
+ const startMs =
+ !isFirst && hasSpacing ? prevEntry.start * 1000 : entry.start * 1000;
const endMs = entry.end * 1000;
- if (endMs < startMs) {
- throw new Error(
- `Invalid ElevenLabs transcript: words[${i}].end must not be earlier than the caption start.`,
- );
- }
-
- if (
- captions.length > 0 &&
- startMs < captions[captions.length - 1].startMs
- ) {
- throw new Error(
- `Invalid ElevenLabs transcript: words[${i}].start is out of timestamp order.`,
- );
- }
-
const text = isFirst ? entry.text : ` ${entry.text}`;
captions.push({
diff --git a/packages/elevenlabs/src/index.ts b/packages/elevenlabs/src/index.ts
index 75f90603b2..50006f266f 100644
--- a/packages/elevenlabs/src/index.ts
+++ b/packages/elevenlabs/src/index.ts
@@ -2,7 +2,6 @@ export {
ElevenLabsTranscriptToCaptionsInput,
ElevenLabsTranscriptToCaptionsOutput,
elevenLabsTranscriptToCaptions,
- isElevenLabsTranscriptJson,
} from './elevenlabs-transcript-to-captions';
export {
diff --git a/packages/elevenlabs/src/test/elevenlabs-transcript-to-captions.test.ts b/packages/elevenlabs/src/test/elevenlabs-transcript-to-captions.test.ts
index 0af8d82445..4152504afb 100644
--- a/packages/elevenlabs/src/test/elevenlabs-transcript-to-captions.test.ts
+++ b/packages/elevenlabs/src/test/elevenlabs-transcript-to-captions.test.ts
@@ -1,21 +1,9 @@
import {expect, test} from 'bun:test';
import type {ElevenLabsTranscript} from '../elevenlabs-transcript';
-import {
- elevenLabsTranscriptToCaptions,
- isElevenLabsTranscriptJson,
-} from '../elevenlabs-transcript-to-captions';
+import {elevenLabsTranscriptToCaptions} from '../elevenlabs-transcript-to-captions';
import expectedCaptions from './gimme-gimme-captions-snapshot.json';
import transcript from './gimme-gimme-transcript.json';
-test('recognizes ElevenLabs transcript JSON', () => {
- expect(
- isElevenLabsTranscriptJson({language_code: 'eng', text: 'Hello'}),
- ).toBe(true);
- expect(isElevenLabsTranscriptJson({language: 'english', text: 'Hello'})).toBe(
- false,
- );
-});
-
test('converts elevenlabs transcript to captions', () => {
const {captions} = elevenLabsTranscriptToCaptions({
transcript: transcript as ElevenLabsTranscript,
@@ -45,76 +33,3 @@ test('handles empty words array', () => {
expect(captions).toEqual([]);
});
-
-test.each([
- {
- name: 'non-object entry',
- words: [null],
- error: 'words[0] must be an object',
- },
- {
- name: 'invalid timestamp',
- words: [{type: 'word', text: 'Hello', start: Number.NaN, end: 1}],
- error: 'words[0].start must be a finite, non-negative number',
- },
- {
- name: 'invalid consumed spacing timestamp',
- words: [
- {type: 'word', text: 'Hello', start: 0, end: 1},
- {type: 'spacing', text: ' ', start: 'later', end: 2},
- {type: 'word', text: 'world', start: 2, end: 3},
- ],
- error: 'words[1].start must be a finite, non-negative number',
- },
- {
- name: 'reversed timestamps',
- words: [{type: 'word', text: 'Hello', start: 2, end: 1}],
- error: 'words[0].end must not be earlier than words[0].start',
- },
- {
- name: 'out-of-order words',
- words: [
- {type: 'word', text: 'Later', start: 2, end: 3},
- {type: 'word', text: 'Earlier', start: 1, end: 2},
- ],
- error: 'words[1].start is out of timestamp order',
- },
-])('rejects $name', ({words, error}) => {
- expect(() =>
- elevenLabsTranscriptToCaptions({
- transcript: {words} as unknown as ElevenLabsTranscript,
- }),
- ).toThrow(`Invalid ElevenLabs transcript: ${error}`);
-});
-
-test('ignores unknown fields and entry types', () => {
- const {captions} = elevenLabsTranscriptToCaptions({
- transcript: {
- language_code: 'eng',
- language_probability: 1,
- text: 'Hello',
- transcription_id: 'test',
- provider_added_field: true,
- words: [
- {type: 'provider_event', provider_added_field: true},
- {
- type: 'word',
- text: 'Hello',
- start: 0,
- end: 1,
- provider_added_field: true,
- },
- ],
- } as unknown as ElevenLabsTranscript,
- });
-
- expect(captions).toEqual([
- {
- confidence: null,
- startMs: 0,
- endMs: 1000,
- text: 'Hello',
- timestampMs: 500,
- },
- ]);
-});
diff --git a/packages/example/e2e/captions-inspector.test.mts b/packages/example/e2e/captions-inspector.test.mts
index 2ee630f849..bbd3052f8d 100644
--- a/packages/example/e2e/captions-inspector.test.mts
+++ b/packages/example/e2e/captions-inspector.test.mts
@@ -115,22 +115,19 @@ test.describe('captions inspector', () => {
name: 'broken.json',
mimeType: 'application/json',
buffer: Buffer.from(
- JSON.stringify({
- language_code: 'eng',
- segments: [
- {
- text: 'Broken',
- start_time: 0,
- end_time: 1,
- words: [{text: 'Broken', end_time: 1}],
- },
- ],
- }),
+ JSON.stringify([
+ {
+ text: 'Broken',
+ endMs: 1000,
+ timestampMs: 500,
+ confidence: null,
+ },
+ ]),
),
});
await expect(
page.getByText(
- /broken\.json:.*segments\[0\]\.words\[0\]\.start_time must be a finite number/,
+ /broken\.json:.*captions\[0\]\.startMs must be a finite, non-negative number/,
),
).toBeVisible();
expect(fs.readFileSync(elementCaptionsFile, 'utf-8')).toBe(
@@ -141,21 +138,22 @@ test.describe('captions inspector', () => {
name: 'captions.json',
mimeType: 'application/json',
buffer: Buffer.from(
- JSON.stringify({
- language_code: 'eng',
- segments: [
- {
- text: 'Imported captions',
- start_time: 0.1,
- end_time: 0.9,
- words: [
- {text: 'Imported', start_time: 0.1, end_time: 0.4},
- {text: ' ', start_time: 0.4, end_time: 0.5},
- {text: 'captions', start_time: 0.5, end_time: 0.9},
- ],
- },
- ],
- }),
+ JSON.stringify([
+ {
+ text: 'Imported',
+ startMs: 100,
+ endMs: 400,
+ timestampMs: 250,
+ confidence: null,
+ },
+ {
+ text: ' captions',
+ startMs: 400,
+ endMs: 900,
+ timestampMs: 650,
+ confidence: null,
+ },
+ ]),
),
});
await expect(
@@ -166,14 +164,12 @@ test.describe('captions inspector', () => {
);
await page.getByRole('button', {name: 'Replace captions'}).click();
await expect(
- page.getByText(
- 'Imported 2 captions from ElevenLabs segmented JSON. Undo is available.',
- ),
+ page.getByText('Imported 2 captions. Undo is available.'),
).toBeVisible();
await expect(defaultCaption).toHaveValue('Imported');
await expect
.poll(() => fs.readFileSync(elementCaptionsFile, 'utf-8'))
- .toMatch(/startMs:\s*100[\s\S]*text:\s*['"]Imported['"]/);
+ .toMatch(/text:\s*['"]Imported['"][\s\S]*startMs:\s*100/);
expect(fs.readFileSync(elementCallSiteFile, 'utf-8')).toBe(
elementCallSiteSourceBefore,
);
diff --git a/packages/openai-whisper/src/index.ts b/packages/openai-whisper/src/index.ts
index 1a88934b0e..11916012c8 100644
--- a/packages/openai-whisper/src/index.ts
+++ b/packages/openai-whisper/src/index.ts
@@ -1,7 +1,6 @@
export {
OpenAiToCaptionsInput,
OpenAiToCaptionsOutput,
- isOpenAiWhisperJson,
openAiWhisperApiToCaptions,
} from './openai-whisper-api-to-captions';
diff --git a/packages/openai-whisper/src/openai-whisper-api-to-captions.ts b/packages/openai-whisper/src/openai-whisper-api-to-captions.ts
index 920a20cba3..60809ce5aa 100644
--- a/packages/openai-whisper/src/openai-whisper-api-to-captions.ts
+++ b/packages/openai-whisper/src/openai-whisper-api-to-captions.ts
@@ -9,21 +9,6 @@ export type OpenAiToCaptionsOutput = {
captions: Caption[];
};
-export const isOpenAiWhisperJson = (value: unknown): boolean => {
- return (
- typeof value === 'object' &&
- value !== null &&
- !Array.isArray(value) &&
- !('language_code' in value) &&
- 'text' in value &&
- ('task' in value ||
- 'language' in value ||
- 'duration' in value ||
- 'segments' in value ||
- 'words' in value)
- );
-};
-
const escapeRegex = (text: string) => {
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
};
@@ -46,107 +31,36 @@ export const openAiWhisperApiToCaptions = ({
}: OpenAiToCaptionsInput): OpenAiToCaptionsOutput => {
const captions: Caption[] = [];
- if (
- typeof transcription !== 'object' ||
- transcription === null ||
- Array.isArray(transcription)
- ) {
- throw new Error(
- 'Invalid OpenAI Whisper transcription: transcription must be an object.',
- );
- }
-
- if (transcription.task && transcription.task !== 'transcribe') {
- throw new Error(
- `Invalid OpenAI Whisper transcription: The transcription does need to be a "transcribe" task. The input you gave is "task": "${transcription.task}"`,
- );
- }
-
if (!transcription.words) {
- throw new Error(
- 'Invalid OpenAI Whisper transcription: The transcription does need to be been generated with `timestamp_granularities: ["word"]`',
- );
- }
-
- const {words} = transcription as unknown as {words: unknown};
- if (!Array.isArray(words)) {
- throw new Error(
- 'Invalid OpenAI Whisper transcription: words must be an array.',
- );
- }
+ if (transcription.task && transcription.task !== 'transcribe') {
+ throw new Error(
+ `The transcription does need to be a "transcribe" task. The input you gave is "task": "${transcription.task}"`,
+ );
+ }
- if (typeof transcription.text !== 'string') {
throw new Error(
- 'Invalid OpenAI Whisper transcription: text must be a string.',
+ 'The transcription does need to be been generated with `timestamp_granularities: ["word"]`',
);
}
let remainingText = transcription.text;
- let previousStart: number | null = null;
-
- for (let i = 0; i < words.length; i++) {
- const value: unknown = words[i];
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
- throw new Error(
- `Invalid OpenAI Whisper transcription: words[${i}] must be an object.`,
- );
- }
-
- const word = value as Record<string, unknown>;
- if (typeof word.word !== 'string') {
- throw new Error(
- `Invalid OpenAI Whisper transcription: words[${i}].word must be a string.`,
- );
- }
-
- if (
- typeof word.start !== 'number' ||
- !Number.isFinite(word.start) ||
- word.start < 0
- ) {
- throw new Error(
- `Invalid OpenAI Whisper transcription: words[${i}].start must be a finite, non-negative number.`,
- );
- }
-
- if (
- typeof word.end !== 'number' ||
- !Number.isFinite(word.end) ||
- word.end < 0
- ) {
- throw new Error(
- `Invalid OpenAI Whisper transcription: words[${i}].end must be a finite, non-negative number.`,
- );
- }
-
- if (word.end < word.start) {
- throw new Error(
- `Invalid OpenAI Whisper transcription: words[${i}].end must not be earlier than words[${i}].start.`,
- );
- }
-
- if (previousStart !== null && word.start < previousStart) {
- throw new Error(
- `Invalid OpenAI Whisper transcription: words[${i}].start is out of timestamp order.`,
- );
- }
- previousStart = word.start;
+ for (let i = 0; i < transcription.words.length; i++) {
+ const word = transcription.words[i];
const firstWord = i === 0;
// https://github.com/remotion-dev/remotion/issues/5031
- const wordText = firstWord ? word.word.trimStart() : word.word;
if (firstWord) {
- word.word = wordText;
+ word.word = word.word.trimStart();
}
const punctuation = `\\?,\\.\\%\\–\\!\\;\\:\\'\\"\\-\\_\\(\\)\\[\\]\\{\\}\\@\\#\\$\\^\\&\\*\\+\\=\\/\\|\\<\\>\\~\`\\u2018\\u2019\\u02bc\\uff07`;
- const wordToMatch = wordText.replace(new RegExp(`^[${punctuation}]+`), '');
+ const wordToMatch = word.word.replace(new RegExp(`^[${punctuation}]+`), '');
const match = new RegExp(
`^([\\s?${punctuation}]{0,4})${escapeWordForRegex(wordToMatch)}([${punctuation}]{0,3})?`,
).exec(remainingText);
if (!match) {
throw new Error(
- `Unable to parse punctuation from OpenAI Whisper output. Could not find word "${wordText}" in text "${remainingText.slice(0, 100)}". File an issue under https://remotion.dev/issue and post the input for openAiWhisperApiToCaptions() to ask for a fix.`,
+ `Unable to parse punctuation from OpenAI Whisper output. Could not find word "${word.word}" in text "${remainingText.slice(0, 100)}". File an issue under https://remotion.dev/issue and post the input for openAiWhisperApiToCaptions() to ask for a fix.`,
);
}
diff --git a/packages/openai-whisper/src/test/regressions.test.ts b/packages/openai-whisper/src/test/regressions.test.ts
index 07d3584040..2863b69365 100644
--- a/packages/openai-whisper/src/test/regressions.test.ts
+++ b/packages/openai-whisper/src/test/regressions.test.ts
@@ -1,9 +1,6 @@
import {expect, test} from 'bun:test';
import type {OpenAiVerboseTranscription} from '../openai-format';
-import {
- isOpenAiWhisperJson,
- openAiWhisperApiToCaptions,
-} from '../openai-whisper-api-to-captions';
+import {openAiWhisperApiToCaptions} from '../openai-whisper-api-to-captions';
const transcript1: OpenAiVerboseTranscription = {
task: 'transcribe',
@@ -34,15 +31,6 @@ const transcript1: OpenAiVerboseTranscription = {
language: 'english',
};
-test('recognizes OpenAI Whisper JSON', () => {
- expect(
- isOpenAiWhisperJson({language: 'english', text: 'Hello', words: []}),
- ).toBe(true);
- expect(
- isOpenAiWhisperJson({language_code: 'eng', text: 'Hello', words: []}),
- ).toBe(false);
-});
-
const transcript2: OpenAiVerboseTranscription = {
task: 'transcribe',
text: 'in real-time functions.',
@@ -180,69 +168,3 @@ test('Issue 7298 - apostrophe variants', () => {
},
]);
});
-
-test.each([
- {
- name: 'malformed word text',
- words: [{word: 42, start: 0, end: 1}],
- error: 'words[0].word must be a string',
- },
- {
- name: 'malformed word timestamp',
- words: [{word: 'Hello', start: Number.POSITIVE_INFINITY, end: 1}],
- error: 'words[0].start must be a finite, non-negative number',
- },
- {
- name: 'reversed timestamps',
- words: [{word: 'Hello', start: 2, end: 1}],
- error: 'words[0].end must not be earlier than words[0].start',
- },
- {
- name: 'out-of-order words',
- words: [
- {word: 'Hello', start: 2, end: 3},
- {word: 'world', start: 1, end: 2},
- ],
- error: 'words[1].start is out of timestamp order',
- },
-])('rejects $name', ({words, error}) => {
- expect(() =>
- openAiWhisperApiToCaptions({
- transcription: {
- text: 'Hello world',
- words,
- } as unknown as OpenAiVerboseTranscription,
- }),
- ).toThrow(`Invalid OpenAI Whisper transcription: ${error}`);
-});
-
-test('accepts extra provider fields', () => {
- expect(
- openAiWhisperApiToCaptions({
- transcription: {
- duration: 1,
- language: 'english',
- text: 'Hello',
- words: [
- {
- word: 'Hello',
- start: 0,
- end: 1,
- provider_added_field: true,
- },
- ],
- provider_added_field: true,
- } as unknown as OpenAiVerboseTranscription,
- }),
- ).toEqual({
- captions: [
- {
- confidence: null,
- endMs: 1000,
- startMs: 0,
- text: 'Hello',
- timestampMs: 500,
- },
- ],
- });
-});
diff --git a/packages/studio/package.json b/packages/studio/package.json
index 275ef86508..341f8ec78d 100644
--- a/packages/studio/package.json
+++ b/packages/studio/package.json
@@ -31,11 +31,9 @@
"semver": "7.5.3",
"remotion": "workspace:*",
"@remotion/captions": "workspace:*",
- "@remotion/elevenlabs": "workspace:*",
"@remotion/media": "workspace:*",
"@remotion/player": "workspace:*",
"@remotion/media-utils": "workspace:*",
- "@remotion/openai-whisper": "workspace:*",
"@remotion/renderer": "workspace:*",
"@remotion/web-renderer": "workspace:*",
"@remotion/studio-shared": "workspace:*",
diff --git a/packages/studio/src/components/CaptionInspector.tsx b/packages/studio/src/components/CaptionInspector.tsx
index efe65b74e0..faa8e5a040 100644
--- a/packages/studio/src/components/CaptionInspector.tsx
+++ b/packages/studio/src/components/CaptionInspector.tsx
@@ -9,19 +9,11 @@ import {CollapsibleInspectorSectionHeader} from './InspectorPanel/CollapsibleIns
import {InspectorSectionHeader} from './InspectorPanel/common';
import {sectionHeaderEnd} from './InspectorPanel/styles';
import {showNotification} from './Notifications/NotificationCenter';
-import {parseCaptionFile, type ParsedCaptionFile} from './parse-caption-file';
+import {parseCaptionFile} from './parse-caption-file';
const importTooltip = `Import captions
-Supports Remotion Caption[] JSON, ElevenLabs Speech-to-Text JSON, ElevenLabs segmented export JSON, OpenAI Whisper verbose JSON, and SRT subtitles. JSON transcripts need word-level timestamps. Files are processed locally.`;
-
-const formatLabels: Record<ParsedCaptionFile['format'], string> = {
- remotion: 'Remotion JSON',
- elevenlabs: 'ElevenLabs JSON',
- 'elevenlabs-segments': 'ElevenLabs segmented JSON',
- 'openai-whisper': 'OpenAI Whisper JSON',
- srt: 'SRT',
-};
+Supports Remotion Caption[] JSON. Files are processed locally.`;
const readOnlyStatus: React.CSSProperties = {
color: LIGHT_TEXT,
@@ -67,7 +59,7 @@ export const CaptionInspector: React.FC<{
setIsImporting(true);
try {
- const parsed = parseCaptionFile({
+ const importedCaptions = parseCaptionFile({
fileName: file.name,
contents: await file.text(),
});
@@ -75,7 +67,7 @@ export const CaptionInspector: React.FC<{
captions.length > 0 &&
!(await confirm({
title: 'Replace captions?',
- message: `This will replace ${captions.length} existing ${captions.length === 1 ? 'caption' : 'captions'} with ${parsed.captions.length} imported ${parsed.captions.length === 1 ? 'caption' : 'captions'}. You can undo this action.`,
+ message: `This will replace ${captions.length} existing ${captions.length === 1 ? 'caption' : 'captions'} with ${importedCaptions.length} imported ${importedCaptions.length === 1 ? 'caption' : 'captions'}. You can undo this action.`,
confirmLabel: 'Replace captions',
cancelLabel: 'Cancel',
}))
@@ -83,11 +75,11 @@ export const CaptionInspector: React.FC<{
return;
}
- await onReplaceCaptions(parsed.captions);
+ await onReplaceCaptions(importedCaptions);
const captionLabel =
- parsed.captions.length === 1 ? 'caption' : 'captions';
+ importedCaptions.length === 1 ? 'caption' : 'captions';
showNotification(
- `Imported ${parsed.captions.length} ${captionLabel} from ${formatLabels[parsed.format]}. Undo is available.`,
+ `Imported ${importedCaptions.length} ${captionLabel}. Undo is available.`,
4000,
);
} catch (error) {
@@ -112,7 +104,7 @@ export const CaptionInspector: React.FC<{
<>
<input
ref={fileInput}
- accept=".json,.srt"
+ accept=".json"
aria-label="Import captions file"
hidden
onChange={importCaptions}
diff --git a/packages/studio/src/components/parse-caption-file.ts b/packages/studio/src/components/parse-caption-file.ts
index d5cdcde565..5aacaf660b 100644
--- a/packages/studio/src/components/parse-caption-file.ts
+++ b/packages/studio/src/components/parse-caption-file.ts
@@ -1,49 +1,39 @@
import type {Caption} from '@remotion/captions';
-import {parseSrt} from '@remotion/captions';
-import {
- elevenLabsTranscriptToCaptions,
- isElevenLabsTranscriptJson,
- type ElevenLabsTranscript,
-} from '@remotion/elevenlabs';
-import {
- isOpenAiWhisperJson,
- openAiWhisperApiToCaptions,
- type OpenAiVerboseTranscription,
-} from '@remotion/openai-whisper';
-
-export type ParsedCaptionFile = {
- readonly captions: Caption[];
- readonly format:
- | 'remotion'
- | 'elevenlabs'
- | 'elevenlabs-segments'
- | 'openai-whisper'
- | 'srt';
-};
const isObject = (value: unknown): value is Record<string, unknown> => {
return typeof value === 'object' && value !== null && !Array.isArray(value);
};
-const isElevenLabsSegmentedJson = (value: unknown): boolean => {
- return isObject(value) && 'language_code' in value && 'segments' in value;
-};
-
const isFiniteNumber = (value: unknown): value is number => {
return typeof value === 'number' && Number.isFinite(value);
};
-const errorMessage = (error: unknown) => {
- return error instanceof Error ? error.message : String(error);
-};
+export const parseCaptionFile = ({
+ fileName,
+ contents,
+}: {
+ fileName: string;
+ contents: string;
+}): Caption[] => {
+ if (!fileName.toLowerCase().endsWith('.json')) {
+ throw new Error('Unsupported caption file. Choose a .json file.');
+ }
+
+ let parsed: unknown;
+ try {
+ parsed = JSON.parse(contents);
+ } catch (error) {
+ throw new Error(
+ `Invalid JSON: ${error instanceof Error ? error.message : String(error)}`,
+ );
+ }
-const validateCaptions = (value: unknown): Caption[] => {
- if (!Array.isArray(value)) {
- throw new Error('captions must be an array of Remotion captions.');
+ if (!Array.isArray(parsed)) {
+ throw new Error('Expected a Remotion Caption[] JSON array.');
}
let previousStart: number | null = null;
- for (const [index, caption] of value.entries()) {
+ for (const [index, caption] of parsed.entries()) {
const path = `captions[${index}]`;
if (!isObject(caption)) {
throw new Error(`${path} must be an object.`);
@@ -53,20 +43,12 @@ const validateCaptions = (value: unknown): Caption[] => {
throw new Error(`${path}.text must be a string.`);
}
- if (!isFiniteNumber(caption.startMs)) {
- throw new Error(`${path}.startMs must be a finite number.`);
- }
-
- if (caption.startMs < 0) {
- throw new Error(`${path}.startMs must be a non-negative number.`);
- }
-
- if (!isFiniteNumber(caption.endMs)) {
- throw new Error(`${path}.endMs must be a finite number.`);
+ if (!isFiniteNumber(caption.startMs) || caption.startMs < 0) {
+ throw new Error(`${path}.startMs must be a finite, non-negative number.`);
}
- if (caption.endMs < 0) {
- throw new Error(`${path}.endMs must be a non-negative number.`);
+ if (!isFiniteNumber(caption.endMs) || caption.endMs < 0) {
+ throw new Error(`${path}.endMs must be a finite, non-negative number.`);
}
if (caption.endMs < caption.startMs) {
@@ -104,181 +86,5 @@ const validateCaptions = (value: unknown): Caption[] => {
previousStart = caption.startMs;
}
- return value as Caption[];
-};
-
-const convertSegmentedElevenLabsTranscript = (
- transcript: unknown,
-): Caption[] => {
- if (!isObject(transcript) || !Array.isArray(transcript.segments)) {
- throw new Error('transcript.segments must be an array.');
- }
-
- const captions: Caption[] = [];
- let pendingSpacing = '';
- let pendingSpacingStart: number | null = null;
- let previousStart: number | null = null;
-
- for (const [segmentIndex, segment] of transcript.segments.entries()) {
- const segmentPath = `transcript.segments[${segmentIndex}]`;
- if (!isObject(segment)) {
- throw new Error(`${segmentPath} must be an object.`);
- }
-
- if (!Array.isArray(segment.words)) {
- throw new Error(`${segmentPath}.words must be an array.`);
- }
-
- for (const [wordIndex, word] of segment.words.entries()) {
- const path = `${segmentPath}.words[${wordIndex}]`;
- if (!isObject(word)) {
- throw new Error(`${path} must be an object.`);
- }
-
- if (typeof word.text !== 'string') {
- throw new Error(`${path}.text must be a string.`);
- }
-
- if (!isFiniteNumber(word.start_time)) {
- throw new Error(`${path}.start_time must be a finite number.`);
- }
-
- if (word.start_time < 0) {
- throw new Error(`${path}.start_time must be a non-negative number.`);
- }
-
- if (!isFiniteNumber(word.end_time)) {
- throw new Error(`${path}.end_time must be a finite number.`);
- }
-
- if (word.end_time < 0) {
- throw new Error(`${path}.end_time must be a non-negative number.`);
- }
-
- if (word.end_time < word.start_time) {
- throw new Error(
- `${path}.end_time must not be earlier than start_time.`,
- );
- }
-
- if (previousStart !== null && word.start_time < previousStart) {
- throw new Error(`${path}.start_time is out of timestamp order.`);
- }
-
- previousStart = word.start_time;
- if (word.text.trim() === '') {
- pendingSpacingStart ??= word.start_time;
- pendingSpacing += word.text;
- continue;
- }
-
- const startMs =
- captions.length > 0 && pendingSpacingStart !== null
- ? pendingSpacingStart * 1000
- : word.start_time * 1000;
- const endMs = word.end_time * 1000;
- captions.push({
- confidence: null,
- startMs,
- endMs,
- text: `${pendingSpacing}${word.text}`,
- timestampMs: (startMs + endMs) / 2,
- });
- pendingSpacing = '';
- pendingSpacingStart = null;
- }
- }
-
- return captions;
-};
-
-export const parseCaptionFile = ({
- fileName,
- contents,
-}: {
- fileName: string;
- contents: string;
-}): ParsedCaptionFile => {
- const lowerCaseFileName = fileName.toLowerCase();
- if (lowerCaseFileName.endsWith('.srt')) {
- try {
- return {
- captions: validateCaptions(parseSrt({input: contents}).captions),
- format: 'srt',
- };
- } catch (error) {
- throw new Error(`Invalid SRT file: ${errorMessage(error)}`);
- }
- }
-
- if (!lowerCaseFileName.endsWith('.json')) {
- throw new Error('Unsupported caption file. Choose a .json or .srt file.');
- }
-
- let parsed: unknown;
- try {
- parsed = JSON.parse(contents);
- } catch (error) {
- throw new Error(`Invalid JSON: ${errorMessage(error)}`);
- }
-
- if (Array.isArray(parsed)) {
- return {captions: validateCaptions(parsed), format: 'remotion'};
- }
-
- if (!isObject(parsed)) {
- throw new Error(
- 'Unsupported transcription JSON. Expected an object or Caption[] array.',
- );
- }
-
- if (isElevenLabsSegmentedJson(parsed)) {
- try {
- return {
- captions: validateCaptions(
- convertSegmentedElevenLabsTranscript(parsed),
- ),
- format: 'elevenlabs-segments',
- };
- } catch (error) {
- throw new Error(
- `Detected ElevenLabs segmented JSON, but ${errorMessage(error)}`,
- );
- }
- }
-
- if (isElevenLabsTranscriptJson(parsed)) {
- try {
- const {captions} = elevenLabsTranscriptToCaptions({
- transcript: parsed as ElevenLabsTranscript,
- });
- return {
- captions: validateCaptions(captions),
- format: 'elevenlabs',
- };
- } catch (error) {
- throw new Error(`Detected ElevenLabs JSON, but ${errorMessage(error)}`);
- }
- }
-
- if (isOpenAiWhisperJson(parsed)) {
- try {
- const {captions} = openAiWhisperApiToCaptions({
- transcription: parsed as unknown as OpenAiVerboseTranscription,
- });
- return {
- captions: validateCaptions(captions),
- format: 'openai-whisper',
- };
- } catch (error) {
- throw new Error(
- `Detected OpenAI Whisper JSON, but ${errorMessage(error)}`,
- );
- }
- }
-
- const keys = Object.keys(parsed);
- throw new Error(
- `Unsupported transcription JSON. Found top-level keys: ${keys.length === 0 ? '(none)' : keys.join(', ')}.`,
- );
+ return parsed as Caption[];
};
diff --git a/packages/studio/src/test/parse-caption-file.test.ts b/packages/studio/src/test/parse-caption-file.test.ts
index d3c8acb9a1..5265fdf004 100644
--- a/packages/studio/src/test/parse-caption-file.test.ts
+++ b/packages/studio/src/test/parse-caption-file.test.ts
@@ -8,218 +8,76 @@ const parseJson = (value: unknown) => {
});
};
-test('imports supported caption and transcription formats', () => {
- const remotion = parseJson([
- {
- text: 'Hello',
- startMs: 0,
- endMs: 500,
- timestampMs: 250,
- confidence: null,
- },
- ]);
- expect(remotion).toEqual({
- format: 'remotion',
- captions: [
+test('imports Remotion Caption[] JSON', () => {
+ expect(
+ parseJson([
{
text: 'Hello',
startMs: 0,
endMs: 500,
timestampMs: 250,
confidence: null,
+ pageBreakAfter: true,
},
- ],
- });
- expect(parseJson([])).toEqual({format: 'remotion', captions: []});
-
- const elevenLabs = parseJson({
- language_code: 'eng',
- text: 'Hello world',
- words: [
- {text: 'Hello', type: 'word', start: 0, end: 0.4},
- {text: ' ', type: 'spacing', start: 0.4, end: 0.5},
- {text: 'world', type: 'word', start: 0.5, end: 0.9},
- ],
- });
- expect(elevenLabs.format).toBe('elevenlabs');
- expect(elevenLabs.captions.map(({text}) => text)).toEqual([
- 'Hello',
- ' world',
+ ]),
+ ).toEqual([
+ {
+ text: 'Hello',
+ startMs: 0,
+ endMs: 500,
+ timestampMs: 250,
+ confidence: null,
+ pageBreakAfter: true,
+ },
]);
-
- const segmented = parseJson({
- language_code: 'eng',
- segments: [
- {
- text: 'Hello world',
- start_time: 0,
- end_time: 0.9,
- words: [
- {text: 'Hello', start_time: 0, end_time: 0.4},
- {text: ' ', start_time: 0.4, end_time: 0.5},
- {text: 'world', start_time: 0.5, end_time: 0.9},
- ],
- },
- ],
- });
- expect(segmented).toEqual({
- format: 'elevenlabs-segments',
- captions: [
- {
- confidence: null,
- endMs: 400,
- startMs: 0,
- text: 'Hello',
- timestampMs: 200,
- },
- {
- confidence: null,
- endMs: 900,
- startMs: 400,
- text: ' world',
- timestampMs: 650,
- },
- ],
- });
-
- const openAi = parseJson({
- duration: 0.9,
- language: 'english',
- task: 'transcribe',
- text: 'Hello, world!',
- words: [
- {word: 'Hello', start: 0, end: 0.4},
- {word: 'world', start: 0.5, end: 0.9},
- ],
- });
- expect(openAi.format).toBe('openai-whisper');
- expect(openAi.captions.map(({text}) => text)).toEqual(['Hello,', ' world!']);
-
- const srt = parseCaptionFile({
- fileName: 'CAPTIONS.SRT',
- contents: '1\n00:00:00,000 --> 00:00:01,000\nHello world\n',
- });
- expect(srt.format).toBe('srt');
- expect(srt.captions[0]).toEqual({
- confidence: 1,
- endMs: 1000,
- startMs: 0,
- text: 'Hello world',
- timestampMs: 500,
- });
});
test.each([
{
- name: 'invalid JSON',
- fileName: 'captions.json',
- contents: '{',
+ name: 'non-JSON input',
+ value: '{',
error: 'Invalid JSON:',
},
{
- name: 'unknown JSON',
- fileName: 'captions.json',
- contents: JSON.stringify({language: 'english', utterances: []}),
- error: 'Found top-level keys: language, utterances',
- },
- {
- name: 'OpenAI response without words',
- fileName: 'captions.json',
- contents: JSON.stringify({
- duration: 1,
- language: 'english',
- text: 'Hello',
- }),
- error: 'timestamp_granularities: ["word"]',
- },
- {
- name: 'malformed ElevenLabs segmented word',
- fileName: 'captions.json',
- contents: JSON.stringify({
- language_code: 'eng',
- segments: [
- {
- text: 'Hello',
- start_time: 0,
- end_time: 1,
- words: [{text: 'Hello', end_time: 1}],
- },
- ],
- }),
- error: 'segments[0].words[0].start_time must be a finite number',
+ name: 'non-array JSON',
+ value: '{}',
+ error: 'Expected a Remotion Caption[] JSON array',
},
{
- name: 'malformed ElevenLabs API word',
- fileName: 'captions.json',
- contents: JSON.stringify({
- language_code: 'eng',
- text: 'Hello',
- words: [{type: 'word', text: 'Hello', start: 'now', end: 1}],
- }),
- error:
- 'Detected ElevenLabs JSON, but Invalid ElevenLabs transcript: words[0].start',
- },
- {
- name: 'malformed OpenAI Whisper word',
- fileName: 'captions.json',
- contents: JSON.stringify({
- duration: 1,
- language: 'english',
- text: 'Hello',
- words: [{word: 'Hello', start: 0, end: 'later'}],
- }),
- error:
- 'Detected OpenAI Whisper JSON, but Invalid OpenAI Whisper transcription: words[0].end',
- },
- {
- name: 'invalid canonical confidence',
- fileName: 'captions.json',
- contents: JSON.stringify([
- {
- text: 'Hello',
- startMs: 0,
- endMs: 1,
- timestampMs: 0.5,
- confidence: 2,
- },
+ name: 'missing field',
+ value: JSON.stringify([
+ {text: 'Hello', endMs: 500, timestampMs: 250, confidence: null},
]),
- error: 'captions[0].confidence must be between 0 and 1',
+ error: 'captions[0].startMs must be a finite, non-negative number',
},
{
- name: 'reversed canonical timing',
- fileName: 'captions.json',
- contents: JSON.stringify([
+ name: 'reversed timing',
+ value: JSON.stringify([
{
text: 'Hello',
- startMs: 2,
- endMs: 1,
- timestampMs: 1.5,
+ startMs: 500,
+ endMs: 0,
+ timestampMs: 250,
confidence: null,
},
]),
error: 'captions[0].endMs must not be earlier than startMs',
},
{
- name: 'out-of-order canonical captions',
- fileName: 'captions.json',
- contents: JSON.stringify([
- {
- text: 'Later',
- startMs: 2,
- endMs: 3,
- timestampMs: 2.5,
- confidence: null,
- },
+ name: 'invalid confidence',
+ value: JSON.stringify([
{
- text: 'Earlier',
- startMs: 1,
- endMs: 2,
- timestampMs: 1.5,
- confidence: null,
+ text: 'Hello',
+ startMs: 0,
+ endMs: 500,
+ timestampMs: 250,
+ confidence: 2,
},
]),
- error: 'captions[1].startMs is out of timestamp order',
+ error: 'captions[0].confidence must be between 0 and 1',
},
-])('rejects $name', ({fileName, contents, error}) => {
- expect(() => parseCaptionFile({fileName, contents})).toThrow(error);
+])('rejects $name', ({value, error}) => {
+ expect(() =>
+ parseCaptionFile({fileName: 'captions.json', contents: value}),
+ ).toThrow(error);
});