Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Uploads take MKV and AVI clips; the server reads AVI headers

Claude9a9a6bb97a23
diff --git a/src/app/api/uploads/route.ts b/src/app/api/uploads/route.ts
index 8906ba5f..851e7978 100644
--- a/src/app/api/uploads/route.ts
+++ b/src/app/api/uploads/route.ts
@@ -1,7 +1,9 @@
import { contentTypeFor, saveFile } from "@/server/storage";
const MAX_BYTES = 200 * 1024 * 1024;
-const ALLOWED = /^(image\/(png|jpeg|webp|gif)|video\/(mp4|webm|quicktime)|audio\/(mpeg|wav|x-wav|ogg|flac|mp4|webm))$/;
+const ALLOWED = /^(image\/(png|jpeg|webp|gif)|video\/(mp4|webm|quicktime|x-matroska|x-msvideo)|audio\/(mpeg|wav|x-wav|ogg|flac|mp4|webm))$/;
+/** Names browsers give AVI and Matroska files, to the one Fuobe stores them under. */
+const VIDEO_ALIASES: Record<string, string> = { "video/avi": "video/x-msvideo", "video/msvideo": "video/x-msvideo", "video/vnd.avi": "video/x-msvideo", "video/mkv": "video/x-matroska" };
export async function POST(request: Request) {
const form = await request.formData().catch(() => null);
@@ -9,7 +11,8 @@ export async function POST(request: Request) {
if (!(file instanceof File)) return Response.json({ error: "No file" }, { status: 400 });
if (file.size > MAX_BYTES) return Response.json({ error: "File too large (200 MB max)" }, { status: 413 });
// Scripts often send no type (or octet-stream): go by the file extension then.
- const type = !file.type || file.type === "application/octet-stream" ? contentTypeFor(file.name, "") : file.type;
+ const given = !file.type || file.type === "application/octet-stream" ? contentTypeFor(file.name, "") : file.type;
+ const type = VIDEO_ALIASES[given] ?? given;
if (!ALLOWED.test(type)) return Response.json({ error: `Unsupported file type ${type || file.type || "unknown"}` }, { status: 415 });
const asset = saveFile({
data: Buffer.from(await file.arrayBuffer()),
diff --git a/src/lib/media-probe.ts b/src/lib/media-probe.ts
index 8c62f247..ae542713 100644
--- a/src/lib/media-probe.ts
+++ b/src/lib/media-probe.ts
@@ -8,7 +8,7 @@ export interface MediaInfo {
* "webm": read by the server's WebM / Matroska reader (src/server/webm-probe.ts), for the ComfyUI uploads;
* "image": a still's size (src/lib/image-size.ts), so "auto" can follow a start frame's shape.
*/
- container: "mp4" | "wav" | "flac" | "mp3" | "webm" | "image";
+ container: "mp4" | "wav" | "flac" | "mp3" | "webm" | "avi" | "image";
/** Seconds (the longest track). */
duration?: number;
/** Seconds of picture (the video track alone). */
diff --git a/src/server/avi-probe.ts b/src/server/avi-probe.ts
new file mode 100644
index 00000000..c95db016
--- /dev/null
+++ b/src/server/avi-probe.ts
@@ -0,0 +1,66 @@
+/**
+ * The header of an AVI file (RIFF 'AVI '): length, frame size, frame rate and whether it has a sound stream. Character
+ * Swap Studio takes AVI clips (Akatz Labs' tool does), so the server reads their length and size like an MP4's
+ * before anything runs. Only the 'hdrl' list at the start is read: the main header (avih) and each stream's header
+ * (strh). Pure.
+ */
+export interface AviInfo {
+ duration?: number;
+ width?: number;
+ height?: number;
+ fps?: number;
+ hasAudio: boolean;
+ hasVideo: boolean;
+}
+
+const tag = (b: Buffer, at: number) => b.toString("latin1", at, at + 4);
+
+/** The AVI header facts, or null when the bytes aren't an AVI. */
+export function probeAvi(buf: Buffer): AviInfo | null {
+ if (buf.length < 12 || tag(buf, 0) !== "RIFF" || !/^AVI[ X]$/.test(tag(buf, 8))) return null;
+ const info: AviInfo = { hasAudio: false, hasVideo: false };
+ let frameUs = 0;
+ let totalFrames = 0;
+ let videoRate: { scale: number; rate: number; length: number } | null = null;
+ // Walk the chunks inside one list (or the file), descending into LIST hdrl / strl.
+ const walk = (start: number, end: number) => {
+ let at = start;
+ while (at + 8 <= end) {
+ const id = tag(buf, at);
+ const size = buf.readUInt32LE(at + 4);
+ const body = at + 8;
+ const next = body + size + (size & 1);
+ if (size > end - body) break;
+ if (id === "LIST") {
+ const kind = tag(buf, body);
+ if (kind === "hdrl" || kind === "strl") walk(body + 4, body + size);
+ if (kind === "movi") return;
+ } else if (id === "avih" && size >= 40) {
+ frameUs = buf.readUInt32LE(body);
+ totalFrames = buf.readUInt32LE(body + 16);
+ info.width = buf.readUInt32LE(body + 32) || undefined;
+ info.height = buf.readUInt32LE(body + 36) || undefined;
+ } else if (id === "strh" && size >= 36) {
+ const type = tag(buf, body);
+ if (type === "auds") info.hasAudio = true;
+ if (type === "vids") {
+ info.hasVideo = true;
+ if (!videoRate) videoRate = { scale: buf.readUInt32LE(body + 20), rate: buf.readUInt32LE(body + 24), length: buf.readUInt32LE(body + 32) };
+ }
+ }
+ at = next;
+ }
+ };
+ walk(12, buf.length);
+ const v = videoRate as { scale: number; rate: number; length: number } | null;
+ // The video stream's own rate and length are exact; the main header's microseconds per frame are rounded.
+ if (v && v.scale > 0 && v.rate > 0) {
+ info.fps = Math.round((v.rate / v.scale) * 1000) / 1000;
+ const frames = v.length || totalFrames;
+ if (frames) info.duration = Math.round(((frames * v.scale) / v.rate) * 1000) / 1000;
+ } else if (frameUs > 0) {
+ info.fps = Math.round((1e6 / frameUs) * 1000) / 1000;
+ if (totalFrames) info.duration = Math.round(((totalFrames * frameUs) / 1e6) * 1000) / 1000;
+ }
+ return info;
+}
diff --git a/src/server/media-facts.ts b/src/server/media-facts.ts
index acef1b06..c7745cfb 100644
--- a/src/server/media-facts.ts
+++ b/src/server/media-facts.ts
@@ -1,5 +1,6 @@
import "server-only";
import { probeMedia } from "@/lib/media-probe";
+import { probeAvi } from "./avi-probe";
import { probeWebm } from "./webm-probe";
/** What a clip's header says: length, displayed size, frame rate and whether it has sound. */
@@ -12,7 +13,7 @@ export interface SourceInfo {
}
/**
- * The header facts of a clip in memory: MP4 / MOV first, then WebM / Matroska. A file neither reader
+ * The header facts of a clip in memory: MP4 / MOV first, then WebM / Matroska, then AVI. A file no reader
* knows comes back empty (callers fall back to the browser's numbers or safe defaults).
*/
export function mediaFacts(data: Uint8Array): SourceInfo {
@@ -23,8 +24,11 @@ export function mediaFacts(data: Uint8Array): SourceInfo {
info = null;
}
if (!info) {
- const webm = probeWebm(Buffer.from(data.buffer, data.byteOffset, data.byteLength));
- return webm ? { duration: webm.duration, width: webm.width, height: webm.height, fps: webm.fps, hasAudio: webm.hasAudio } : {};
+ const buf = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
+ const webm = probeWebm(buf);
+ if (webm) return { duration: webm.duration, width: webm.width, height: webm.height, fps: webm.fps, hasAudio: webm.hasAudio };
+ const avi = probeAvi(buf);
+ return avi ? { duration: avi.duration, width: avi.width, height: avi.height, fps: avi.fps, hasAudio: avi.hasAudio } : {};
}
return { duration: info.videoDuration ?? info.duration, width: info.width, height: info.height, fps: info.fps, hasAudio: info.hasAudio };
}
diff --git a/src/server/providers/comfyui.ts b/src/server/providers/comfyui.ts
index e1d89ee2..121ddab5 100644
--- a/src/server/providers/comfyui.ts
+++ b/src/server/providers/comfyui.ts
@@ -53,6 +53,7 @@ import { probeMedia, type MediaInfo } from "@/lib/media-probe";
import { ocrText, parseOcrFrame, parseOcrLines, type OcrFrame } from "@/lib/ocr";
import { getSetting, isCandidate, savedSetting } from "../settings";
import { decodePng, encodePng } from "../png";
+import { probeAvi } from "../avi-probe";
import { probeWebm } from "../webm-probe";
import { adoptFile, readRef, scratchDir } from "../storage";
import { zipToFile } from "../zip";
@@ -491,6 +492,14 @@ function webmInfo(data: Buffer): MediaInfo | null {
return { container: "webm", duration: w.duration, videoDuration: w.hasVideo ? w.duration : undefined, width: w.width, height: w.height, fps: w.fps, frames, hasVideo: w.hasVideo, hasAudio: w.hasAudio };
}
+/** An AVI clip's header facts in MediaInfo's shape (src/server/avi-probe.ts; Character Swap Studio takes AVI). */
+function aviInfo(data: Buffer): MediaInfo | null {
+ const a = probeAvi(data);
+ if (!a) return null;
+ const frames = a.duration && a.fps ? Math.round(a.duration * a.fps) : undefined;
+ return { container: "avi", duration: a.duration, videoDuration: a.hasVideo ? a.duration : undefined, width: a.width, height: a.height, fps: a.fps, frames, hasVideo: a.hasVideo, hasAudio: a.hasAudio };
+}
+
/** The header facts of a file this server uploaded to ComfyUI (a still has only its size; undefined for unreadable containers). */
export function mediaInfoFor(name: unknown): MediaInfo | undefined {
return typeof name === "string" ? globalThis.__fuobeComfyMedia?.get(name) : undefined;
@@ -1237,8 +1246,8 @@ async function uploadTo(worker: ComfyWorker, data: Buffer, contentType: string,
const j = (await res.json()) as { name: string; subfolder?: string };
const uploaded = j.subfolder ? `${j.subfolder}/${j.name}` : j.name;
// Remember a clip's / track's header facts (length, size, fps, audio) for prepareWorkflow.
- if (MEDIA_FILE.test(contentType) || MEDIA_EXT.test(name) || /\.(webm|mkv)$/i.test(name)) {
- const info = probeMedia(data) ?? webmInfo(data);
+ if (MEDIA_FILE.test(contentType) || MEDIA_EXT.test(name) || /\.(webm|mkv|avi)$/i.test(name)) {
+ const info = probeMedia(data) ?? webmInfo(data) ?? aviInfo(data);
if (info) rememberMedia(uploaded, info);
} else if (/^image\//.test(contentType)) {
// A start frame's size: "auto" makes an image-to-video clip in the picture's own shape.
diff --git a/src/server/storage.ts b/src/server/storage.ts
index 0d29d801..ec6bc6b0 100644
--- a/src/server/storage.ts
+++ b/src/server/storage.ts
@@ -37,6 +37,8 @@ const EXT_BY_TYPE: Record<string, string> = {
"model/x-ksplat": "ksplat",
// Pro exports: FFV1 in Matroska, EXR frames (ComfyUI's /view says image/aces) and their zip.
"video/x-matroska": "mkv",
+ // Character Swap Studio takes AVI clips (Akatz Labs' tool does).
+ "video/x-msvideo": "avi",
"image/x-exr": "exr",
"image/aces": "exr",
"application/zip": "zip",
@@ -68,6 +70,7 @@ const TYPE_BY_EXT: Record<string, string> = {
spz: "model/x-spz",
ksplat: "model/x-ksplat",
mkv: "video/x-matroska",
+ avi: "video/x-msvideo",
exr: "image/x-exr",
zip: "application/zip",
};
diff --git a/tests/avi-probe.test.ts b/tests/avi-probe.test.ts
new file mode 100644
index 00000000..b7678da5
--- /dev/null
+++ b/tests/avi-probe.test.ts
@@ -0,0 +1,54 @@
+import { describe, expect, it } from "vitest";
+import { probeAvi } from "@/server/avi-probe";
+import { mediaFacts } from "@/server/media-facts";
+
+// AVI headers (Character Swap Studio imports AVI clips): built here chunk by chunk, as FFmpeg writes them.
+const chunk = (id: string, body: Buffer) => {
+ const head = Buffer.alloc(8);
+ head.write(id, 0, "latin1");
+ head.writeUInt32LE(body.length, 4);
+ return Buffer.concat([head, body, body.length & 1 ? Buffer.alloc(1) : Buffer.alloc(0)]);
+};
+const list = (kind: string, ...items: Buffer[]) => chunk("LIST", Buffer.concat([Buffer.from(kind, "latin1"), ...items]));
+function avih(o: { us: number; frames: number; width: number; height: number }) {
+ const b = Buffer.alloc(56);
+ b.writeUInt32LE(o.us, 0);
+ b.writeUInt32LE(o.frames, 16);
+ b.writeUInt32LE(o.width, 32);
+ b.writeUInt32LE(o.height, 36);
+ return chunk("avih", b);
+}
+function strh(type: "vids" | "auds", scale: number, rate: number, length: number) {
+ const b = Buffer.alloc(56);
+ b.write(type, 0, "latin1");
+ b.writeUInt32LE(scale, 20);
+ b.writeUInt32LE(rate, 24);
+ b.writeUInt32LE(length, 32);
+ return chunk("strh", b);
+}
+const riff = (...items: Buffer[]) => {
+ const body = Buffer.concat([Buffer.from("AVI ", "latin1"), ...items]);
+ return chunk("RIFF", body);
+};
+
+describe("AVI headers", () => {
+ it("read the length, size, frame rate and sound from the stream headers", () => {
+ const file = riff(list("hdrl", avih({ us: 33367, frames: 120, width: 1280, height: 720 }), list("strl", strh("vids", 1001, 30000, 120)), list("strl", strh("auds", 1, 48000, 192000))), list("movi", Buffer.alloc(16)));
+ expect(probeAvi(file)).toEqual({ hasVideo: true, hasAudio: true, width: 1280, height: 720, fps: 29.97, duration: 4.004 });
+ expect(mediaFacts(file)).toEqual({ duration: 4.004, width: 1280, height: 720, fps: 29.97, hasAudio: true });
+ });
+
+ it("fall back to the main header, and say a silent clip has no sound", () => {
+ const file = riff(list("hdrl", avih({ us: 40000, frames: 250, width: 640, height: 480 }), list("strl", strh("vids", 0, 0, 0))));
+ expect(probeAvi(file)).toEqual({ hasVideo: true, hasAudio: false, width: 640, height: 480, fps: 25, duration: 10 });
+ });
+
+ it("isn't fooled by other RIFF files or short bytes", () => {
+ const wav = chunk("RIFF", Buffer.concat([Buffer.from("WAVE", "latin1"), chunk("fmt ", Buffer.alloc(16))]));
+ expect(probeAvi(wav)).toBeNull();
+ expect(probeAvi(Buffer.from("RIFF"))).toBeNull();
+ // A truncated header reads what is there.
+ const cut = riff(list("hdrl", avih({ us: 41667, frames: 24, width: 320, height: 180 }))).subarray(0, 40);
+ expect(probeAvi(cut)).toMatchObject({ hasVideo: false });
+ });
+});