This commit is contained in:
idranme 2024-10-11 13:38:59 +08:00
parent 433a175809
commit abb468c3f8
3 changed files with 40 additions and 49 deletions
src
common/utils
ntqqapi
onebot11

@ -53,49 +53,44 @@ function convert(ctx: Context, input: Input, options: FFmpegOptions, outputPath?
} }
export async function encodeSilk(ctx: Context, filePath: string) { export async function encodeSilk(ctx: Context, filePath: string) {
try { const file = await fsPromise.readFile(filePath)
const file = await fsPromise.readFile(filePath) if (!isSilk(file)) {
if (!isSilk(file)) { ctx.logger.info(`语音文件${filePath}需要转换成silk`)
ctx.logger.info(`语音文件${filePath}需要转换成silk`) let result: EncodeResult
let result: EncodeResult const allowSampleRate = [8000, 12000, 16000, 24000, 32000, 44100, 48000]
const allowSampleRate = [8000, 12000, 16000, 24000, 32000, 44100, 48000] if (isWav(file) && allowSampleRate.includes(getWavFileInfo(file).fmt.sampleRate)) {
if (isWav(file) && allowSampleRate.includes(getWavFileInfo(file).fmt.sampleRate)) { result = await encode(file, 0)
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,
}
} else { } else {
const silk = file const input = await convert(ctx, filePath, {
let duration = 1 output: [
try { '-ar 24000',
duration = getDuration(silk) / 1000 '-ac 1',
} catch (e) { '-f s16le'
ctx.logger.warn('获取语音文件时长失败, 默认为1秒', filePath, (e as Error).stack) ]
} })
return { result = await encode(input, 24000)
converted: false, }
path: filePath, const pttPath = path.join(TEMP_DIR, randomUUID())
duration, 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 {}
} }
} }

@ -210,13 +210,9 @@ export namespace SendElement {
export async function ptt(ctx: Context, pttPath: string): Promise<SendPttElement> { export async function ptt(ctx: Context, pttPath: string): Promise<SendPttElement> {
const { converted, path: silkPath, duration } = await encodeSilk(ctx, pttPath) 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) const { md5, fileName, path, fileSize } = await ctx.ntFileApi.uploadFile(silkPath, ElementType.Ptt)
if (fileSize === 0) { if (fileSize === 0) {
throw '文件异常,大小为0' throw new Error('文件异常,大小为 0')
} }
if (converted) { if (converted) {
unlink(silkPath) unlink(silkPath)

@ -180,19 +180,19 @@ class OneBot11Adapter extends Service {
msg.target_id = parseInt(message.peerUin) msg.target_id = parseInt(message.peerUin)
} }
this.dispatch(msg) 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 => { OB11Entities.groupEvent(this.ctx, message).then(groupEvent => {
if (groupEvent) { if (groupEvent) {
this.dispatch(groupEvent) this.dispatch(groupEvent)
} }
}) }).catch(e => this.ctx.logger.error('handling incoming group events', e))
OB11Entities.privateEvent(this.ctx, message).then(privateEvent => { OB11Entities.privateEvent(this.ctx, message).then(privateEvent => {
if (privateEvent) { if (privateEvent) {
this.dispatch(privateEvent) this.dispatch(privateEvent)
} }
}) }).catch(e => this.ctx.logger.error('handling incoming buddy events', e))
} }
private handleRecallMsg(message: RawMessage) { private handleRecallMsg(message: RawMessage) {