From abb468c3f827579f3706b41682817ee679aa10f4 Mon Sep 17 00:00:00 2001 From: idranme <96647698+idranme@users.noreply.github.com> Date: Fri, 11 Oct 2024 13:38:59 +0800 Subject: [PATCH] optimize --- src/common/utils/audio.ts | 77 ++++++++++++++++++--------------------- src/ntqqapi/entities.ts | 6 +-- src/onebot11/adapter.ts | 6 +-- 3 files changed, 40 insertions(+), 49 deletions(-) diff --git a/src/common/utils/audio.ts b/src/common/utils/audio.ts index fce6ad0..e98ab7d 100644 --- a/src/common/utils/audio.ts +++ b/src/common/utils/audio.ts @@ -53,49 +53,44 @@ function convert(ctx: Context, input: Input, options: FFmpegOptions, outputPath? } export async function encodeSilk(ctx: Context, filePath: string) { - try { - const file = await fsPromise.readFile(filePath) - if (!isSilk(file)) { - ctx.logger.info(`语音文件${filePath}需要转换成silk`) - let result: EncodeResult - const allowSampleRate = [8000, 12000, 16000, 24000, 32000, 44100, 48000] - if (isWav(file) && allowSampleRate.includes(getWavFileInfo(file).fmt.sampleRate)) { - result = await encode(file, 0) - } else { - const input = await convert(ctx, filePath, { - output: [ - '-ar 24000', - '-ac 1', - '-f s16le' - ] - }) - result = await encode(input, 24000) - } - const pttPath = path.join(TEMP_DIR, randomUUID()) - await fsPromise.writeFile(pttPath, result.data) - ctx.logger.info(`语音文件${filePath}转换成功!`, pttPath, `时长:`, result.duration) - return { - converted: true, - path: pttPath, - duration: result.duration / 1000, - } + const file = await fsPromise.readFile(filePath) + if (!isSilk(file)) { + ctx.logger.info(`语音文件${filePath}需要转换成silk`) + let result: EncodeResult + const allowSampleRate = [8000, 12000, 16000, 24000, 32000, 44100, 48000] + if (isWav(file) && allowSampleRate.includes(getWavFileInfo(file).fmt.sampleRate)) { + result = await encode(file, 0) } else { - const silk = file - let duration = 1 - try { - duration = getDuration(silk) / 1000 - } catch (e) { - ctx.logger.warn('获取语音文件时长失败, 默认为1秒', filePath, (e as Error).stack) - } - return { - converted: false, - path: filePath, - duration, - } + const input = await convert(ctx, filePath, { + output: [ + '-ar 24000', + '-ac 1', + '-f s16le' + ] + }) + result = await encode(input, 24000) + } + const pttPath = path.join(TEMP_DIR, randomUUID()) + await fsPromise.writeFile(pttPath, result.data) + ctx.logger.info(`语音文件${filePath}转换成功!`, pttPath, `时长:`, result.duration) + return { + converted: true, + path: pttPath, + duration: result.duration / 1000, + } + } else { + const silk = file + let duration = 1 + try { + duration = getDuration(silk) / 1000 + } catch (e) { + ctx.logger.warn('获取语音文件时长失败, 默认为1秒', filePath, (e as Error).stack) + } + return { + converted: false, + path: filePath, + duration, } - } catch (err) { - ctx.logger.error('convert silk failed', (err as Error).stack) - return {} } } diff --git a/src/ntqqapi/entities.ts b/src/ntqqapi/entities.ts index 0ec2bd2..e5e3b99 100644 --- a/src/ntqqapi/entities.ts +++ b/src/ntqqapi/entities.ts @@ -210,13 +210,9 @@ export namespace SendElement { export async function ptt(ctx: Context, pttPath: string): Promise { const { converted, path: silkPath, duration } = await encodeSilk(ctx, pttPath) - if (!silkPath) { - throw '语音转换失败, 请检查语音文件是否正常' - } - // log("生成语音", silkPath, duration); const { md5, fileName, path, fileSize } = await ctx.ntFileApi.uploadFile(silkPath, ElementType.Ptt) if (fileSize === 0) { - throw '文件异常,大小为0' + throw new Error('文件异常,大小为 0') } if (converted) { unlink(silkPath) diff --git a/src/onebot11/adapter.ts b/src/onebot11/adapter.ts index ee3a61a..d34c2f3 100644 --- a/src/onebot11/adapter.ts +++ b/src/onebot11/adapter.ts @@ -180,19 +180,19 @@ class OneBot11Adapter extends Service { msg.target_id = parseInt(message.peerUin) } this.dispatch(msg) - }).catch(e => this.ctx.logger.error('constructMessage error: ', e.stack.toString())) + }).catch(e => this.ctx.logger.error('handling incoming messages', e)) OB11Entities.groupEvent(this.ctx, message).then(groupEvent => { if (groupEvent) { this.dispatch(groupEvent) } - }) + }).catch(e => this.ctx.logger.error('handling incoming group events', e)) OB11Entities.privateEvent(this.ctx, message).then(privateEvent => { if (privateEvent) { this.dispatch(privateEvent) } - }) + }).catch(e => this.ctx.logger.error('handling incoming buddy events', e)) } private handleRecallMsg(message: RawMessage) {