UNPKG

@remotion/install-whisper-cpp

Version:

Helpers for installing and using Whisper.cpp

169 lines (168 loc) 6.62 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.installWhisperCpp = exports.getWhisperExecutablePath = void 0; /* eslint-disable no-console */ const child_process_1 = require("child_process"); const fs_1 = __importStar(require("fs")); const os_1 = __importDefault(require("os")); const path_1 = __importDefault(require("path")); const download_1 = require("./download"); const utils_1 = require("./utils"); const getIsSemVer = (str) => { return /^[\d]{1}\.[\d]{1,2}\.+/.test(str); }; const execute = ({ printOutput, signal, cwd, shell, args, bin, }) => { const stdio = printOutput ? 'inherit' : 'ignore'; return new Promise((resolve, reject) => { const child = (0, child_process_1.spawn)(bin, args, { stdio, signal: signal !== null && signal !== void 0 ? signal : undefined, cwd: cwd !== null && cwd !== void 0 ? cwd : undefined, shell: shell !== null && shell !== void 0 ? shell : undefined, }); child.on('exit', (code, exitSignal) => { if (code !== 0) { reject(new Error(`Error while executing ${bin} ${args.join(' ')}. Exit code: ${code}, signal: ${exitSignal}`)); return; } resolve(); }); }); }; const installForWindows = async ({ version, to, printOutput, signal, }) => { if (!getIsSemVer(version)) { throw new Error(`Non-semantic version provided. Only releases of Whisper.cpp are supported on Windows (e.g., 1.5.4). Provided version: ${version}. See https://www.remotion.dev/docs/install-whisper-cpp/install-whisper-cpp#version for more information.`); } const url = version === '1.5.5' ? 'https://remotion-ffmpeg-binaries.s3.eu-central-1.amazonaws.com/whisper-bin-x64-1-5-5.zip' : `https://github.com/ggerganov/whisper.cpp/releases/download/v${version}/whisper-bin-x64.zip`; const filePath = path_1.default.join(process.cwd(), 'whisper-bin-x64.zip'); const fileStream = fs_1.default.createWriteStream(filePath); await (0, download_1.downloadFile)({ fileStream, printOutput, url, onProgress: undefined, signal, }); await execute({ shell: 'powershell', printOutput, signal, cwd: null, bin: 'Expand-Archive', args: ['-Force', filePath, to], }); (0, fs_1.rmSync)(filePath); }; const installWhisperForUnix = async ({ version, to, printOutput, signal, }) => { await execute({ bin: 'git', args: ['clone', 'https://github.com/ggerganov/whisper.cpp.git', to], printOutput, signal, cwd: null, shell: null, }); const ref = getIsSemVer(version) ? `v${version}` : version; await execute({ bin: 'git', args: ['checkout', ref], printOutput, cwd: to, signal, shell: null, }); await execute({ args: [], bin: 'make', cwd: to, signal, printOutput, shell: null, }); }; const getWhisperExecutablePath = (whisperPath, whisperCppVersion) => { // INFO: 'main.exe' is deprecated. let cppBin = ['main']; let cppFolder = []; if ((0, utils_1.compareVersions)(whisperCppVersion, '1.7.4') >= 0) { cppBin = ['whisper-cli']; cppFolder = ['build', 'bin']; } return os_1.default.platform() === 'win32' ? path_1.default.join(path_1.default.resolve(process.cwd(), whisperPath), ...cppFolder, `${cppBin}.exe`) : path_1.default.join(path_1.default.resolve(process.cwd(), whisperPath), ...cppFolder, `./${cppBin}`); }; exports.getWhisperExecutablePath = getWhisperExecutablePath; const installWhisperCpp = async ({ version, to, printOutput = true, signal, }) => { if ((0, fs_1.existsSync)(to)) { if (!(0, fs_1.existsSync)((0, exports.getWhisperExecutablePath)(to, version))) { if (printOutput) { throw new Error(`Whisper folder ${to} exists but the executable (${(0, exports.getWhisperExecutablePath)(to, version)}) is missing. Delete ${to} and try again.`); } return Promise.resolve({ alreadyExisted: false }); } if (printOutput) { console.log(`Whisper already exists at ${to}`); } return Promise.resolve({ alreadyExisted: true }); } if (process.platform === 'darwin' || process.platform === 'linux') { await installWhisperForUnix({ version, to, printOutput, signal: signal !== null && signal !== void 0 ? signal : null, }); return Promise.resolve({ alreadyExisted: false }); } if (process.platform === 'win32') { await installForWindows({ version, to, printOutput, signal: signal !== null && signal !== void 0 ? signal : null, }); return Promise.resolve({ alreadyExisted: false }); } throw new Error(`Unsupported platform: ${process.platform}`); }; exports.installWhisperCpp = installWhisperCpp;