Skip to content
Python

history: an mp4 output is a video even when the SDK calls it an image

neuregexbea5980fda37
diff --git a/server/history.ts b/server/history.ts
index fbc8d7a..70a40e7 100644
--- a/server/history.ts
+++ b/server/history.ts
@@ -79,6 +79,18 @@ function safeExt(name: string | undefined, type: string | undefined): string {
return EXT[(type ?? "").split(";")[0].trim()] ?? ".bin";
}
+const VIDEO_EXT = new Set([".mp4", ".webm", ".mov", ".mkv", ".gif"]);
+const AUDIO_EXT = new Set([".mp3", ".wav", ".flac", ".ogg", ".m4a", ".opus"]);
+
+/** What the file is, by its extension first: core SaveVideo and CreateVideo report their mp4 under
+ * `images` (with `animated: true`), so the SDK calls it an image and the web would draw it in an <img>.
+ * (First web run of morph, 23 oct: a blank card for a good video.) */
+function mediaKind(ext: string, sdkType: string): "image" | "video" | "audio" {
+ if (VIDEO_EXT.has(ext)) return "video";
+ if (AUDIO_EXT.has(ext)) return "audio";
+ return sdkType === "video" || sdkType === "audio" ? sdkType : "image";
+}
+
function normStatus(s: string): Status {
if (s === "canceling") return "running";
return (["queued", "running", "succeeded", "failed", "canceled", "expired"] as string[]).includes(s) ? (s as Status) : "running";
@@ -257,9 +269,10 @@ export class History {
if (!MEDIA.has(out.type)) continue;
try {
const bytes = await out.toBytes();
- const file = `out-${String(++n).padStart(2, "0")}${safeExt(out.name, out.contentType)}`;
+ const ext = safeExt(out.name, out.contentType);
+ const file = `out-${String(++n).padStart(2, "0")}${ext}`;
writeFileSync(join(folder, file), bytes);
- entry.outputs.push({ file, type: out.type, name: out.name, node: out.nodeId });
+ entry.outputs.push({ file, type: mediaKind(ext, out.type), name: out.name, node: out.nodeId });
} catch (e) {
entry.error = { message: `could not download ${out.name}: ${(e as Error).message}` };
}
@@ -367,7 +380,10 @@ export class History {
private read(id: string): Entry | null {
if (!ID.test(id)) return null;
try {
- return JSON.parse(readFileSync(join(this.dir, id, "meta.json"), "utf-8")) as Entry;
+ const entry = JSON.parse(readFileSync(join(this.dir, id, "meta.json"), "utf-8")) as Entry;
+ // entries saved before mediaKind() carry the SDK's type: fix them as they are read
+ for (const o of entry.outputs ?? []) o.type = mediaKind(extname(o.file).toLowerCase(), o.type);
+ return entry;
} catch {
return null;
}
diff --git a/test/history.test.ts b/test/history.test.ts
index c6bd33a..5f7bf80 100644
--- a/test/history.test.ts
+++ b/test/history.test.ts
@@ -23,6 +23,8 @@ function fakeJob(end: "succeeded" | "failed", events: ComfyEvent[] = []): JobLik
get outputs() {
return status === "succeeded"
? [{ name: "ComfyUI_00001_.png", type: "image", nodeId: "9", contentType: "image/png", toBytes: async () => PNG },
+ // core SaveVideo reports its mp4 under `images`, so the SDK says "image"
+ { name: "video_00001_.mp4", type: "image", nodeId: "68", contentType: "video/mp4", toBytes: async () => PNG },
{ name: "latent.bin", type: "latent", nodeId: "3", contentType: "application/octet-stream", toBytes: async () => new Uint8Array() }]
: [];
},
@@ -66,7 +68,8 @@ test("following a job stores its media outputs and timings", async () => {
const done1 = await history.follow(entry, fakeJob("succeeded", [progress]));
assert.equal(done1.status, "succeeded");
assert.equal(done1.job, "job-1");
- assert.deepEqual(done1.outputs.map((o) => [o.file, o.type]), [["out-01.png", "image"]], "latents are skipped");
+ assert.deepEqual(done1.outputs.map((o) => [o.file, o.type]), [["out-01.png", "image"], ["out-02.mp4", "video"]],
+ "latents are skipped; an mp4 is a video whatever the SDK called it");
assert.ok(existsSync(join(dir, entry.id, "out-01.png")));
assert.equal(done1.seconds, 2.5);
assert.equal(history.get(entry.id)!.progress, null, "live progress is dropped once the run ends");