Skip to content

Commit

Permalink
3.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
idranme committed Jun 21, 2024
1 parent 230a65e commit a1ea078
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
34 changes: 21 additions & 13 deletions src/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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<Buffer> {
export function convertToPcm(input: Buffer, samplingRate = '24000'): Promise<Buffer> {
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)
Expand All @@ -38,7 +41,12 @@ export function convertToPcm(buffer: Buffer, samplingRate = '24000'): Promise<Bu

export function getVideoCover(input: Buffer): Promise<Buffer> {
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)
Expand All @@ -54,16 +62,16 @@ export function getVideoCover(input: Buffer): Promise<Buffer> {
})
}

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)
}
}
8 changes: 4 additions & 4 deletions src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,19 @@ export class RedMessageEncoder<C extends Context = Context> 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' })
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a1ea078

Please sign in to comment.