diff --git a/package.json b/package.json index 63313f0..f89a30e 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { "name": "nodejs-whisper", - "version": "0.0.3", + "version": "0.0.6", "description": "Node bindings for OpenAI's Whisper. Optimized for CPU.", "main": "dist/index.js", + "types": "dist/index.d.ts", "publishConfig": { "access": "public" }, diff --git a/src/WhisperHelper.ts b/src/WhisperHelper.ts index 84beaac..17d6e7e 100644 --- a/src/WhisperHelper.ts +++ b/src/WhisperHelper.ts @@ -4,10 +4,14 @@ import fs from 'fs' import { MODELS, MODELS_LIST, MODEL_OBJECT } from './constants' export const constructCommand = (filePath: string, args: IOptions) => { + if (args?.modelName == undefined) { + throw new Error('[Nodejs-whisper] Error: Provide model name') + } + let anyModelExist = [] MODELS.forEach(model => { - if (!fs.existsSync(path.join(__dirname, '..', 'cpp', 'whisper.cpp', 'models', MODEL_OBJECT[args.modelName]))) { + if (!fs.existsSync(path.join(__dirname, '..', 'cpp', 'whisper.cpp', 'models', MODEL_OBJECT[args?.modelName]))) { } else { anyModelExist.push(model) } @@ -20,10 +24,19 @@ export const constructCommand = (filePath: string, args: IOptions) => { const modelName = MODEL_OBJECT[args.modelName as keyof typeof MODEL_OBJECT] - if (MODELS_LIST.indexOf(args.modelName) === -1) { + if (MODELS_LIST.indexOf(args?.modelName) === -1) { throw new Error('[Nodejs-whisper] Error: Model not found') } - let command = `./main ${constructOptionsFlags(args)} -l auto -m ./models/${modelName} -f ${filePath} ` + + const defaultOutput = + !args?.whisperOptions?.outputInCsv && + !args?.whisperOptions?.outputInSrt && + !args?.whisperOptions?.outputInText && + !args?.whisperOptions?.outputInVtt + + let command = `./main ${constructOptionsFlags(args)} ${ + defaultOutput && '-osrt' + } -l auto -m ./models/${modelName} -f ${filePath} ` return command } @@ -51,7 +64,7 @@ const constructOptionsFlags = (args: IOptions) => { if (args?.whisperOptions?.timestamps_length) { flag += `-ml ${args.whisperOptions.timestamps_length} ` } - if (args.whisperOptions.splitOnWord) { + if (args?.whisperOptions?.splitOnWord) { flag += `-sow true ` } return flag diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..5630470 --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,17 @@ +export interface WhisperOptions { + outputInText?: boolean + outputInVtt?: boolean + outputInSrt?: boolean + outputInCsv?: boolean + translateToEnglish?: boolean + timestamps_length?: number + wordTimestamps?: boolean + splitOnWord?: boolean +} + +export interface IOptions { + modelName: string + whisperOptions?: WhisperOptions +} + +export declare function nodewhisper(filePath: string, options: IOptions): Promise diff --git a/src/index.ts b/src/index.ts index 21bacea..00c563c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,6 @@ import downloadModel from './downloadModel' import { constructCommand } from './WhisperHelper' import { checkIfFileExists, convertToWavType } from './utils' -import path from 'path' export interface IOptions { modelName: string @@ -27,19 +26,3 @@ export async function nodewhisper(filePath: string, options: IOptions) { return transcript } - -// const filePath = path.resolve(__dirname, '..', 'test.wav') - -// nodewhisper(filePath, { -// modelName: 'base.en', -// whisperOptions: { -// outputInText: false, -// outputInVtt: false, -// outputInSrt: true, -// outputInCsv: false, -// translateToEnglish: false, -// wordTimestamps: false, -// timestamps_length: 20, -// splitOnWord: true, -// }, -// }) diff --git a/tsconfig.json b/tsconfig.json index ff87005..eae998b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "esModuleInterop": true, "target": "es6", "moduleResolution": "node", + "declaration": true, "sourceMap": true, "outDir": "dist" },