Skip to content

πŸŽ₯ Make videos programmatically with React

`@remotion/openai-whisper`: Fix matching words which contain characters with special RegEx meaning

Jonny Burger7412f5bf77af
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 526ed1c076..3eff7d1d9e 100644
--- a/packages/openai-whisper/src/openai-whisper-api-to-captions.ts
+++ b/packages/openai-whisper/src/openai-whisper-api-to-captions.ts
@@ -23,7 +23,7 @@ export const openAiWhisperApiToCaptions = ({
for (const word of transcription.words) {
const match = new RegExp(
- `^([\\s\\.]{0,4})${word.word}([\\?,\\.\\%\\–\\!\\;\\:\\'\\"\\-\\_\\(\\)\\[\\]\\{\\}\\@\\#\\$\\^\\&\\*\\+\\=\\/\\|\\<\\>\\~\`]{0,3})?`,
+ `^([\\s\\.]{0,4})${word.word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}([\\?,\\.\\%\\–\\!\\;\\:\\'\\"\\-\\_\\(\\)\\[\\]\\{\\}\\@\\#\\$\\^\\&\\*\\+\\=\\/\\|\\<\\>\\~\`]{0,3})?`,
).exec(remainingText);
if (!match) {
throw new Error(
diff --git a/packages/openai-whisper/src/test/get-and-convert.test.ts b/packages/openai-whisper/src/test/get-and-convert.test.ts
index 2ffaa36f92..ac45d86ef0 100644
--- a/packages/openai-whisper/src/test/get-and-convert.test.ts
+++ b/packages/openai-whisper/src/test/get-and-convert.test.ts
@@ -95,7 +95,7 @@ if (!process.env.CI) {
'..',
'template-audiogram',
'public',
- 'audio.mp3',
+ 'audio.wav',
),
),
model: 'whisper-1',
diff --git a/packages/openai-whisper/src/test/special-chars.test.ts b/packages/openai-whisper/src/test/special-chars.test.ts
new file mode 100644
index 0000000000..db258708fd
--- /dev/null
+++ b/packages/openai-whisper/src/test/special-chars.test.ts
@@ -0,0 +1,141 @@
+import {expect, test} from 'bun:test';
+import type {OpenAiVerboseTranscription} from '../openai-format';
+import {openAiWhisperApiToCaptions} from '../openai-whisper-api-to-captions';
+
+const text = ' $500 that you can spend on a computer right now.';
+
+const transcript: OpenAiVerboseTranscription = {
+ task: 'transcribe',
+ text,
+ words: [
+ {
+ start: 1,
+ end: 2,
+ word: '$500',
+ },
+ {
+ start: 3,
+ end: 4,
+ word: 'that',
+ },
+ {
+ start: 5,
+ end: 6,
+ word: 'you',
+ },
+ {
+ start: 7,
+ end: 8,
+ word: 'can',
+ },
+ {
+ start: 9,
+ end: 10,
+ word: 'spend',
+ },
+ {
+ start: 11,
+ end: 12,
+ word: 'on',
+ },
+ {
+ start: 13,
+ end: 14,
+ word: 'a',
+ },
+ {
+ start: 15,
+ end: 16,
+ word: 'computer',
+ },
+ {
+ start: 17,
+ end: 18,
+ word: 'right',
+ },
+ {
+ start: 19,
+ end: 20,
+ word: 'now.',
+ },
+ ],
+ duration: 4,
+ language: 'english',
+};
+
+test('should not crash', () => {
+ expect(openAiWhisperApiToCaptions({transcription: transcript})).toEqual({
+ captions: [
+ {
+ confidence: null,
+ endMs: 2000,
+ startMs: 1000,
+ text: ' $500',
+ timestampMs: 1500,
+ },
+ {
+ confidence: null,
+ endMs: 4000,
+ startMs: 3000,
+ text: ' that',
+ timestampMs: 3500,
+ },
+ {
+ confidence: null,
+ endMs: 6000,
+ startMs: 5000,
+ text: ' you',
+ timestampMs: 5500,
+ },
+ {
+ confidence: null,
+ endMs: 8000,
+ startMs: 7000,
+ text: ' can',
+ timestampMs: 7500,
+ },
+ {
+ confidence: null,
+ endMs: 10000,
+ startMs: 9000,
+ text: ' spend',
+ timestampMs: 9500,
+ },
+ {
+ confidence: null,
+ endMs: 12000,
+ startMs: 11000,
+ text: ' on',
+ timestampMs: 11500,
+ },
+ {
+ confidence: null,
+ endMs: 14000,
+ startMs: 13000,
+ text: ' a',
+ timestampMs: 13500,
+ },
+ {
+ confidence: null,
+ endMs: 16000,
+ startMs: 15000,
+ text: ' computer',
+ timestampMs: 15500,
+ },
+ {
+ confidence: null,
+ endMs: 18000,
+ startMs: 17000,
+ text: ' right',
+ timestampMs: 17500,
+ },
+ {
+ confidence: null,
+ endMs: 20000,
+ startMs: 19000,
+ text: ' now.',
+ timestampMs: 19500,
+ },
+ ],
+ });
+});