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.

timeline: show a video coming on before seeking it (fixes the flaky video tests) (#31)

riverisbuilding9e68b610158a
diff --git a/.changeset/video-coming-on.md b/.changeset/video-coming-on.md
new file mode 100644
index 0000000..addff4f
--- /dev/null
+++ b/.changeset/video-coming-on.md
@@ -0,0 +1,5 @@
+---
+"openfilm": patch
+---
+
+A video clip's first frame in a render, an export or `look` is the one asked for. A video loaded ahead of its cut, hidden, and sought as it came on could keep the frame it had for a second or so, though the browser said the new one was shown: the same moment drew two pictures. It is now shown before it is sought.
diff --git a/packages/openfilm/src/timeline.mjs b/packages/openfilm/src/timeline.mjs
index ca3c27c..0518468 100644
--- a/packages/openfilm/src/timeline.mjs
+++ b/packages/openfilm/src/timeline.mjs
@@ -196,6 +196,9 @@ function restore(c) {
c.found = [];
}
+/** `n` frames of the page's rendering: what was changed before is committed to the compositor. */
+const frames = (n) => new Promise((done) => { const next = () => (--n > 0 ? requestAnimationFrame(next) : done()); requestAnimationFrame(next); });
+
const SAME_TIME = 1e-4; // s: a video's currentTime is kept to the microsecond, so a time set is read back a hair off
const PRESENT_MS = 1000; // how long a sought frame may take to be on screen before the seek is made again
const SEEK_TRIES = 3;
@@ -601,6 +604,7 @@ async function draw(t, live) {
if (c.kind === 'sound') return;
const on = !c.hidden && t >= c.at && t < c.end;
c.shown = on;
+ const appears = on && c.el.style.visibility !== 'visible';
c.el.style.visibility = on ? 'visible' : 'hidden';
if (on) fadeAt(c, t);
if (!on) {
@@ -618,6 +622,10 @@ async function draw(t, live) {
if (!c.el.paused) c.el.pause();
c.lastWant = want;
await loaded(c);
+ /* a video coming on: shown before it is sought. A frame made current in the moment it turns visible is said to
+ be presented, yet the picture keeps the frame it had for a second or so (Chromium, its frames sent apart from
+ the page's): the same t drew two pictures */
+ if (appears) await frames(2);
await showVideoAt(c.el, want, await frameOf(c));
return;
}
diff --git a/packages/openfilm/src/timeline.test.mjs b/packages/openfilm/src/timeline.test.mjs
index ae959c4..3f1e7cf 100644
--- a/packages/openfilm/src/timeline.test.mjs
+++ b/packages/openfilm/src/timeline.test.mjs
@@ -358,6 +358,39 @@ test('a video woken ahead while hidden, then shown: its old frame presented agai
}
});
+test('a video coming on is drawn visible before it is sought: a frame made current as it turns visible kept the old picture', { skip: !hasFfmpeg && 'no ffmpeg', timeout: 60_000 }, async () => {
+ const dir = footage('of-coming-on-');
+ /* `u` first, so how long the file's frames last is known before `v` comes on */
+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [
+ { id: 'u', src: 'a.mp4', time: [0, 1] }, { id: 'v', src: 'a.mp4', at: 2, time: [0, 4] },
+ ] }] }));
+ const s = await open(dir);
+ try {
+ const seen = await s.film.page.evaluate(async () => {
+ const v = document.getElementById('v');
+ await window.film.frame(0.5);
+ /* before the clip: its video is woken at its first frame, hidden */
+ await window.film.frame(1.5);
+ let drawn = 0;
+ let on = true;
+ const count = () => { drawn++; if (on) requestAnimationFrame(count); };
+ requestAnimationFrame(count);
+ let shown = null;
+ new MutationObserver(() => { if (shown == null && v.style.visibility === 'visible') shown = drawn; }).observe(v, { attributeFilter: ['style'] });
+ const seeks = [];
+ v.addEventListener('seeking', () => seeks.push({ drawn: drawn - shown, visible: v.style.visibility }));
+ await window.film.frame(2.75);
+ on = false;
+ return seeks;
+ });
+ assert.equal(seen.length, 1, JSON.stringify(seen));
+ assert.equal(seen[0].visible, 'visible');
+ assert.ok(seen[0].drawn >= 2, `drawn visible ${seen[0].drawn} times before the seek`);
+ } finally {
+ await s.close();
+ }
+});
+
test('an edit that splits, adds, removes or moves clips to a new track changes the film in place, without loading it again', { skip: !hasFfmpeg && 'no ffmpeg', timeout: 120_000 }, async () => {
const dir = footage('of-inplace-');
writeFileSync(join(dir, 'title.html'), page('title', 2));