Skip to content

🎥 Make videos programmatically with React

`@remotion/install-whisper-cpp`: Make it install with Whisper.cpp on Windows

Jonny Burgerdde83fcbd395
diff --git a/packages/install-whisper-cpp/src/download-whisper-model.ts b/packages/install-whisper-cpp/src/download-whisper-model.ts
index ca74238cf3..057825193d 100644
--- a/packages/install-whisper-cpp/src/download-whisper-model.ts
+++ b/packages/install-whisper-cpp/src/download-whisper-model.ts
@@ -1,5 +1,6 @@
import fs, {existsSync} from 'fs';
import path from 'path';
+import {downloadFile, type OnProgress} from './download';
const models = [
'tiny',
@@ -16,7 +17,6 @@ const models = [
] as const;
export type WhisperModel = (typeof models)[number];
-export type OnProgress = (downloadedBytes: number, totalBytes: number) => void;
export const getModelPath = (folder: string, model: WhisperModel) => {
return path.join(folder, `ggml-${model}.bin`);
@@ -58,64 +58,11 @@ export const downloadWhisperModel = async ({
const fileStream = fs.createWriteStream(filePath);
- const {body, headers} = await fetch(baseModelUrl);
- const contentLength = headers.get('content-length');
-
- if (body === null) {
- throw new Error('Failed to download whisper model');
- }
-
- if (contentLength === null) {
- throw new Error('Content-Length header not found');
- }
-
- const totalFileSize = parseInt(contentLength, 10);
-
- let downloaded = 0;
- let lastPrinted = 0;
-
- const reader = body.getReader();
-
- // eslint-disable-next-line no-async-promise-executor
- await new Promise<void>(async (resolve, reject) => {
- try {
- // eslint-disable-next-line no-constant-condition
- while (true) {
- const {done, value} = await reader.read();
-
- if (value) {
- downloaded += value.length;
-
- if (
- printOutput &&
- (downloaded - lastPrinted > 1024 * 1024 * 10 ||
- downloaded === totalFileSize)
- ) {
- console.log(
- `Downloaded ${downloaded} of ${contentLength} bytes (${(
- (downloaded / Number(contentLength)) *
- 100
- ).toFixed(2)}%)`,
- );
- lastPrinted = downloaded;
- }
-
- fileStream.write(value, () => {
- onProgress?.(downloaded, totalFileSize);
- if (downloaded === totalFileSize) {
- fileStream.end();
- resolve();
- }
- });
- }
-
- if (done) {
- break;
- }
- }
- } catch (e) {
- reject(e);
- }
+ await downloadFile({
+ fileStream,
+ url: baseModelUrl,
+ printOutput,
+ onProgress,
});
return {alreadyExisted: false};
diff --git a/packages/install-whisper-cpp/src/download.ts b/packages/install-whisper-cpp/src/download.ts
new file mode 100644
index 0000000000..3417d3366f
--- /dev/null
+++ b/packages/install-whisper-cpp/src/download.ts
@@ -0,0 +1,72 @@
+export type OnProgress = (downloadedBytes: number, totalBytes: number) => void;
+
+export const downloadFile = async ({
+ fileStream,
+ url,
+ printOutput,
+ onProgress,
+}: {
+ fileStream: NodeJS.WritableStream;
+ url: string;
+ printOutput: boolean;
+ onProgress: OnProgress | undefined;
+}) => {
+ const {body, headers} = await fetch(url);
+ const contentLength = headers.get('content-length');
+
+ if (body === null) {
+ throw new Error('Failed to download whisper model');
+ }
+
+ if (contentLength === null) {
+ throw new Error('Content-Length header not found');
+ }
+
+ let downloaded = 0;
+ let lastPrinted = 0;
+
+ const totalFileSize = parseInt(contentLength, 10);
+
+ const reader = body.getReader();
+ // eslint-disable-next-line no-async-promise-executor
+ await new Promise<void>(async (resolve, reject) => {
+ try {
+ // eslint-disable-next-line no-constant-condition
+ while (true) {
+ const {done, value} = await reader.read();
+
+ if (value) {
+ downloaded += value.length;
+
+ if (
+ printOutput &&
+ (downloaded - lastPrinted > 1024 * 1024 * 10 ||
+ downloaded === totalFileSize)
+ ) {
+ console.log(
+ `Downloaded ${downloaded} of ${contentLength} bytes (${(
+ (downloaded / Number(contentLength)) *
+ 100
+ ).toFixed(2)}%)`,
+ );
+ lastPrinted = downloaded;
+ }
+
+ fileStream.write(value, () => {
+ onProgress?.(downloaded, totalFileSize);
+ if (downloaded === totalFileSize) {
+ fileStream.end();
+ resolve();
+ }
+ });
+ }
+
+ if (done) {
+ break;
+ }
+ }
+ } catch (e) {
+ reject(e);
+ }
+ });
+};
diff --git a/packages/install-whisper-cpp/src/index.ts b/packages/install-whisper-cpp/src/index.ts
index e7687bfb74..676c8a3c20 100644
--- a/packages/install-whisper-cpp/src/index.ts
+++ b/packages/install-whisper-cpp/src/index.ts
@@ -1,9 +1,6 @@
export {Caption, convertToCaptions} from './convert-to-captions';
-export {
- OnProgress,
- WhisperModel,
- downloadWhisperModel,
-} from './download-whisper-model';
+export type {OnProgress} from './download';
+export {WhisperModel, downloadWhisperModel} from './download-whisper-model';
export {installWhisperCpp} from './install-whisper-cpp';
export type {Language} from './languages';
export {TranscriptionJson, transcribe} from './transcribe';
diff --git a/packages/install-whisper-cpp/src/install-whisper-cpp.ts b/packages/install-whisper-cpp/src/install-whisper-cpp.ts
index 92dfcb14e2..a5588fee33 100644
--- a/packages/install-whisper-cpp/src/install-whisper-cpp.ts
+++ b/packages/install-whisper-cpp/src/install-whisper-cpp.ts
@@ -1,11 +1,9 @@
import type {StdioOptions} from 'child_process';
import fs, {existsSync, rmSync} from 'fs';
import {execSync} from 'node:child_process';
-import {Readable} from 'node:stream';
-import {finished} from 'node:stream/promises';
-import type {ReadableStream} from 'node:stream/web';
import os from 'os';
import path from 'path';
+import {downloadFile} from './download';
const getIsSemVer = (str: string) => {
return /^[\d]{1}\.[\d]{1,2}\.+/.test(str);
@@ -33,14 +31,12 @@ const installForWindows = async ({
const filePath = path.join(process.cwd(), 'whisper-bin-x64.zip');
const fileStream = fs.createWriteStream(filePath);
- const {body} = await fetch(url);
- if (body === null) {
- throw new Error('Failed to download whisper binary');
- }
-
- await finished(
- Readable.fromWeb(body as unknown as ReadableStream).pipe(fileStream),
- );
+ await downloadFile({
+ fileStream,
+ printOutput,
+ url,
+ onProgress: undefined,
+ });
execSync(`Expand-Archive -Force ${filePath} ${to}`, {
shell: 'powershell',