The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.
Render: footage the browser cannot decode is copied in a form it plays, instead of drawn black (#277)
Mouse_Hong015a240c8fe7
diff --git a/.changeset/black-footage.md b/.changeset/black-footage.mdnew file mode 100644index 0000000..4f18559--- /dev/null+++ b/.changeset/black-footage.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Exports: footage the browser shows no picture of is copied in a form it plays, instead of being drawn black. ProRes with sound, which most cameras record, plays its sound and shows no picture, without an error: it gets a copy too, as HEVC on a computer without a decoder for it already did.diff --git a/packages/openfilm/src/seekable.mjs b/packages/openfilm/src/seekable.mjsindex c57254b..8916340 100644--- a/packages/openfilm/src/seekable.mjs+++ b/packages/openfilm/src/seekable.mjs@@ -146,16 +146,20 @@ export function filmVideos(root, film) {/*** In a page: whether the browser shows the first frame of the video at `url`. One it does not decode (ProRes; HEVC- * without a chip for it) fails at once, or never shows a frame.+ * without a chip for it) fails at once, or loads it and shows nothing: a file it can read but not decode has no+ * picture, and a film drawn from it is black.* @param {string} url*/-const firstFrame = (url) => new Promise((done) => {+export const firstFrame = (url) => new Promise((done) => {const video = document.createElement('video');video.muted = true;video.preload = 'auto';const end = (/** @type {boolean} */ shown) => { clearTimeout(timer); video.removeAttribute('src'); video.load(); done(shown); };const timer = setTimeout(() => end(false), 10_000);- video.addEventListener('loadeddata', () => end(true), { once: true });+ /* `loadeddata` says the file loaded, not that a picture came: a browser that cannot decode it (HEVC without a+ decoder for it) says it too, and shows nothing. A picture is what plays: without one, the file needs a copy+ the browser can play */+ video.addEventListener('loadeddata', () => end(video.videoWidth > 0), { once: true });video.addEventListener('error', () => end(false), { once: true });video.src = url;});diff --git a/packages/openfilm/src/seekable.test.mjs b/packages/openfilm/src/seekable.test.mjsindex 6ece16c..927391a 100644--- a/packages/openfilm/src/seekable.test.mjs+++ b/packages/openfilm/src/seekable.test.mjs@@ -3,10 +3,11 @@ import assert from 'node:assert/strict';import { execFile, execFileSync } from 'node:child_process';import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';-import { join } from 'node:path';+import { join, relative, sep } from 'node:path';import { filmHtml } from './film-doc.mjs';+import { launch, serve } from './host.mjs';import { render } from './render.mjs';-import { copyNeed, madeCopy, seekableCopies, slowSeeking } from './seekable.mjs';+import { copyNeed, firstFrame, madeCopy, seekableCopies, slowSeeking } from './seekable.mjs';import { look } from './look.mjs';const hasFfmpeg = (() => { try { execFileSync('ffmpeg', ['-version'], { stdio: 'ignore' }); return true; } catch { return false; } })();@@ -15,6 +16,8 @@ const ffmpeg = { skip: !hasFfmpeg && 'no ffmpeg', timeout: 120_000 };/** 4 s at 30 fps with a key frame every `gop` frames. */const clip = (file, gop) => execFileSync('ffmpeg', ['-v', 'error', '-y', '-f', 'lavfi', '-i', 'testsrc2=size=320x180:rate=30', '-t', '4','-c:v', 'libx264', '-preset', 'ultrafast', '-g', String(gop), '-keyint_min', String(gop), '-sc_threshold', '0', '-pix_fmt', 'yuv420p', file]);+/** The codecs of a file's sound streams, as ffprobe names them: '' when it has none. */+const soundStreams = (file) => execFileSync('ffprobe', ['-v', 'error', '-select_streams', 'a', '-show_entries', 'stream=codec_type', '-of', 'csv=p=0', file]).toString().trim();/** A video's frame times, as its packets give them. */const times = (file) => execFileSync('ffprobe', ['-v', 'error', '-select_streams', 'v:0', '-show_entries', 'packet=pts_time', '-of', 'csv=p=0', file]).toString().trim().split('\n').map(Number).sort((a, b) => a - b);@@ -51,6 +54,33 @@ test('a render serves the film\'s slow-seeking videos as their copies', ffmpeg,}});+test('footage the browser plays the sound of but shows no picture of, ProRes with sound, gets a copy it plays', ffmpeg, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-seekable-nopic-'));+ const site = await serve(dir);+ const browser = await launch();+ try {+ /* ProRes with AAC sound, as cameras record it: the browser plays the sound, shows no picture and reports no error */+ execFileSync('ffmpeg', ['-v', 'error', '-y', '-f', 'lavfi', '-i', 'testsrc2=size=320x180:rate=30', '-f', 'lavfi', '-i', 'sine=frequency=440:duration=1',+ '-t', '1', '-c:v', 'prores_ks', '-profile:v', '3', '-c:a', 'aac', '-shortest', join(dir, 'prores-sound.mov')]);+ clip(join(dir, 'fast.mp4'), 5);+ const page = await browser.newPage();+ const plays = (file) => page.evaluate(firstFrame, `${site.url}/${relative(dir, file).split(sep).map(encodeURIComponent).join('/')}`);+ assert.equal(await plays(join(dir, 'prores-sound.mov')), false, 'a file it shows no picture of is not playable');+ assert.equal(await plays(join(dir, 'fast.mp4')), true, 'a video with a picture plays');+ /* asked as a render asks it: only the footage without a picture needs a copy */+ const copies = await seekableCopies([join(dir, 'prores-sound.mov'), join(dir, 'fast.mp4')], dir, { plays, slow: false });+ assert.deepEqual([...copies.keys()], [join(dir, 'prores-sound.mov')]);+ const copy = /** @type {string} */ (copies.get(join(dir, 'prores-sound.mov')));+ assert.equal(await plays(copy), true, 'the copy plays');+ assert.deepEqual(times(copy), times(join(dir, 'prores-sound.mov')), 'the same frames at the same times');+ assert.equal(soundStreams(copy), '', 'the copy is the picture only: the sound is still mixed from the original');+ } finally {+ await browser.close();+ await site.close();+ rmSync(dir, { recursive: true, force: true });+ }+});+test('footage the browser does not decode gets a copy it plays, with its alpha when it has one; footage it decodes is not asked about', ffmpeg, async () => {const dir = mkdtempSync(join(tmpdir(), 'of-seekable-unplayable-'));try {@@ -82,11 +112,16 @@ test('footage the browser does not decode gets a copy it plays, with its alpha wtest('a render shows ProRes footage, which no browser decodes, through its copy', ffmpeg, async () => {const dir = mkdtempSync(join(tmpdir(), 'of-seekable-prores-render-'));try {- execFileSync('ffmpeg', ['-v', 'error', '-y', '-f', 'lavfi', '-i', 'color=c=0x2060c0:size=320x180:rate=30', '-t', '1', '-c:v', 'prores_ks', '-profile:v', '3', join(dir, 'a.mov')]);+ /* ProRes with the sound a camera records with it: a browser plays the sound, shows no picture and reports no error */+ execFileSync('ffmpeg', ['-v', 'error', '-y', '-f', 'lavfi', '-i', 'color=c=0x2060c0:size=320x180:rate=30', '-f', 'lavfi', '-i', 'sine=frequency=440:duration=1',+ '-t', '1', '-c:v', 'prores_ks', '-profile:v', '3', '-c:a', 'aac', '-shortest', join(dir, 'a.mov')]);writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'v', src: 'a.mov', at: 0, time: [0, 1] }] }] }));const started = Date.now();const done = await render([dir, '0-0.5'], { out: join(dir, 'out.mp4'), workers: 1, silent: true, onProgress() {} });assert.ok(Date.now() - started < 60_000, 'no frame waits on a video that never shows');+ const [made] = readdirSync(join(dir, '.film/cache/seek')).filter((f) => f.endsWith('.mp4') && !f.includes('.part'));+ assert.ok(made, 'the footage is drawn through its copy');+ assert.equal(soundStreams(join(dir, '.film/cache/seek', made)), '', 'the copy is the picture only');const [r, g, b] = execFileSync('ffmpeg', ['-v', 'error', '-ss', '0.2', '-i', done.out, '-frames:v', '1', '-vf', 'crop=4:4:160:90,format=rgb24', '-f', 'rawvideo', '-']);assert.ok(Math.abs(r - 0x20) < 16 && Math.abs(g - 0x60) < 16 && Math.abs(b - 0xc0) < 16, `the footage's colour: ${r},${g},${b}`);} finally {