Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/minimax/src/preview.ts

Raw
import { spawn } from "node:child_process";
import { findExecutable } from "./pi-ext-executable.ts";

/** Render-only poster strip; lives in details, never in model text or structuredContent. */
export interface Preview {
	mimeType: "image/jpeg";
	data: string;
}

export const PREVIEW_KEY = "preview";
const FRAMES = 4;
const FRAME_HEIGHT = 128;
const MAX_BYTES = 72 * 1024; // ≤ 96 KiB base64 per retrieved video in session history.
const TIMEOUT_MS = 15_000;

/**
 * Sample FRAMES evenly across the clip into one horizontal JPEG strip.
 * Best effort: missing ffmpeg, timeout, cancellation, oversize or bad output → undefined.
 */
export async function posterStrip(
	path: string,
	durationSeconds: number,
	signal?: AbortSignal,
	ffmpeg: string | undefined = findExecutable(["ffmpeg", "ffmpeg.exe"]),
): Promise<Preview | undefined> {
	if (!ffmpeg || signal?.aborted) return undefined;
	const seconds =
		Number.isFinite(durationSeconds) && durationSeconds > 0
			? durationSeconds
			: 4;
	const combined = AbortSignal.any([
		AbortSignal.timeout(TIMEOUT_MS),
		...(signal ? [signal] : []),
	]);
	return new Promise((resolve) => {
		let settled = false;
		const finish = (value: Preview | undefined) => {
			if (settled) return;
			settled = true;
			resolve(value);
		};
		let child: ReturnType<typeof spawn>;
		try {
			child = spawn(
				ffmpeg,
				[
					"-v",
					"error",
					"-nostdin",
					"-i",
					path,
					"-vf",
					`fps=${(FRAMES / seconds).toFixed(4)},scale=-2:${FRAME_HEIGHT},tile=${FRAMES}x1`,
					"-frames:v",
					"1",
					"-q:v",
					"6",
					"-f",
					"image2pipe",
					"-vcodec",
					"mjpeg",
					"pipe:1",
				],
				{ stdio: ["ignore", "pipe", "ignore"], signal: combined, shell: false },
			);
		} catch {
			finish(undefined);
			return;
		}
		const chunks: Buffer[] = [];
		let bytes = 0;
		child.stdout?.on("data", (chunk: Buffer) => {
			bytes += chunk.length;
			if (bytes > MAX_BYTES) {
				child.kill();
				finish(undefined);
			} else chunks.push(chunk);
		});
		child.on("error", () => finish(undefined));
		child.on("close", (code) => {
			const jpeg = Buffer.concat(chunks, Math.min(bytes, MAX_BYTES));
			finish(
				code === 0 &&
					jpeg.length > 3 &&
					jpeg.subarray(0, 3).equals(Buffer.from([255, 216, 255]))
					? { mimeType: "image/jpeg", data: jpeg.toString("base64") }
					: undefined,
			);
		});
	});
}

export function isPreview(value: unknown): value is Preview {
	const v = value as Partial<Preview> | null;
	return (
		!!v &&
		v.mimeType === "image/jpeg" &&
		typeof v.data === "string" &&
		v.data.length > 0 &&
		v.data.length <= Math.ceil((MAX_BYTES * 4) / 3) + 4
	);
}