Files
workinf_Blender_Wasm/web/app/src/sequencer/ffmpeg-movie-probe.worker.ts
mes123456 f68c6c9678
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled
Add full source parity validation workflow
2026-09-07 16:45:07 +08:00

90 lines
3.7 KiB
TypeScript

import { FFmpeg } from "@ffmpeg/ffmpeg";
interface ProbeRequest {
type: "probe" | "frame";
requestId: string;
bytes: ArrayBuffer;
mimeType: string;
width?: number;
height?: number;
}
interface ProbeResponse {
requestId: string;
status: "READY" | "BLOCKED";
reason: "RUNTIME_UNAVAILABLE" | "DECODE_FAILED" | null;
decoded: { width: number; height: number; durationMicros: number } | null;
data?: ArrayBuffer;
error?: string;
}
const ffmpeg = new FFmpeg();
let loaded = false;
const workerScope = self as unknown as { postMessage: (message: unknown, transfer?: Transferable[]) => void };
function response(requestId: string, patch: Omit<ProbeResponse, "requestId">): void {
workerScope.postMessage({ requestId, ...patch }, patch.data ? [patch.data] : []);
}
async function ensureLoaded(): Promise<void> {
if (loaded) return;
const vendorRoot = new URL(self.location.href).pathname.startsWith("/src/") ? "/src/vendor/ffmpeg" : "/vendor/ffmpeg";
await ffmpeg.load({
coreURL: `${vendorRoot}/ffmpeg-core.js`,
wasmURL: `${vendorRoot}/ffmpeg-core.wasm`,
});
loaded = true;
}
async function readText(path: string): Promise<string> {
const value = await ffmpeg.readFile(path, "utf8");
return typeof value === "string" ? value : new TextDecoder().decode(value);
}
async function probe(request: ProbeRequest): Promise<ProbeResponse["decoded"]> {
await ensureLoaded();
await ffmpeg.writeFile("input.movie", new Uint8Array(request.bytes));
await ffmpeg.ffprobe([
"-v", "error", "-show_entries", "stream=width,height:format=duration", "-of", "json", "input.movie", "-o", "probe.json",
]);
const parsed = JSON.parse(await readText("probe.json")) as { streams?: Array<{ width?: number; height?: number }>; format?: { duration?: string } };
const stream = parsed.streams?.find((candidate) => Number.isFinite(candidate.width) && Number.isFinite(candidate.height));
const width = Math.trunc(stream?.width ?? 0);
const height = Math.trunc(stream?.height ?? 0);
const duration = Number(parsed.format?.duration ?? 0);
if (width < 1 || height < 1 || !Number.isFinite(duration) || duration <= 0) return null;
return { width, height, durationMicros: Math.max(1, Math.round(duration * 1_000_000)) };
}
self.onmessage = (event: MessageEvent<ProbeRequest>) => {
const request = event.data;
void (async () => {
try {
const decoded = await probe(request);
if (!decoded) {
response(request.requestId, { status: "BLOCKED", reason: "DECODE_FAILED", decoded: null });
return;
}
if (request.type === "probe") {
response(request.requestId, { status: "READY", reason: null, decoded });
return;
}
const width = Math.max(1, Math.min(4096, Math.trunc(request.width ?? decoded.width)));
const height = Math.max(1, Math.min(4096, Math.trunc(request.height ?? decoded.height)));
await ffmpeg.exec([
"-i", "input.movie", "-vf", `scale=${width}:${height}`, "-frames:v", "1", "-f", "rawvideo", "-pix_fmt", "rgba", "frame.rgba",
]);
const data = await ffmpeg.readFile("frame.rgba");
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
if (bytes.byteLength !== width * height * 4) {
response(request.requestId, { status: "BLOCKED", reason: "DECODE_FAILED", decoded: null });
return;
}
response(request.requestId, { status: "READY", reason: null, decoded: { width, height, durationMicros: decoded.durationMicros }, data: bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer });
}
catch (error) {
response(request.requestId, { status: "BLOCKED", reason: "RUNTIME_UNAVAILABLE", decoded: null, error: error instanceof Error ? error.message : String(error) });
}
})();
};