Files

101 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

// get_record 正向对照:区分「NapCat 侧 ffmpeg 整体不可用」与「仅缺 silk/amr 解码器」。
//
// 上一轮结论只到「NapCat 连自己收到的真实 amr 也解不开」,但这有两种解释:
// A. ffmpeg 缺失/损坏 → 任何格式都转不了
// B. ffmpeg 在但缺 silk/amr 解码器 → 只有语音类失败
// 两者对 QLinker 的处置不同(A 需修运行环境,B 需换 NapCat 构建或自带转码),
// 因此用一个**自造的合法 WAV**(纯 PCM,无需任何编码器)做正向对照。
//
// 全部只读,不发送消息。凭据只从环境变量读取。
import fs from 'node:fs';
import path from 'node:path';
const WS_URL = process.env.NAPCAT_WS_URL || 'ws://192.168.5.32:16099';
const TOKEN = process.env.NAPCAT_WS_TOKEN;
const OUT_DIR = process.env.PROBE_OUT || 'local';
if (!TOKEN) { console.error('缺少环境变量 NAPCAT_WS_TOKEN。凭据不得硬编码。'); process.exit(1); }
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
fs.mkdirSync(OUT_DIR, { recursive: true });
const OUT = path.join(OUT_DIR, `record-codec-probe-${stamp}.jsonl`);
const say = (...a) => console.log(...a);
// 生成 0.5s 8kHz 单声道 16bit PCM 正弦波 WAV
function wav(seconds = 0.5, hz = 440, rate = 8000) {
const n = Math.floor(seconds * rate);
const body = Buffer.alloc(n * 2);
for (let i = 0; i < n; i++) {
const v = Math.round(Math.sin((2 * Math.PI * hz * i) / rate) * 12000);
body.writeInt16LE(v, i * 2);
}
const h = Buffer.alloc(44);
h.write('RIFF', 0); h.writeUInt32LE(36 + body.length, 4); h.write('WAVE', 8);
h.write('fmt ', 12); h.writeUInt32LE(16, 16); h.writeUInt16LE(1, 20); h.writeUInt16LE(1, 22);
h.writeUInt32LE(rate, 24); h.writeUInt32LE(rate * 2, 28); h.writeUInt16LE(2, 32); h.writeUInt16LE(16, 34);
h.write('data', 36); h.writeUInt32LE(body.length, 40);
return Buffer.concat([h, body]);
}
const WAV = wav();
const WAV_B64 = WAV.toString('base64');
fs.writeFileSync(path.join(OUT_DIR, `probe-tone-${stamp}.wav`), WAV);
say(`自造 WAV: ${WAV.length} 字节,magic=${JSON.stringify(WAV.slice(0, 4).toString('latin1'))}`);
const ws = new WebSocket(`${WS_URL.replace(/\/$/, '')}/?access_token=${encodeURIComponent(TOKEN)}`);
const pending = new Map();
let opened = false, authed = false;
ws.onopen = () => { opened = true; };
ws.onmessage = (ev) => {
let o; try { o = JSON.parse(String(ev.data)); } catch { return; }
if (o.echo && pending.has(o.echo)) { pending.get(o.echo).resolve(o); pending.delete(o.echo); return; }
if (o.post_type === 'meta_event' && o.meta_event_type === 'lifecycle') authed = true;
};
for (let i = 0; i < 100 && !opened; i++) await new Promise((r) => setTimeout(r, 100));
if (!opened) { console.error('连接超时'); process.exit(1); }
for (let i = 0; i < 100 && !authed; i++) await new Promise((r) => setTimeout(r, 100));
say('认证=' + authed);
const api = (action, params, ms = 40000) => new Promise((resolve) => {
const echo = `cc-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`;
const to = setTimeout(() => { pending.delete(echo); resolve({ __timeout: true }); }, ms);
pending.set(echo, { resolve: (o) => { clearTimeout(to); resolve(o); }, to });
ws.send(JSON.stringify({ action, params, echo }));
});
// 自造 WAV 的输入形式穷举,找出 get_record 到底接受哪种载荷表达
const CASES = [
{ id: 'wav.base64Prefix', p: { file: 'base64://' + WAV_B64, out_format: 'mp3' } },
{ id: 'wav.dataUri', p: { file: 'data:audio/wav;base64,' + WAV_B64, out_format: 'mp3' } },
{ id: 'wav.bareBase64', p: { file: WAV_B64, out_format: 'mp3' } },
{ id: 'wav.b64Field', p: { file: WAV_B64, out_format: 'mp3', b64: true } },
];
const silkFile = fs.readdirSync(OUT_DIR).filter((f) => f.startsWith('record-silk-')).sort().pop();
if (silkFile) {
const silkB64 = fs.readFileSync(path.join(OUT_DIR, silkFile)).toString('base64');
CASES.push({ id: 'silk.base64Prefix', p: { file: 'base64://' + silkB64, out_format: 'mp3' } });
} else {
say('未找到 local/record-silk-*.bin,跳过 silk 对照');
}
const rows = [];
for (const c of CASES) {
const r = await api('get_record', c.p);
const rc = r.__timeout ? 'TIMEOUT' : r.retcode;
const msg = String(r.message ?? r.wording ?? '');
const rec = { id: c.id, retcode: rc, message: msg,
input_len: String(c.p.file ?? '').length,
data_preview: JSON.stringify(r.data ?? null).slice(0, 300) };
rows.push(rec);
fs.appendFileSync(OUT, JSON.stringify({ kind: 'probe', ...rec }) + '\n');
say(`${c.id.padEnd(20)} retcode=${String(rc).padEnd(7)} ${msg.slice(0, 58).padEnd(60)} ${rec.data_preview.slice(0, 50)}`);
}
const wavOk = rows.some((r) => r.id.startsWith('wav.') && r.retcode === 0);
say(`\n判定:自造合法 WAV ${wavOk ? '转换成功 → ffmpeg 可用,缺的是 silk/amr 解码支持' : '全部失败 → 需结合错误类型区分「输入形式不被接受」与「解码器缺失」'}`);
say('注意:若 WAV 各形式均报 file not found,说明 get_record 不接受内联载荷,'
+ '此时本对照**无法**区分 A/B,只能保留「环境解码能力受限」为待验证定性。');
say('\n结果 -> ' + OUT);
try { ws.close(); } catch {}
process.exit(0);