Skip to content

🎥 Make videos programmatically with React

Return punctuation in result

Jonny Burger7890f02206ee
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 5c994239d9..cffc1d91ea 100644
--- a/packages/openai-whisper/src/openai-whisper-api-to-captions.ts
+++ b/packages/openai-whisper/src/openai-whisper-api-to-captions.ts
@@ -9,22 +9,35 @@ export type OpenAiToCaptionsOutput = {
captions: Caption[];
};
-export const openAiWhisperApiToCaptions = (
- input: OpenAiToCaptionsInput,
-): OpenAiToCaptionsOutput => {
+export const openAiWhisperApiToCaptions = ({
+ transcription,
+}: OpenAiToCaptionsInput): OpenAiToCaptionsOutput => {
let captions: Caption[] = [];
- if (!input.transcription.words) {
+ if (!transcription.words || !transcription.segments) {
throw new Error(
- 'The transcription does need to be been generated with `timestamp_granularities: ["word"]`',
+ 'The transcription does need to be been generated with `timestamp_granularities: ["word", "segment"]`',
);
}
- for (const word of input.transcription.words) {
+ let remainingText = transcription.text;
+
+ for (const word of transcription.words) {
+ const match = new RegExp(`^(^.{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.`,
+ );
+ }
+ const foundText = match[0];
+ remainingText = remainingText.slice(foundText.length);
+
captions.push({
confidence: null,
endMs: word.end * 1000,
startMs: word.start * 1000,
- text: word.word,
+ text: foundText,
timestampMs: ((word.start + word.end) / 2) * 1000,
});
}
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 82b54b9634..a85a317931 100644
--- a/packages/openai-whisper/src/test/get-and-convert.test.ts
+++ b/packages/openai-whisper/src/test/get-and-convert.test.ts
@@ -19,49 +19,49 @@ test('Should convert to captions from mock', () => {
confidence: null,
endMs: 7559.999942779541,
startMs: 7039.999961853027,
- text: 'up',
+ text: ' up,',
timestampMs: 7299.999952316284,
},
{
confidence: null,
endMs: 7880.000114440918,
startMs: 7619.999885559082,
- text: 'everybody',
+ text: ' everybody?',
timestampMs: 7750,
},
{
confidence: null,
endMs: 8300.000190734863,
startMs: 8239.999771118164,
- text: 'This',
+ text: ' This',
timestampMs: 8269.999980926514,
},
{
confidence: null,
endMs: 8699.999809265137,
startMs: 8300.000190734863,
- text: 'is',
+ text: ' is',
timestampMs: 8500,
},
{
confidence: null,
endMs: 8899.999618530273,
startMs: 8699.999809265137,
- text: 'Cortland',
+ text: ' Cortland',
timestampMs: 8799.999713897705,
},
{
confidence: null,
endMs: 9159.99984741211,
startMs: 8899.999618530273,
- text: 'from',
+ text: ' from',
timestampMs: 9029.999732971191,
},
{
confidence: null,
endMs: 9560.0004196167,
startMs: 9159.99984741211,
- text: 'IndieHackers',
+ text: ' IndieHackers.',
timestampMs: 9360.000133514404,
},
{
@@ -75,7 +75,7 @@ test('Should convert to captions from mock', () => {
confidence: null,
endMs: 10359.999656677246,
startMs: 10000,
- text: 'and',
+ text: ' and',
timestampMs: 10179.999828338623,
},
]);
@@ -103,9 +103,8 @@ if (!process.env.CI) {
prompt: 'Hello, welcome to my lecture.',
timestamp_granularities: ['word', 'segment'],
});
- console.log(JSON.stringify(transcription));
const {captions} = openAiWhisperApiToCaptions({transcription});
- expect(captions.length).toBe(497);
+ expect(captions.length).toBeGreaterThan(400);
},
{timeout: 60000},
);