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.

Exact frames: a video's old frame shown again is not taken for the one sought (#12)

riverisbuilding7f21b61f8446
diff --git a/.changeset/video-stale-frame.md b/.changeset/video-stale-frame.md
new file mode 100644
index 0000000..1efa84f
--- /dev/null
+++ b/.changeset/video-stale-frame.md
@@ -0,0 +1,5 @@
+---
+"openfilm": patch
+---
+
+Exact frames (render, look, Studio's exports): a video sought as it comes on waits for its own frame on screen. On a slow machine the old frame, shown again as the video turned visible, could be taken for it, and `look` reported the same t drawing two different pictures.
diff --git a/packages/openfilm/src/timeline.mjs b/packages/openfilm/src/timeline.mjs
index ac121fa..9e2004b 100644
--- a/packages/openfilm/src/timeline.mjs
+++ b/packages/openfilm/src/timeline.mjs
@@ -180,7 +180,8 @@ const SAME_TIME = 1e-4; // s: a video's currentTime is kept to the microsecond,
* Put a video on the frame at `t` and wait until that frame is the one on screen. `seeked` fires when the decoder
* has the frame, not when the compositor shows it: captured right after, the picture is still the previous one
* (often a stretch back, after a long seek), and the same t drew different pictures. The frame callback is the
- * moment the new frame is presented.
+ * moment the new frame is presented; one before the seek is done is still the old frame (shown again as its video turns
+ * visible), and is not taken for it.
*/
async function showVideoAt(v, t) {
/* a seek still under way (the start a video was woken at, see wake): its `seeked` and its frame would be taken for
@@ -189,8 +190,17 @@ async function showVideoAt(v, t) {
/* the same time again (to the microsecond the element keeps): no seek, nothing new to present (a seek there would
present nothing, and the wait would run out) */
if (!v.seeking && Math.abs(v.currentTime - t) < SAME_TIME && v.readyState >= 2) return;
+ /* the old frame is the one nearer where the video was than where it is sought (a frame there would look the same) */
+ const before = v.currentTime;
+ let sought = false;
const presented = typeof v.requestVideoFrameCallback === 'function'
- ? new Promise((done) => { v.requestVideoFrameCallback(() => done()); setTimeout(done, 3000); })
+ ? new Promise((done) => {
+ const next = () => v.requestVideoFrameCallback((now, frame) => {
+ if (sought || Math.abs(frame.mediaTime - t) <= Math.abs(frame.mediaTime - before)) done(); else next();
+ });
+ next();
+ setTimeout(done, 3000);
+ })
: null;
v.currentTime = t;
await new Promise((done) => {
@@ -199,6 +209,7 @@ async function showVideoAt(v, t) {
v.addEventListener('loadeddata', done, { once: true });
setTimeout(done, 10000);
});
+ sought = true;
if (presented) await presented;
}
diff --git a/packages/openfilm/src/timeline.test.mjs b/packages/openfilm/src/timeline.test.mjs
index c3861e9..65abf3e 100644
--- a/packages/openfilm/src/timeline.test.mjs
+++ b/packages/openfilm/src/timeline.test.mjs
@@ -240,6 +240,38 @@ test('a video loaded only when its frame is asked draws the same picture as one
}
});
+test('an exact frame of a video sought there waits for its own frame on screen, not one presented before the seek was done', { skip: !hasFfmpeg && 'no ffmpeg', timeout: 60_000 }, async () => {
+ const dir = footage('of-sought-frame-');
+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'v', src: 'a.mp4', at: 1, time: [0, 4] }] }] }));
+ const s = await open(dir);
+ try {
+ const seen = await s.film.page.evaluate(async () => {
+ const v = document.querySelector('video');
+ await window.film.frame(1);
+ /* every frame presented from here, and one more presentation before the seek is done: the old frame shown again
+ (a video turning visible), as a slow machine has it */
+ const real = v.requestVideoFrameCallback.bind(v);
+ const presented = [];
+ const watch = () => real((now, meta) => { presented.push(meta.mediaTime); watch(); });
+ watch();
+ const old = v.currentTime;
+ let early = true;
+ v.requestVideoFrameCallback = (callback) => {
+ if (!early) return real(callback);
+ early = false;
+ queueMicrotask(() => callback(performance.now(), { mediaTime: old }));
+ return 0;
+ };
+ await window.film.frame(3.25);
+ return presented;
+ });
+ /* 10 frames a second: the frame at 2.25 s of the video is the one from 2.2 s */
+ assert.ok(seen.some((m) => Math.abs(m - 2.2) < 0.01), `the frame at 2.25 s was on screen when the frame was done: ${seen}`);
+ } 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));