Introduce NT SILK-based voice (PTT) upload pipeline and native FFmpeg addon wrapper. - Add ffmpeg-addon wrapper (packages/core/src/bridge/highway/ffmpeg-addon.ts) to load platform/arch .node binaries, detect SILK files, and convert inputs to NT SILK via the native addon. Provides encodeSilk, isSilkFile and default temp dir helpers. - Implement ptt-upload pipeline (packages/core/src/bridge/highway/ptt-upload.ts) to: resolve/fetch audio bytes, convert to NT SILK if needed, send OIDB upload request, perform highway HTTP upload when required, and produce Encodable MsgInfo bytes for commonElem. - Wire record element support into element-builder: uploadPttMsgInfo is used to build a commonElem for 'record' sends; when SendContext is absent, record sends are skipped with a warning (graceful no-bridge path). - Update message parsing to accept record segments from data.file, data.url or data.path and drop empty sources. - Enable canSendRecord in OneBot instance context. - Add tests covering record parsing and the graceful send path (packages/core/tests/message-parser.test.ts). - Include prebuilt native ffmpeg addon binaries under packages/runtime/native/ffmpeg and update vite config to validate & copy the ffmpeg addon into the dist layout. These changes enable sending voice messages by converting/encoding sources to the expected NT SILK format and uploading them through the existing highway/NTV2 flows.
162 lines
5.9 KiB
TypeScript
162 lines
5.9 KiB
TypeScript
// FFmpeg native addon wrapper — port of NapCat's FFmpegAddonAdapter.
|
|
// No fallback: throws if the addon isn't available on this platform/arch.
|
|
//
|
|
// The addon is shipped under `native/ffmpeg/ffmpegAddon.<platform>.<arch>.node`
|
|
// to match the layout used by the upstream NapCat build so the binaries
|
|
// can be dropped in as-is.
|
|
|
|
import fs from 'fs';
|
|
import os from 'os';
|
|
import path from 'path';
|
|
import crypto from 'crypto';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
export interface FFmpegVideoInfo {
|
|
width: number;
|
|
height: number;
|
|
duration: number;
|
|
format: string;
|
|
videoCodec: string;
|
|
image: Buffer;
|
|
}
|
|
|
|
interface FFmpegNativeAddon {
|
|
convertFile(inputFile: string, outputFile: string, format: string): Promise<{ success: boolean }>;
|
|
getVideoInfo(filePath: string, format?: 'bmp' | 'bmp24'): Promise<FFmpegVideoInfo>;
|
|
getDuration(filePath: string): Promise<number>;
|
|
convertToNTSilkTct(inputPath: string, outputPath: string): Promise<void>;
|
|
decodeAudioToPCM(filePath: string, pcmPath: string, sampleRate?: number): Promise<{ result: boolean; sampleRate: number }>;
|
|
decodeAudioToFmt(filePath: string, pcmPath: string, format: string): Promise<{ channels: number; sampleRate: number; format: string }>;
|
|
}
|
|
|
|
let cachedAddon: FFmpegNativeAddon | null = null;
|
|
let cachedLoadError: string | null = null;
|
|
|
|
function addonFileName(): string {
|
|
return `ffmpegAddon.${process.platform}.${process.arch}.node`;
|
|
}
|
|
|
|
function addonSearchDirs(): string[] {
|
|
// Mirrors the hook injector's resolution strategy so the addon is found
|
|
// regardless of whether we're running from:
|
|
// 1. A released zip (dist/native/ffmpeg/<file>.node), where __dirname is
|
|
// the dist root.
|
|
// 2. The bundled build (repoRoot/dist/native/ffmpeg/<file>.node).
|
|
// 3. `tsx` dev mode (packages/runtime/native/ffmpeg/<file>.node), where
|
|
// __dirname is `packages/core/src/bridge/highway` — four levels deep
|
|
// under `packages/`. Note: the hook injector lives at
|
|
// `packages/core/src/hook` so it only needs 3 `..`; we need 4.
|
|
return [
|
|
path.resolve(__dirname, 'native', 'ffmpeg'),
|
|
path.resolve(__dirname, '..', '..', '..', '..', 'runtime', 'native', 'ffmpeg'),
|
|
path.resolve(process.cwd(), 'dist', 'native', 'ffmpeg'),
|
|
path.resolve(process.cwd(), 'packages', 'runtime', 'native', 'ffmpeg'),
|
|
];
|
|
}
|
|
|
|
function resolveAddonPath(): string | null {
|
|
const fileName = addonFileName();
|
|
for (const dir of addonSearchDirs()) {
|
|
const full = path.join(dir, fileName);
|
|
if (fs.existsSync(full)) return full;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Load the ffmpegAddon once and return a cached instance.
|
|
* Throws on first-use failure; subsequent calls replay the same error message
|
|
* so callers don't silently fall back.
|
|
*/
|
|
export function getFFmpegAddon(): FFmpegNativeAddon {
|
|
if (cachedAddon) return cachedAddon;
|
|
if (cachedLoadError) throw new Error(cachedLoadError);
|
|
|
|
const addonPath = resolveAddonPath();
|
|
if (!addonPath) {
|
|
cachedLoadError = `ffmpegAddon not found for ${process.platform}-${process.arch} (looked for ${addonFileName()})`;
|
|
throw new Error(cachedLoadError);
|
|
}
|
|
|
|
try {
|
|
const mod = { exports: {} as Record<string, unknown> };
|
|
process.dlopen(mod, addonPath);
|
|
cachedAddon = mod.exports as unknown as FFmpegNativeAddon;
|
|
return cachedAddon;
|
|
} catch (error) {
|
|
cachedLoadError = `failed to load ffmpegAddon (${addonPath}): ${error instanceof Error ? error.message : String(error)}`;
|
|
throw new Error(cachedLoadError);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check whether `filePath` is already an NT SILK container. The addon can
|
|
* read any input directly so the only reason we peek at the header is to
|
|
* skip the conversion (and the temp file) when the caller already fed us
|
|
* a ready-to-upload silk file.
|
|
*/
|
|
export function isSilkFile(filePath: string): boolean {
|
|
try {
|
|
const fd = fs.openSync(filePath, 'r');
|
|
try {
|
|
const buf = Buffer.alloc(10);
|
|
fs.readSync(fd, buf, 0, 10, 0);
|
|
const header = buf.toString();
|
|
return header.includes('#!SILK') || header.includes('\x02#!SILK');
|
|
} finally {
|
|
fs.closeSync(fd);
|
|
}
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export interface EncodeSilkResult {
|
|
/** Absolute path to the silk file ready for highway upload. */
|
|
path: string;
|
|
/** Voice duration in whole seconds (>= 1). */
|
|
duration: number;
|
|
/** `true` if we wrote a new silk file and the caller should unlink it. */
|
|
converted: boolean;
|
|
}
|
|
|
|
/**
|
|
* Convert `inputFile` to NT SILK when necessary and return the output path
|
|
* + duration. The caller is responsible for unlinking the returned path
|
|
* when `converted === true`.
|
|
*/
|
|
export async function encodeSilk(inputFile: string, tempDir: string): Promise<EncodeSilkResult> {
|
|
const addon = getFFmpegAddon();
|
|
|
|
// Already silk — trust the file, just measure duration via the addon.
|
|
if (isSilkFile(inputFile)) {
|
|
let duration = 1;
|
|
try {
|
|
duration = Math.max(1, Math.round(await addon.getDuration(inputFile)));
|
|
} catch {
|
|
// getDuration on a silk file occasionally fails on certain encodings;
|
|
// the upload path only needs a rough, non-zero value, so fall back
|
|
// to the size-based heuristic used by NapCat's audio.ts.
|
|
const stat = fs.statSync(inputFile);
|
|
duration = Math.max(1, Math.floor(stat.size / 1024 / 3));
|
|
}
|
|
return { path: inputFile, duration, converted: false };
|
|
}
|
|
|
|
fs.mkdirSync(tempDir, { recursive: true });
|
|
const outPath = path.join(tempDir, crypto.randomUUID());
|
|
await addon.convertToNTSilkTct(inputFile, outPath);
|
|
if (!fs.existsSync(outPath)) {
|
|
throw new Error('convertToNTSilkTct produced no output file');
|
|
}
|
|
const duration = Math.max(1, Math.round(await addon.getDuration(inputFile)));
|
|
return { path: outPath, duration, converted: true };
|
|
}
|
|
|
|
/** Default location for temporary silk files. */
|
|
export function defaultPttTempDir(): string {
|
|
return path.join(os.tmpdir(), 'snowluma-ptt');
|
|
}
|