From a1ea078b2b8383a2d084678a02df1f3ae56a5a02 Mon Sep 17 00:00:00 2001 From: idranme Date: Fri, 21 Jun 2024 21:46:02 +0800 Subject: [PATCH] 3.2.1 --- package.json | 2 +- src/media.ts | 34 +++++++++++++++++++++------------- src/message.ts | 8 ++++---- src/utils.ts | 2 +- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index a38d848..5ca9e53 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-adapter-red", "description": "Red Protocol Adapter for Koishi", - "version": "3.2.0", + "version": "3.2.1", "main": "lib/index.js", "typings": "lib/index.d.ts", "files": [ diff --git a/src/media.ts b/src/media.ts index 0a61de3..f7652ae 100644 --- a/src/media.ts +++ b/src/media.ts @@ -2,7 +2,7 @@ import { readFile } from 'node:fs/promises' import { join } from 'node:path' import { exec } from 'node:child_process' import { tmpdir } from 'node:os' -import { writeFile, unlink } from 'node:fs' +import { writeFileSync, unlink } from 'node:fs' import { toUTF8String } from './utils' import { noop } from 'koishi' @@ -12,15 +12,18 @@ function saveTmp(data: Buffer, ext?: string): string { ext = ext ? '.' + ext : '' const filename = `adapter-red-${Date.now()}${ext}` const tmpPath = join(TMP_DIR, filename) - writeFile(tmpPath, data, (err) => { - if (err) throw err - }) + writeFileSync(tmpPath, data) return tmpPath } -export function convertToPcm(buffer: Buffer, samplingRate = '24000'): Promise { +export function convertToPcm(input: Buffer, samplingRate = '24000'): Promise { return new Promise((resolve, reject) => { - const tmpPath = saveTmp(buffer) + let tmpPath: string + try { + tmpPath = saveTmp(input) + } catch (err) { + return reject(err) + } const targetPath = join(TMP_DIR, `adapter-red-${Date.now()}`) exec(`ffmpeg -y -i "${tmpPath}" -ar ${samplingRate} -ac 1 -f s16le "${targetPath}"`, async () => { unlink(tmpPath, noop) @@ -38,7 +41,12 @@ export function convertToPcm(buffer: Buffer, samplingRate = '24000'): Promise { return new Promise((resolve, reject) => { - const tmpPath = saveTmp(input) + let tmpPath: string + try { + tmpPath = saveTmp(input) + } catch (err) { + return reject(err) + } const targetPath = join(TMP_DIR, `adapter-red-${Date.now()}`) exec(`ffmpeg -y -i "${tmpPath}" -frames:v 1 -f image2 -codec png -update 1 "${targetPath}"`, async () => { unlink(tmpPath, noop) @@ -54,16 +62,16 @@ export function getVideoCover(input: Buffer): Promise { }) } -export function calculatePngSize(input: Buffer) { +export function calculatePngSize(data: Buffer) { // Detect "fried" png's: http://www.jongware.com/pngdefry.html - if (toUTF8String(input, 12, 16) === 'CgBI') { + if (toUTF8String(data, 12, 16) === 'CgBI') { return { - height: input.readUInt32BE(36), - width: input.readUInt32BE(32) + height: data.readUInt32BE(36), + width: data.readUInt32BE(32) } } return { - height: input.readUInt32BE(20), - width: input.readUInt32BE(16) + height: data.readUInt32BE(20), + width: data.readUInt32BE(16) } } \ No newline at end of file diff --git a/src/message.ts b/src/message.ts index 4a7bdcf..ffa7e78 100644 --- a/src/message.ts +++ b/src/message.ts @@ -143,19 +143,19 @@ export class RedMessageEncoder extends MessageEncod const pcm = await convert(voice, 24000) const silk = await ctx.silk.encode(pcm, 24000) voice = silk.data - duration = Math.round(silk.duration / 1000) + duration = silk.duration / 1000 } else { const silk = await ctx.silk.encode(voice, 0) voice = silk.data - duration = Math.round(silk.duration / 1000) + duration = silk.duration / 1000 } } else if (!ctx.silk.isSilk(voice)) { const pcm = await convert(voice, 24000) const silk = await ctx.silk.encode(pcm, 24000) voice = silk.data - duration = Math.round(silk.duration / 1000) + duration = silk.duration / 1000 } - duration ||= Math.round(ctx.silk.getDuration(voice) / 1000) + duration ||= ctx.silk.getDuration(voice) / 1000 const form = new FormData() const value = new Blob([voice], { type: 'audio/amr' }) diff --git a/src/utils.ts b/src/utils.ts index 3982766..2e95d27 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -139,7 +139,7 @@ export async function decodeMessage( } case 7: { if (quoted) continue - const key = `${data.chatType}/${data.peerUin}/${v.replyElement}` + const key = `${data.chatType}/${data.peerUin}/${v.replyElement.replayMsgSeq}` const [msgId, firstElementId] = bot.redSeq.get(key) ?? [] const record = data.records[0] const elements = record && await parse(record, msgId, firstElementId, true)