@snowdigital/whisper-node
Version:
NodeJS interface for whisper.cpp
84 lines • 4.9 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.constructCommand = void 0;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const constants_1 = require("./constants");
// Get the correct executable path based on platform and build system
function getExecutablePath() {
const execName = process.platform === 'win32' ? 'whisper-cli.exe' : 'whisper-cli';
// Check common CMake build locations
const possiblePaths = [
path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build', 'bin', execName), // Unix CMake
path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build', 'bin', 'Release', execName), // Windows CMake Release
path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build', 'bin', 'Debug', execName), // Windows CMake Debug
path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build', execName), // Alternative location
path_1.default.join(constants_1.WHISPER_CPP_PATH, execName), // Root directory
];
for (const execPath of possiblePaths) {
if (fs_1.default.existsSync(execPath)) {
return execPath;
}
}
return ''; // Not found
}
const constructCommand = (filePath, args) => {
var _a;
const errors = [];
if (!args.modelName) {
errors.push('[Whisper-node] Error: Provide model name');
}
if (!constants_1.MODELS_LIST.includes(args.modelName)) {
errors.push(`[Whisper-node] Error: Enter a valid model name. Available models are: ${constants_1.MODELS_LIST.join(', ')}`);
}
const modelPath = path_1.default.join(constants_1.WHISPER_CPP_PATH, 'models', constants_1.MODEL_OBJECT[args.modelName]);
if (!fs_1.default.existsSync(modelPath)) {
errors.push('[Whisper-node] Error: Model file does not exist. Please ensure the model is downloaded and correctly placed.');
}
if (errors.length > 0) {
throw new Error(errors.join('\n'));
}
// Get the actual executable path
const executablePath = getExecutablePath();
if (!executablePath) {
throw new Error('[Whisper-node] Error: whisper-cli executable not found');
}
const modelName = constants_1.MODEL_OBJECT[args.modelName];
// Construct command with proper path escaping
const escapeArg = (arg) => {
if (process.platform === 'win32') {
return `"${arg.replace(/"/g, '\\"')}"`;
}
return `"${arg}"`;
};
// Use relative model path from whisper.cpp directory
const modelArg = `./models/${modelName}`;
const command = `${escapeArg(executablePath)} ${constructOptionsFlags(args)} -l ${((_a = args.whisperOptions) === null || _a === void 0 ? void 0 : _a.language) || 'auto'} -m ${escapeArg(modelArg)} -f ${escapeArg(filePath)}`;
return command;
};
exports.constructCommand = constructCommand;
const constructOptionsFlags = (args) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
const flags = [
((_a = args.whisperOptions) === null || _a === void 0 ? void 0 : _a.noTimestamps) ? '--no-timestamps ' : '',
((_b = args.whisperOptions) === null || _b === void 0 ? void 0 : _b.noPrints) ? '--no-prints ' : '',
((_c = args.whisperOptions) === null || _c === void 0 ? void 0 : _c.printColors) ? '--print-colors ' : '',
((_d = args.whisperOptions) === null || _d === void 0 ? void 0 : _d.outputInCsv) ? '-ocsv ' : '',
((_e = args.whisperOptions) === null || _e === void 0 ? void 0 : _e.outputInJson) ? '-oj ' : '',
((_f = args.whisperOptions) === null || _f === void 0 ? void 0 : _f.outputInJsonFull) ? '-ojf ' : '',
((_g = args.whisperOptions) === null || _g === void 0 ? void 0 : _g.outputInLrc) ? '-olrc ' : '',
((_h = args.whisperOptions) === null || _h === void 0 ? void 0 : _h.outputInSrt) ? '-osrt ' : '',
((_j = args.whisperOptions) === null || _j === void 0 ? void 0 : _j.outputInText) ? '-otxt ' : '',
((_k = args.whisperOptions) === null || _k === void 0 ? void 0 : _k.outputInVtt) ? '-ovtt ' : '',
((_l = args.whisperOptions) === null || _l === void 0 ? void 0 : _l.outputInWords) ? '-owts ' : '',
((_m = args.whisperOptions) === null || _m === void 0 ? void 0 : _m.translateToEnglish) ? '-tr ' : '',
((_o = args.whisperOptions) === null || _o === void 0 ? void 0 : _o.wordTimestamps) ? '-ml 1 ' : '',
((_p = args.whisperOptions) === null || _p === void 0 ? void 0 : _p.timestamps_length) ? `-ml ${args.whisperOptions.timestamps_length} ` : '',
((_q = args.whisperOptions) === null || _q === void 0 ? void 0 : _q.splitOnWord) ? '-sow true ' : '',
].join('');
return flags.trim();
};
//# sourceMappingURL=WhisperHelper.js.map