nwhisper
Version:
Native Node.js bindings for OpenAI's Whisper using whisper.cpp. High-performance local speech-to-text with custom model support.
53 lines • 2.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.transcribe = transcribe;
exports.nodewhisper = nodewhisper;
const node_fs_1 = __importDefault(require("node:fs"));
const autoDownloadModel_1 = __importDefault(require("./autoDownloadModel"));
const utils_1 = require("./utils");
const WhisperHelper_1 = require("./WhisperHelper");
const whisper_1 = require("./whisper");
// Main transcription function
async function transcribe(filePath, options) {
const { removeWavFileAfterTranscription = false, logger = console } = options;
try {
if (options.autoDownloadModelName) {
logger.debug(`[nwhisper] Checking and downloading model if needed: ${options.autoDownloadModelName}`);
logger.debug('autoDownloadModelName', options.autoDownloadModelName);
logger.debug('modelDir', options.modelDir);
logger.debug('options', options);
await (0, autoDownloadModel_1.default)(logger, options.autoDownloadModelName, options.withCuda, options.modelDir);
}
logger.debug(`[nwhisper] Checking file existence: ${filePath}`);
(0, utils_1.checkIfFileExists)(filePath);
logger.debug(`[nwhisper] Converting file to WAV format: ${filePath}`);
const outputFilePath = await (0, utils_1.convertToWavType)(filePath, logger);
logger.debug(`[nwhisper] Constructing command for file: ${outputFilePath}`);
const command = (0, WhisperHelper_1.constructCommand)(outputFilePath, options);
logger.debug(`[nwhisper] Executing command: ${command}`);
const transcript = await (0, whisper_1.executeCppCommand)(command, logger, options.withCuda);
if (!transcript) {
throw new Error('Transcription failed or produced no output.');
}
if (removeWavFileAfterTranscription && node_fs_1.default.existsSync(outputFilePath)) {
logger.debug(`[nwhisper] Removing temporary WAV file: ${outputFilePath}`);
node_fs_1.default.unlinkSync(outputFilePath);
}
return transcript;
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
logger.error(`[nwhisper] Error during processing: ${errorMessage}`);
throw new Error(`Operation failed: ${errorMessage}`);
}
}
/**
* @deprecated Use `transcribe` instead. This function will be removed in a future version.
*/
async function nodewhisper(filePath, options) {
return transcribe(filePath, options);
}
//# sourceMappingURL=index.js.map