Skip to content

The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.

Look: a film's contact sheet embeds its frames at cell size (#233)

Rickcba20bbc1fe7
diff --git a/.changeset/look-sheet-cell-size.md b/.changeset/look-sheet-cell-size.md
new file mode 100644
index 0000000..4f04f47
--- /dev/null
+++ b/.changeset/look-sheet-cell-size.md
@@ -0,0 +1,5 @@
+---
+"openfilm": patch
+---
+
+`look` of a 4K film no longer fails with "Target page, context or browser has been closed". A film's contact sheet now embeds each frame at the size of its cell, as a media file's sheet does, so twelve detailed 4K frames no longer pass the 100 MB a page can be given. The full frames are still compared as before.
diff --git a/packages/openfilm/src/look.mjs b/packages/openfilm/src/look.mjs
index acbec86..3fbb13d 100644
--- a/packages/openfilm/src/look.mjs
+++ b/packages/openfilm/src/look.mjs
@@ -120,7 +120,7 @@ export async function look(args, opts = {}) {
const wave = await waveform(s, from, to);
const marks = joinPieces(m.sounds).filter((c) => (c.at ?? 0) >= from && (c.at ?? 0) < to).map((c) => ({ t: c.at ?? 0, name: basename(c.src) }));
const file = join(dir, `${Number(from.toFixed(3))}-${fmt(to)}.png`);
- writeFileSync(file, await sheet(browser, shots, m, from, to, wave, marks));
+ writeFileSync(file, await sheet(browser, shots.map((x) => ({ ...x, png: cellPicture(x.png, dir) })), m, from, to, wave, marks));
files.push(file);
}
sounds.push(...listSounds(s, m, at != null ? [at, at] : range ?? [0, end], warnings));
@@ -275,6 +275,23 @@ async function waveform(s, from, to) {
}
}
+/**
+ * A frame as small as its cell on the sheet (CELL_WIDTH wide, alpha kept): the sheet is one HTML string given to the
+ * browser, and the full frames of a 4K stage (5 to 11 MB each as PNG) pass the 100 MB a page can be given. Through files,
+ * not a pipe, and given up on after 30 s: the frame as it is when ffmpeg cannot.
+ * @param {Buffer} png @param {string} dir
+ */
+function cellPicture(png, dir) {
+ const stem = join(dir, `.cell-${process.pid}-${hash(png)}`);
+ try {
+ writeFileSync(`${stem}-in.png`, png);
+ const r = spawnSync('ffmpeg', ['-v', 'error', '-y', '-i', `${stem}-in.png`, '-vf', `scale=${CELL_WIDTH}:-1,format=rgb24|rgba`, '-frames:v', '1', `${stem}.png`],
+ { timeout: 30_000, windowsHide: true });
+ return r.status === 0 ? readFileSync(`${stem}.png`) : png;
+ } catch { return png; }
+ finally { rmSync(`${stem}-in.png`, { force: true }); rmSync(`${stem}.png`, { force: true }); }
+}
+
async function sheet(browser, shots, meta, from, to, wave, marks, label = 'sound mix') {
const cols = shots.length <= 4 ? shots.length : shots.length <= 9 ? 3 : 4;
const cellW = CELL_WIDTH;
diff --git a/packages/openfilm/src/look.test.mjs b/packages/openfilm/src/look.test.mjs
index 0848b51..5a97253 100644
--- a/packages/openfilm/src/look.test.mjs
+++ b/packages/openfilm/src/look.test.mjs
@@ -80,6 +80,19 @@ test('a transparent WebM keeps its alpha in a media sheet', { timeout: 60_000, s
for (const cell of cells) assert.deepEqual(cell, { width: 480, height: 480, depth: 8, alpha: true });
});
+test("a film's sheet embeds its frames at cell size: a large stage's full frames would pass what a page can be given", { timeout: 90_000 }, async (t) => {
+ const { writeFileSync } = await import('node:fs');
+ const dir = mkdtempSync(join(tmpdir(), 'of-look-large-'));
+ t.after(() => rmSync(dir, { recursive: true, force: true }));
+ writeFileSync(join(dir, 'a.html'), '<!doctype html><body style="margin:0;background:#123"><script>window.film = { duration: 2, width: 1920, height: 1080, frame(t) { document.body.style.opacity = String(0.5 + t / 4); } };</script>');
+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 1920, h: 1080 }, tracks: [{ clips: [{ src: 'a.html' }] }] }));
+ const cells = sheetCells(t);
+ const r = await look([dir], { count: 2 });
+ assert.notEqual(r.ok, false, r.lines.join('\n'));
+ assert.equal(cells.length, 2);
+ for (const cell of cells) assert.deepEqual({ width: cell.width, height: cell.height }, { width: 480, height: 270 });
+});
+
/* the pages under test/: each one a mistake an agent makes (or one that only looks like one), and what look says */
const page = (name) => fileURLToPath(new URL(`../test/${name}`, import.meta.url));
const run = async (name) => { const r = await look([page(name)], {}); return { ok: r.ok !== false, text: r.lines.join('\n') }; };