diff --git a/src/WhisperHelper.ts b/src/WhisperHelper.ts index 39cdee3..749d47f 100644 --- a/src/WhisperHelper.ts +++ b/src/WhisperHelper.ts @@ -33,6 +33,11 @@ export const constructCommand = (filePath: string, args: IOptions): string => { } const constructOptionsFlags = (args: IOptions): string => { + if (args.responseFormat) { + // Include the response format... + args.whisperOptions['outputIn' + capitalize(args.responseFormat)] = true + } + let flags = [ args.whisperOptions?.outputInCsv ? '-ocsv ' : '', args.whisperOptions?.outputInJson ? '-oj ' : '', @@ -50,3 +55,7 @@ const constructOptionsFlags = (args: IOptions): string => { return flags.trim() } + +function capitalize(s) { + return s && String(s[0]).toUpperCase() + String(s).slice(1) +} diff --git a/src/index.ts b/src/index.ts index 992bb94..e03e7cd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,10 +12,11 @@ export interface IOptions { withCuda?: boolean removeWavFileAfterTranscription?: boolean logger?: Console + responseFormat?: 'json' | 'csv' | 'srt' | 'vtt' | 'lrc' | 'text' } export async function nodewhisper(filePath: string, options: IOptions) { - const { removeWavFileAfterTranscription = false, logger = console } = options + const { removeWavFileAfterTranscription = false, logger = console, responseFormat } = options try { if (options.autoDownloadModelName) { @@ -48,6 +49,12 @@ export async function nodewhisper(filePath: string, options: IOptions) { fs.unlinkSync(outputFilePath) } + if (responseFormat) { + logger.debug(`[Nodejs-whisper] Retrieving response from: ${command}`) + const response = fs.readFileSync(`${outputFilePath}.${responseFormat}`, 'utf8') + return responseFormat === 'json' ? JSON.parse(response) : response + } + return transcript } catch (error) { logger.error(`[Nodejs-whisper] Error during processing: ${error.message}`)