Skip to content

πŸŽ₯ Make videos programmatically with React

`@remotion/openai-whisper`: Make output of a foreign API work

Jonny Burger9c5134000e41
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 a8cae146fa..b1f5e01f8f 100644
--- a/packages/openai-whisper/src/openai-whisper-api-to-captions.ts
+++ b/packages/openai-whisper/src/openai-whisper-api-to-captions.ts
@@ -28,7 +28,14 @@ export const openAiWhisperApiToCaptions = ({
let remainingText = transcription.text;
- for (const word of transcription.words) {
+ 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
+ if (firstWord) {
+ word.word = word.word.trimStart();
+ }
+
const punctuation = `\\?,\\.\\%\\–\\!\\;\\:\\'\\"\\-\\_\\(\\)\\[\\]\\{\\}\\@\\#\\$\\^\\&\\*\\+\\=\\/\\|\\<\\>\\~\``;
const match = new RegExp(
`^([\\s${punctuation}]{0,4})${word.word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}([${punctuation}]{0,3})?`,
diff --git a/packages/openai-whisper/src/test/foreign-api.test.ts b/packages/openai-whisper/src/test/foreign-api.test.ts
new file mode 100644
index 0000000000..491482ba06
--- /dev/null
+++ b/packages/openai-whisper/src/test/foreign-api.test.ts
@@ -0,0 +1,279 @@
+import {expect, test} from 'bun:test';
+import type {OpenAiVerboseTranscription} from '../openai-format';
+import {openAiWhisperApiToCaptions} from '../openai-whisper-api-to-captions';
+
+test('foreign API (https://github.com/remotion-dev/remotion/issues/5031)', () => {
+ const res: OpenAiVerboseTranscription = {
+ text: "Hello world. It's great to finally be here, surrounded by the endless possibilities that this universe has to offer, and I'm excited to see what the future holds for all of us.",
+ task: 'transcribe',
+ language: '',
+ duration: 10.625,
+ words: [
+ {word: ' Hello', start: 0, end: 0.46000000834465027},
+ {word: ' world.', start: 0.46000000834465027, end: 0.9800000190734863},
+ {word: " It's", start: 0.9800000190734863, end: 1.5800000429153442},
+ {word: ' great', start: 1.5800000429153442, end: 1.7999999523162842},
+ {word: ' to', start: 1.7999999523162842, end: 1.9600000381469727},
+ {word: ' finally', start: 1.9600000381469727, end: 2.319999933242798},
+ {word: ' be', start: 2.319999933242798, end: 2.5799999237060547},
+ {word: ' here,', start: 2.5799999237060547, end: 3},
+ {word: ' surrounded', start: 3, end: 3.5999999046325684},
+ {word: ' by', start: 3.5999999046325684, end: 4.019999980926514},
+ {word: ' the', start: 4.019999980926514, end: 4.179999828338623},
+ {word: ' endless', start: 4.179999828338623, end: 4.519999980926514},
+ {
+ word: ' possibilities',
+ start: 4.519999980926514,
+ end: 5.119999885559082,
+ },
+ {word: ' that', start: 5.119999885559082, end: 5.639999866485596},
+ {word: ' this', start: 5.639999866485596, end: 5.820000171661377},
+ {word: ' universe', start: 5.820000171661377, end: 6.179999828338623},
+ {word: ' has', start: 6.179999828338623, end: 6.519999980926514},
+ {word: ' to', start: 6.519999980926514, end: 6.659999847412109},
+ {word: ' offer,', start: 6.659999847412109, end: 7.179999828338623},
+ {word: ' and', start: 7.179999828338623, end: 7.679999828338623},
+ {word: " I'm", start: 7.679999828338623, end: 7.820000171661377},
+ {word: ' excited', start: 7.820000171661377, end: 8.180000305175781},
+ {word: ' to', start: 8.180000305175781, end: 8.420000076293945},
+ {word: ' see', start: 8.420000076293945, end: 8.600000381469727},
+ {word: ' what', start: 8.600000381469727, end: 8.739999771118164},
+ {word: ' the', start: 8.739999771118164, end: 8.859999656677246},
+ {word: ' future', start: 8.859999656677246, end: 9.180000305175781},
+ {word: ' holds', start: 9.180000305175781, end: 9.520000457763672},
+ {word: ' for', start: 9.520000457763672, end: 9.779999732971191},
+ {word: ' all', start: 9.779999732971191, end: 9.979999542236328},
+ {word: ' of', start: 9.979999542236328, end: 10.079999923706055},
+ {word: ' us.', start: 10.079999923706055, end: 10.34000015258789},
+ ],
+ };
+
+ expect(openAiWhisperApiToCaptions({transcription: res})).toEqual({
+ captions: [
+ {
+ confidence: null,
+ endMs: 460.00000834465027,
+ startMs: 0,
+ text: 'Hello',
+ timestampMs: 230.00000417232513,
+ },
+ {
+ confidence: null,
+ endMs: 980.0000190734863,
+ startMs: 460.00000834465027,
+ text: ' world.',
+ timestampMs: 720.0000137090683,
+ },
+ {
+ confidence: null,
+ endMs: 1580.0000429153442,
+ startMs: 980.0000190734863,
+ text: " It's",
+ timestampMs: 1280.0000309944153,
+ },
+ {
+ confidence: null,
+ endMs: 1799.9999523162842,
+ startMs: 1580.0000429153442,
+ text: ' great',
+ timestampMs: 1689.9999976158142,
+ },
+ {
+ confidence: null,
+ endMs: 1960.0000381469727,
+ startMs: 1799.9999523162842,
+ text: ' to',
+ timestampMs: 1879.9999952316284,
+ },
+ {
+ confidence: null,
+ endMs: 2319.999933242798,
+ startMs: 1960.0000381469727,
+ text: ' finally',
+ timestampMs: 2139.9999856948853,
+ },
+ {
+ confidence: null,
+ endMs: 2579.9999237060547,
+ startMs: 2319.999933242798,
+ text: ' be',
+ timestampMs: 2449.9999284744263,
+ },
+ {
+ confidence: null,
+ endMs: 3000,
+ startMs: 2579.9999237060547,
+ text: ' here,',
+ timestampMs: 2789.9999618530273,
+ },
+ {
+ confidence: null,
+ endMs: 3599.9999046325684,
+ startMs: 3000,
+ text: ' surrounded',
+ timestampMs: 3299.999952316284,
+ },
+ {
+ confidence: null,
+ endMs: 4019.9999809265137,
+ startMs: 3599.9999046325684,
+ text: ' by',
+ timestampMs: 3809.999942779541,
+ },
+ {
+ confidence: null,
+ endMs: 4179.999828338623,
+ startMs: 4019.9999809265137,
+ text: ' the',
+ timestampMs: 4099.999904632568,
+ },
+ {
+ confidence: null,
+ endMs: 4519.999980926514,
+ startMs: 4179.999828338623,
+ text: ' endless',
+ timestampMs: 4349.999904632568,
+ },
+ {
+ confidence: null,
+ endMs: 5119.999885559082,
+ startMs: 4519.999980926514,
+ text: ' possibilities',
+ timestampMs: 4819.999933242798,
+ },
+ {
+ confidence: null,
+ endMs: 5639.999866485596,
+ startMs: 5119.999885559082,
+ text: ' that',
+ timestampMs: 5379.999876022339,
+ },
+ {
+ confidence: null,
+ endMs: 5820.000171661377,
+ startMs: 5639.999866485596,
+ text: ' this',
+ timestampMs: 5730.000019073486,
+ },
+ {
+ confidence: null,
+ endMs: 6179.999828338623,
+ startMs: 5820.000171661377,
+ text: ' universe',
+ timestampMs: 6000,
+ },
+ {
+ confidence: null,
+ endMs: 6519.999980926514,
+ startMs: 6179.999828338623,
+ text: ' has',
+ timestampMs: 6349.999904632568,
+ },
+ {
+ confidence: null,
+ endMs: 6659.999847412109,
+ startMs: 6519.999980926514,
+ text: ' to',
+ timestampMs: 6589.9999141693115,
+ },
+ {
+ confidence: null,
+ endMs: 7179.999828338623,
+ startMs: 6659.999847412109,
+ text: ' offer,',
+ timestampMs: 6919.999837875366,
+ },
+ {
+ confidence: null,
+ endMs: 7679.999828338623,
+ startMs: 7179.999828338623,
+ text: ' and',
+ timestampMs: 7429.999828338623,
+ },
+ {
+ confidence: null,
+ endMs: 7820.000171661377,
+ startMs: 7679.999828338623,
+ text: " I'm",
+ timestampMs: 7750,
+ },
+ {
+ confidence: null,
+ endMs: 8180.000305175781,
+ startMs: 7820.000171661377,
+ text: ' excited',
+ timestampMs: 8000.000238418579,
+ },
+ {
+ confidence: null,
+ endMs: 8420.000076293945,
+ startMs: 8180.000305175781,
+ text: ' to',
+ timestampMs: 8300.000190734863,
+ },
+ {
+ confidence: null,
+ endMs: 8600.000381469727,
+ startMs: 8420.000076293945,
+ text: ' see',
+ timestampMs: 8510.000228881836,
+ },
+ {
+ confidence: null,
+ endMs: 8739.999771118164,
+ startMs: 8600.000381469727,
+ text: ' what',
+ timestampMs: 8670.000076293945,
+ },
+ {
+ confidence: null,
+ endMs: 8859.999656677246,
+ startMs: 8739.999771118164,
+ text: ' the',
+ timestampMs: 8799.999713897705,
+ },
+ {
+ confidence: null,
+ endMs: 9180.000305175781,
+ startMs: 8859.999656677246,
+ text: ' future',
+ timestampMs: 9019.999980926514,
+ },
+ {
+ confidence: null,
+ endMs: 9520.000457763672,
+ startMs: 9180.000305175781,
+ text: ' holds',
+ timestampMs: 9350.000381469727,
+ },
+ {
+ confidence: null,
+ endMs: 9779.999732971191,
+ startMs: 9520.000457763672,
+ text: ' for',
+ timestampMs: 9650.000095367432,
+ },
+ {
+ confidence: null,
+ endMs: 9979.999542236328,
+ startMs: 9779.999732971191,
+ text: ' all',
+ timestampMs: 9879.99963760376,
+ },
+ {
+ confidence: null,
+ endMs: 10079.999923706055,
+ startMs: 9979.999542236328,
+ text: ' of',
+ timestampMs: 10029.999732971191,
+ },
+ {
+ confidence: null,
+ endMs: 10340.00015258789,
+ startMs: 10079.999923706055,
+ text: ' us.',
+ timestampMs: 10210.000038146973,
+ },
+ ],
+ });
+});