Skip to content

🎥 Make videos programmatically with React

`@remotion/install-whisper-cpp`: `onProgress` callback for `transcribe()` API

Jonny Burgerffb6a705d278
diff --git a/packages/install-whisper-cpp/src/transcribe.ts b/packages/install-whisper-cpp/src/transcribe.ts
index 750d42b1a7..2401579356 100644
--- a/packages/install-whisper-cpp/src/transcribe.ts
+++ b/packages/install-whisper-cpp/src/transcribe.ts
@@ -89,6 +89,8 @@ const readJson = async (jsonPath: string) => {
return JSON.parse(data);
};
+type TranscribeOnProgress = (progress: number) => void;
+
const transcribeToTemporaryFile = async ({
fileToTranscribe,
whisperPath,
@@ -101,6 +103,7 @@ const transcribeToTemporaryFile = async ({
tokensPerItem,
language,
signal,
+ onProgress,
}: {
fileToTranscribe: string;
whisperPath: string;
@@ -111,8 +114,9 @@ const transcribeToTemporaryFile = async ({
tokenLevelTimestamps: boolean;
printOutput: boolean;
tokensPerItem: number | null;
- language?: Language | null;
+ language: Language | null;
signal: AbortSignal | null;
+ onProgress: TranscribeOnProgress | null;
}): Promise<{
outputPath: string;
}> => {
@@ -138,6 +142,7 @@ const transcribeToTemporaryFile = async ({
'-ojf', // Output full JSON
tokenLevelTimestamps ? ['--dtw', model] : null,
model ? [`-m`, `${modelPath}`] : null,
+ ['-pp'], // print progress
translate ? '-tr' : null,
language ? ['-l', language.toLowerCase()] : null,
]
@@ -155,6 +160,12 @@ const transcribeToTemporaryFile = async ({
const onData = (data: Buffer) => {
const str = data.toString('utf-8');
+ const hasProgress = str.includes('progress =');
+ if (hasProgress) {
+ const progress = parseFloat(str.split('progress =')[1].trim());
+ onProgress?.(progress / 100);
+ }
+
output += str;
// Sometimes it hangs here
@@ -186,6 +197,7 @@ const transcribeToTemporaryFile = async ({
if (existsSync(predictedPath)) {
resolve(predictedPath);
+ onProgress?.(1);
return;
}
@@ -218,6 +230,7 @@ export const transcribe = async <HasTokenLevelTimestamps extends boolean>({
tokensPerItem,
language,
signal,
+ onProgress,
}: {
inputPath: string;
whisperPath: string;
@@ -229,6 +242,7 @@ export const transcribe = async <HasTokenLevelTimestamps extends boolean>({
tokensPerItem?: true extends HasTokenLevelTimestamps ? never : number | null;
language?: Language | null;
signal?: AbortSignal;
+ onProgress?: TranscribeOnProgress;
}): Promise<TranscriptionJson<HasTokenLevelTimestamps>> => {
if (!existsSync(whisperPath)) {
throw new Error(
@@ -258,8 +272,9 @@ export const transcribe = async <HasTokenLevelTimestamps extends boolean>({
tokenLevelTimestamps,
printOutput,
tokensPerItem: tokenLevelTimestamps ? 1 : tokensPerItem ?? 1,
- language,
+ language: language ?? null,
signal: signal ?? null,
+ onProgress: onProgress ?? null,
});
const json = (await readJson(
diff --git a/packages/it-tests/download-whisper.ts b/packages/it-tests/download-whisper.ts
index c9ba52fd4e..c4565e4d61 100644
--- a/packages/it-tests/download-whisper.ts
+++ b/packages/it-tests/download-whisper.ts
@@ -25,6 +25,8 @@ const transcription = await transcribe({
whisperPath: to,
model: "medium.en",
tokenLevelTimestamps: true,
+ printOutput: false,
+ onProgress: (progress) => {
+ console.log(progress);
+ },
});
-
-console.log(transcription);