Skip to content

Commit 5c2e6be

Browse files
author
Andrew Dodson
committed
feat(formatResponse): return the response in the corresponding format
1 parent 52f15e0 commit 5c2e6be

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/WhisperHelper.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export const constructCommand = (filePath: string, args: IOptions): string => {
3333
}
3434

3535
const constructOptionsFlags = (args: IOptions): string => {
36+
if (args.responseFormat) {
37+
// Include the response format...
38+
args.whisperOptions['outputIn' + capitalize(args.responseFormat)] = true
39+
}
40+
3641
let flags = [
3742
args.whisperOptions?.outputInCsv ? '-ocsv ' : '',
3843
args.whisperOptions?.outputInJson ? '-oj ' : '',
@@ -50,3 +55,7 @@ const constructOptionsFlags = (args: IOptions): string => {
5055

5156
return flags.trim()
5257
}
58+
59+
function capitalize(s) {
60+
return s && String(s[0]).toUpperCase() + String(s).slice(1)
61+
}

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ export interface IOptions {
1212
withCuda?: boolean
1313
removeWavFileAfterTranscription?: boolean
1414
logger?: Console
15+
responseFormat?: 'json' | 'csv' | 'srt' | 'vtt' | 'lrc' | 'text'
1516
}
1617

1718
export async function nodewhisper(filePath: string, options: IOptions) {
18-
const { removeWavFileAfterTranscription = false, logger = console } = options
19+
const { removeWavFileAfterTranscription = false, logger = console, responseFormat } = options
1920

2021
try {
2122
if (options.autoDownloadModelName) {
@@ -48,6 +49,12 @@ export async function nodewhisper(filePath: string, options: IOptions) {
4849
fs.unlinkSync(outputFilePath)
4950
}
5051

52+
if (responseFormat) {
53+
logger.debug(`[Nodejs-whisper] Retrieving response from: ${command}`)
54+
const response = fs.readFileSync(`${outputFilePath}.${responseFormat}`, 'utf8')
55+
return responseFormat === 'json' ? JSON.parse(response) : response
56+
}
57+
5158
return transcript
5259
} catch (error) {
5360
logger.error(`[Nodejs-whisper] Error during processing: ${error.message}`)

0 commit comments

Comments
 (0)