From 35ac13f6ab655e5f8d31e749c71627e184b1ba97 Mon Sep 17 00:00:00 2001 From: kw0n0 Date: Thu, 19 Dec 2024 17:27:07 +0900 Subject: [PATCH 1/2] feat: allow conversion when passing buffer data. --- src/index.ts | 15 ++++++++---- src/utils.ts | 65 ++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index 992bb94..c746d91 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,9 @@ export interface IOptions { logger?: Console } -export async function nodewhisper(filePath: string, options: IOptions) { +export type FilePath = string + +export async function nodewhisper(audioSrc: FilePath | Buffer, options: IOptions) { const { removeWavFileAfterTranscription = false, logger = console } = options try { @@ -27,11 +29,14 @@ export async function nodewhisper(filePath: string, options: IOptions) { await autoDownloadModel(logger, options.autoDownloadModelName, options.withCuda) } - logger.debug(`[Nodejs-whisper] Checking file existence: ${filePath}`) - checkIfFileExists(filePath) + logger.debug(`[Nodejs-whisper] Checking file existence: ${audioSrc}`) + if (!Buffer.isBuffer(audioSrc)) { + logger.debug(`[Nodejs-whisper] Checking file existence: ${audioSrc}`) + checkIfFileExists(audioSrc) + } - logger.debug(`[Nodejs-whisper] Converting file to WAV format: ${filePath}`) - const outputFilePath = await convertToWavType(filePath, logger) + logger.debug(`[Nodejs-whisper] Converting file to WAV format: ${audioSrc}`) + const outputFilePath = await convertToWavType(audioSrc, logger) logger.debug(`[Nodejs-whisper] Constructing command for file: ${outputFilePath}`) const command = constructCommand(outputFilePath, options) diff --git a/src/utils.ts b/src/utils.ts index ee66e4a..a4b8a66 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,7 @@ import fs from 'fs' import path from 'path' import shell from 'shelljs' +import { FilePath } from '.' export const checkIfFileExists = (filePath: string) => { if (!fs.existsSync(filePath)) { @@ -28,38 +29,82 @@ async function isValidWavHeader(filePath) { }) } -export const convertToWavType = async (inputFilePath, logger = console) => { - const fileExtension = path.extname(inputFilePath).toLowerCase() +export const convertToWavType = async (inputAudioSrc: FilePath | Buffer, logger = console) => { + //NOTE: Buffer 데이터가 들어온 경우 + if (Buffer.isBuffer(inputAudioSrc)) { + logger.debug('[Nodejs-whisper] Starting audio buffer conversion process') - logger.debug(`[Nodejs-whisper] Checking if the file is a valid WAV: ${inputFilePath}`) + //NOTE: 임시 입력 파일 생성 + const tempInputPath = `Nodejs-whisper-input_${Date.now()}` + fs.writeFileSync(tempInputPath, inputAudioSrc) + logger.debug(`[Nodejs-whisper] Temporary input file created: ${tempInputPath}`) + + //NOTE: 출력 WAV 파일 경로 설정 + const outputFilePath = `Nodejs-whisper-temp_${Date.now()}.wav` + logger.debug(`[Nodejs-whisper] Output WAV path set: ${outputFilePath}`) + + try { + //NOTE: ffmpeg 명령어 실행 + const command = `ffmpeg -nostats -loglevel error -y -i "${tempInputPath}" -ar 16000 -ac 1 -c:a pcm_s16le "${outputFilePath}"` + logger.debug(`[Nodejs-whisper] Executing ffmpeg command: ${command}`) + + const result = shell.exec(command) + if (result.code !== 0) { + console.error(`[Nodejs-whisper] Conversion failed with code ${result.code}`) + throw new Error(`[Nodejs-whisper] Failed to convert buffer to WAV: ${result.stderr}`) + } + + logger.debug('[Nodejs-whisper] Audio conversion completed successfully') + + fs.unlinkSync(tempInputPath) + logger.debug(`[Nodejs-whisper] Temporary input file cleaned up: ${tempInputPath}`) + + return outputFilePath + } catch (error) { + console.error('[Nodejs-whisper] Error during conversion:', error) + if (fs.existsSync(tempInputPath)) { + fs.unlinkSync(tempInputPath) + logger.debug(`[Nodejs-whisper] Cleaned up temp input file: ${tempInputPath}`) + } + if (fs.existsSync(outputFilePath)) { + fs.unlinkSync(outputFilePath) + logger.debug(`[Nodejs-whisper] Cleaned up output file: ${outputFilePath}`) + } + throw error + } + } + + const fileExtension = path.extname(inputAudioSrc).toLowerCase() + + logger.debug(`[Nodejs-whisper] Checking if the file is a valid WAV: ${inputAudioSrc}`) if (fileExtension === '.wav') { - const isWav = await isValidWavHeader(inputFilePath) + const isWav = await isValidWavHeader(inputAudioSrc) if (isWav) { logger.debug(`[Nodejs-whisper] File is a valid WAV file.`) - return inputFilePath + return inputAudioSrc } else { logger.debug(`[Nodejs-whisper] File has a .wav extension but is not a valid WAV, overwriting...`) // Overwrite the original WAV file - const command = `ffmpeg -nostats -loglevel error -y -i "${inputFilePath}" -ar 16000 -ac 1 -c:a pcm_s16le "${inputFilePath}"` + const command = `ffmpeg -nostats -loglevel error -y -i "${inputAudioSrc}" -ar 16000 -ac 1 -c:a pcm_s16le "${inputAudioSrc}"` const result = shell.exec(command) if (result.code !== 0) { throw new Error(`[Nodejs-whisper] Failed to convert audio file: ${result.stderr}`) } - return inputFilePath + return inputAudioSrc } } else { // Convert to a new WAV file const outputFilePath = path.join( - path.dirname(inputFilePath), - `${path.basename(inputFilePath, fileExtension)}.wav` + path.dirname(inputAudioSrc), + `${path.basename(inputAudioSrc, fileExtension)}.wav` ) logger.debug(`[Nodejs-whisper] Converting to a new WAV file: ${outputFilePath}`) - const command = `ffmpeg -nostats -loglevel error -y -i "${inputFilePath}" -ar 16000 -ac 1 -c:a pcm_s16le "${outputFilePath}"` + const command = `ffmpeg -nostats -loglevel error -y -i "${inputAudioSrc}" -ar 16000 -ac 1 -c:a pcm_s16le "${outputFilePath}"` const result = shell.exec(command) if (result.code !== 0) { throw new Error(`[Nodejs-whisper] Failed to convert audio file: ${result.stderr}`) From 9e2ff8000ae0dc26b903e91e2da6a28bdbfed68f Mon Sep 17 00:00:00 2001 From: kw0n0 Date: Thu, 19 Dec 2024 17:33:47 +0900 Subject: [PATCH 2/2] chore: translate annotation --- src/utils.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index a4b8a66..d227c6f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -30,21 +30,21 @@ async function isValidWavHeader(filePath) { } export const convertToWavType = async (inputAudioSrc: FilePath | Buffer, logger = console) => { - //NOTE: Buffer 데이터가 들어온 경우 + //When Buffer data is received if (Buffer.isBuffer(inputAudioSrc)) { logger.debug('[Nodejs-whisper] Starting audio buffer conversion process') - //NOTE: 임시 입력 파일 생성 + //Create temporary input file const tempInputPath = `Nodejs-whisper-input_${Date.now()}` fs.writeFileSync(tempInputPath, inputAudioSrc) logger.debug(`[Nodejs-whisper] Temporary input file created: ${tempInputPath}`) - //NOTE: 출력 WAV 파일 경로 설정 + //Set output WAV file path const outputFilePath = `Nodejs-whisper-temp_${Date.now()}.wav` logger.debug(`[Nodejs-whisper] Output WAV path set: ${outputFilePath}`) try { - //NOTE: ffmpeg 명령어 실행 + //Same logic as before const command = `ffmpeg -nostats -loglevel error -y -i "${tempInputPath}" -ar 16000 -ac 1 -c:a pcm_s16le "${outputFilePath}"` logger.debug(`[Nodejs-whisper] Executing ffmpeg command: ${command}`)