Skip to content

🎥 Make videos programmatically with React

`openai-whisper`: Fix crash in postprocessing

Jonny Burger604a88b01be8
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 84febc56ca..44f273cb5c 100644
--- a/packages/openai-whisper/src/openai-whisper-api-to-captions.ts
+++ b/packages/openai-whisper/src/openai-whisper-api-to-captions.ts
@@ -22,9 +22,9 @@ export const openAiWhisperApiToCaptions = ({
let remainingText = transcription.text;
for (const word of transcription.words) {
- const match = new RegExp(`^(^.{0,4})${word.word}([\\?,\\.]{0,3})?`).exec(
- remainingText,
- );
+ const match = new RegExp(
+ `^([\\s\\.]{0,4})${word.word}([\\?,\\.]{0,3})?`,
+ ).exec(remainingText);
if (!match) {
throw new Error(
`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 to ask for a fix.`,
diff --git a/packages/openai-whisper/src/test/partial-word.test.ts b/packages/openai-whisper/src/test/partial-word.test.ts
new file mode 100644
index 0000000000..49417f90f6
--- /dev/null
+++ b/packages/openai-whisper/src/test/partial-word.test.ts
@@ -0,0 +1,67 @@
+import {expect, test} from 'bun:test';
+import type {OpenAiVerboseTranscription} from '../openai-format';
+import {openAiWhisperApiToCaptions} from '../openai-whisper-api-to-captions';
+
+export const transcript: OpenAiVerboseTranscription = {
+ task: 'transcribe',
+ text: 'which is a massive.',
+ words: [
+ {
+ end: 12.180000305175781,
+ word: 'which',
+ start: 12.079999923706055,
+ },
+ {
+ end: 12.300000190734863,
+ word: 'is',
+ start: 12.180000305175781,
+ },
+ {
+ end: 12.619999885559082,
+ word: 'a',
+ start: 12.300000190734863,
+ },
+ {
+ end: 12.779999732971191,
+ word: 'massive',
+ start: 12.619999885559082,
+ },
+ ],
+ duration: 12.779999732971191,
+ language: 'english',
+};
+
+test('Partial word test', () => {
+ expect(
+ openAiWhisperApiToCaptions({transcription: transcript}).captions,
+ ).toEqual([
+ {
+ confidence: null,
+ endMs: 12180.000305175781,
+ startMs: 12079.999923706055,
+ text: 'which',
+ timestampMs: 12130.000114440918,
+ },
+ {
+ confidence: null,
+ endMs: 12300.000190734863,
+ startMs: 12180.000305175781,
+ text: ' is',
+ timestampMs: 12240.000247955322,
+ },
+ {
+ confidence: null,
+ endMs: 12619.999885559082,
+ startMs: 12300.000190734863,
+ text: ' a',
+ timestampMs: 12460.000038146973,
+ },
+ {
+ confidence: null,
+ endMs: 12779.999732971191,
+ startMs: 12619.999885559082,
+ text: ' massive.',
+ timestampMs: 12699.999809265137,
+ },
+ ]);
+});