diff --git a/src/common/audio.ts b/src/common/audio.ts index 190a8a4d..030048bd 100644 --- a/src/common/audio.ts +++ b/src/common/audio.ts @@ -30,7 +30,8 @@ async function handleWavFile( ): Promise<{ input: Buffer; sampleRate: number }> { const { fmt } = getWavFileInfo(file); if (!ALLOW_SAMPLE_RATE.includes(fmt.sampleRate)) { - return { input: await FFmpegService.convert(filePath, pcmPath), sampleRate: 24000 }; + const result = await FFmpegService.convert(filePath, pcmPath); + return { input: await fsPromise.readFile(pcmPath), sampleRate: result.sampleRate }; } return { input: file, sampleRate: fmt.sampleRate }; } @@ -42,9 +43,18 @@ export async function encodeSilk(filePath: string, TEMP_DIR: string, logger: Log if (!isSilk(file)) { logger.log(`语音文件${filePath}需要转换成silk`); const pcmPath = `${pttPath}.pcm`; - const { input, sampleRate } = isWav(file) - ? await handleWavFile(file, filePath, pcmPath) - : { input: await FFmpegService.convert(filePath, pcmPath), sampleRate: 24000 }; + // const { input, sampleRate } = isWav(file) ? await handleWavFile(file, filePath, pcmPath): { input: await FFmpegService.convert(filePath, pcmPath) ? await fsPromise.readFile(pcmPath) : Buffer.alloc(0), sampleRate: 24000 }; + let input: Buffer; + let sampleRate: number; + if (isWav(file)) { + const result = await handleWavFile(file, filePath, pcmPath); + input = result.input; + sampleRate = result.sampleRate; + } else { + const result = await FFmpegService.convert(filePath, pcmPath); + input = await fsPromise.readFile(pcmPath); + sampleRate = result.sampleRate; + } const silk = await runTask(getWorkerPath(), { input: input, sampleRate: sampleRate }); fsPromise.unlink(pcmPath).catch((e) => logger.logError('删除临时文件失败', pcmPath, e)); await fsPromise.writeFile(pttPath, Buffer.from(silk.data)); @@ -69,7 +79,7 @@ export async function encodeSilk(filePath: string, TEMP_DIR: string, logger: Log }; } } catch (error: unknown) { - logger.logError('convert silk failed', (error as Error).stack); + logger.logError('convert silk failed', error); return {}; } } diff --git a/src/common/ffmpeg-adapter-interface.ts b/src/common/ffmpeg-adapter-interface.ts index 4e9a015c..1ad42227 100644 --- a/src/common/ffmpeg-adapter-interface.ts +++ b/src/common/ffmpeg-adapter-interface.ts @@ -49,7 +49,7 @@ export interface IFFmpegAdapter { * @param pcmPath 输出 PCM 文件路径 * @returns PCM 数据 Buffer */ - convertToPCM(filePath: string, pcmPath: string): Promise; + convertToPCM(filePath: string, pcmPath: string): Promise<{ result: boolean, sampleRate: number }>; /** * 转换音频文件 diff --git a/src/common/ffmpeg-addon-adapter.ts b/src/common/ffmpeg-addon-adapter.ts index 3494f5d8..e4cb93dd 100644 --- a/src/common/ffmpeg-addon-adapter.ts +++ b/src/common/ffmpeg-addon-adapter.ts @@ -88,14 +88,11 @@ export class FFmpegAddonAdapter implements IFFmpegAdapter { /** * 转换为 PCM */ - async convertToPCM(filePath: string, pcmPath: string): Promise { + async convertToPCM(filePath: string, pcmPath: string): Promise<{ result: boolean, sampleRate: number }> { const addon = this.ensureAddon(); - const result = await addon.decodeAudioToPCM(filePath); + const result = await addon.decodeAudioToPCM(filePath, pcmPath, 24000); - // 写入文件 - await writeFile(pcmPath, result.pcm); - - return result.pcm; + return result; } /** diff --git a/src/common/ffmpeg-addon.ts b/src/common/ffmpeg-addon.ts index 1712e510..acc5dca3 100644 --- a/src/common/ffmpeg-addon.ts +++ b/src/common/ffmpeg-addon.ts @@ -67,5 +67,5 @@ export interface FFmpeg { /** * Decode audio file to raw PCM data */ - decodeAudioToPCM(filePath: string): Promise; + decodeAudioToPCM(filePath: string, pcmPath: string, sampleRate?: number): Promise<{ result: boolean, sampleRate: number }>; } \ No newline at end of file diff --git a/src/common/ffmpeg-exec-adapter.ts b/src/common/ffmpeg-exec-adapter.ts index 8c503701..1a37c126 100644 --- a/src/common/ffmpeg-exec-adapter.ts +++ b/src/common/ffmpeg-exec-adapter.ts @@ -157,7 +157,7 @@ export class FFmpegExecAdapter implements IFFmpegAdapter { /** * 转换为 PCM */ - async convertToPCM(filePath: string, pcmPath: string): Promise { + async convertToPCM(filePath: string, pcmPath: string): Promise<{ result: boolean, sampleRate: number }> { try { ensureDirExists(pcmPath); @@ -174,7 +174,7 @@ export class FFmpegExecAdapter implements IFFmpegAdapter { throw new Error('转换PCM失败,输出文件不存在'); } - return readFileSync(pcmPath); + return { result: true, sampleRate: 24000 }; } catch (error: any) { throw new Error(`FFmpeg处理转换出错: ${error.message}`); } diff --git a/src/common/ffmpeg.ts b/src/common/ffmpeg.ts index e71fb254..4d120bb9 100644 --- a/src/common/ffmpeg.ts +++ b/src/common/ffmpeg.ts @@ -93,7 +93,7 @@ export class FFmpegService { /** * 转换为 PCM 格式 */ - public static async convert(filePath: string, pcmPath: string): Promise { + public static async convert(filePath: string, pcmPath: string): Promise<{ result: boolean, sampleRate: number }> { const adapter = await this.getAdapter(); return adapter.convertToPCM(filePath, pcmPath); } diff --git a/src/native/ffmpeg/ffmpegAddon.darwin.arm64.node b/src/native/ffmpeg/ffmpegAddon.darwin.arm64.node index f8b69143..af8b0c20 100644 Binary files a/src/native/ffmpeg/ffmpegAddon.darwin.arm64.node and b/src/native/ffmpeg/ffmpegAddon.darwin.arm64.node differ diff --git a/src/native/ffmpeg/ffmpegAddon.linux.arm64.node b/src/native/ffmpeg/ffmpegAddon.linux.arm64.node index eefdb7a0..aa2413e6 100644 Binary files a/src/native/ffmpeg/ffmpegAddon.linux.arm64.node and b/src/native/ffmpeg/ffmpegAddon.linux.arm64.node differ diff --git a/src/native/ffmpeg/ffmpegAddon.linux.x64.node b/src/native/ffmpeg/ffmpegAddon.linux.x64.node index 5d044a09..8886b5ff 100644 Binary files a/src/native/ffmpeg/ffmpegAddon.linux.x64.node and b/src/native/ffmpeg/ffmpegAddon.linux.x64.node differ diff --git a/src/native/ffmpeg/ffmpegAddon.win32.x64.node b/src/native/ffmpeg/ffmpegAddon.win32.x64.node index 06054c75..1dca7f78 100644 Binary files a/src/native/ffmpeg/ffmpegAddon.win32.x64.node and b/src/native/ffmpeg/ffmpegAddon.win32.x64.node differ