nwhisper
Version:
Native Node.js bindings for OpenAI's Whisper using whisper.cpp. High-performance local speech-to-text with custom model support.
112 lines • 5.4 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 node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
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 = [
node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build', 'bin', execName), // Unix CMake
node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build', 'bin', 'Release', execName), // Windows CMake Release
node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build', 'bin', 'Debug', execName), // Windows CMake Debug
node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'build', execName), // Alternative location
node_path_1.default.join(constants_1.WHISPER_CPP_PATH, execName), // Root directory
];
for (const execPath of possiblePaths) {
if (node_fs_1.default.existsSync(execPath)) {
return execPath;
}
}
return ''; // Not found
}
const constructCommand = (filePath, args) => {
const errors = [];
let modelPath;
let modelArg = '';
// Check if model path is provided (absolute path)
if (args.modelPath) {
// Use model path - skip validation
if (!node_fs_1.default.existsSync(args.modelPath)) {
errors.push(`[nwhisper] Error: Model file does not exist: ${args.modelPath}`);
}
modelPath = args.modelPath;
modelArg = args.modelPath;
}
// Check if model directory + model name is provided
else if (args.modelDir && args.modelName) {
// Ensure model directory exists
if (!node_fs_1.default.existsSync(args.modelDir)) {
try {
node_fs_1.default.mkdirSync(args.modelDir, { recursive: true });
console.debug(`[nwhisper] Created model directory: ${args.modelDir}`);
}
catch (_error) {
errors.push(`[nwhisper] Error: Failed to create model directory: ${args.modelDir}`);
}
}
// Use model directory with model name
const modelFile = constants_1.MODEL_OBJECT[args.modelName] || `${args.modelName}.bin`;
modelPath = node_path_1.default.join(args.modelDir, modelFile);
if (!node_fs_1.default.existsSync(modelPath)) {
errors.push(`[nwhisper] Error: Model file does not exist in directory: ${modelPath}`);
}
modelArg = modelPath;
}
// Use standard model validation
else if (args.modelName) {
if (!constants_1.MODELS_LIST.includes(args.modelName)) {
errors.push(`[nwhisper] Error: Enter a valid model name. Available models are: ${constants_1.MODELS_LIST.join(', ')}`);
}
modelPath = node_path_1.default.join(constants_1.WHISPER_CPP_PATH, 'models', constants_1.MODEL_OBJECT[args.modelName]);
if (!node_fs_1.default.existsSync(modelPath)) {
errors.push('[nwhisper] Error: Model file does not exist. Please ensure the model is downloaded and correctly placed.');
}
// Use relative model path from whisper.cpp directory for standard models
modelArg = `./models/${constants_1.MODEL_OBJECT[args.modelName]}`;
}
else {
errors.push('[nwhisper] Error: Provide model name, model path, or model directory with model name');
}
if (errors.length > 0) {
throw new Error(errors.join('\n'));
}
// Get the actual executable path
const executablePath = getExecutablePath();
if (!executablePath) {
throw new Error('[nwhisper] Error: whisper-cli executable not found');
}
// Construct command with proper path escaping
const escapeArg = (arg) => {
if (process.platform === 'win32') {
return `"${arg.replace(/"/g, '\\"')}"`;
}
return `"${arg}"`;
};
const command = `${escapeArg(executablePath)} ${constructOptionsFlags(args)} -l ${args.whisperOptions?.language || 'auto'} -m ${escapeArg(modelArg)} -f ${escapeArg(filePath)}`;
return command;
};
exports.constructCommand = constructCommand;
const constructOptionsFlags = (args) => {
const flags = [
args.whisperOptions?.outputInCsv ? '-ocsv ' : '',
args.whisperOptions?.outputInJson ? '-oj ' : '',
args.whisperOptions?.outputInJsonFull ? '-ojf ' : '',
args.whisperOptions?.outputInLrc ? '-olrc ' : '',
args.whisperOptions?.outputInSrt ? '-osrt ' : '',
args.whisperOptions?.outputInText ? '-otxt ' : '',
args.whisperOptions?.outputInVtt ? '-ovtt ' : '',
args.whisperOptions?.outputInWords ? '-owts ' : '',
args.whisperOptions?.translateToEnglish ? '-tr ' : '',
args.whisperOptions?.wordTimestamps ? '-ml 1 ' : '',
args.whisperOptions?.timestamps_length ? `-ml ${args.whisperOptions.timestamps_length} ` : '',
args.whisperOptions?.splitOnWord ? '-sow true ' : '',
].join('');
return flags.trim();
};
//# sourceMappingURL=WhisperHelper.js.map