Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(webv2): show the video's own first frame in the preview, not the 256px poster

Lincoln Stein19355a6d1c24
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx
index 41b41322c7..0c2bcf5ee9 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrame.tsx
@@ -383,6 +383,42 @@ const PreviewVideo = ({
void refresh.then(finishRefresh, () => finishRefresh(false));
}, [isItemCurrent, publishCopyAvailability, source.itemKey]);
+ // The preview keeps `poster` — the 256px WebP gallery thumbnail — as its instant
+ // placeholder, but the browser goes on showing it well after the video's own first frame is
+ // decoded and ready: by `loadedmetadata` the element is already at readyState 4 with
+ // `resize` fired, and the poster survives only because the HTML spec's *show-poster flag* is
+ // still set. Upscaled across the whole stage, that 256px still is what reads as fuzzy.
+ // Seeking clears the show-poster flag, and that — not any new decoding — is what puts the
+ // native-resolution frame on screen. Measured on a 1280x720 clip, Laplacian variance of the
+ // painted stage goes from 254 to 576.
+ //
+ // Dropping the `poster` attribute instead does not work: the flag stays set with nothing
+ // left to draw, and the stage paints black until the user presses play.
+ //
+ // The nudge is not free. It pulls more of the clip than `preload="metadata"` alone would —
+ // measured on a 7MB/20s clip, one 1MB range request becomes three (~3.1MB), which then
+ // settles rather than running away. A full-resolution poster served by the backend would buy
+ // the same frame without the extra range traffic, if that ever becomes worth the endpoint.
+ //
+ // The guard skips the nudge when the user hit play before metadata arrived — measured, that
+ // is the one interleaving where `loadedmetadata` observes `paused === false`. It deliberately
+ // does not claim to protect a mid-playback viewer from a reload: `load()` is the only thing
+ // that refires `loadedmetadata` on this element, and it has already reset the position to 0
+ // and `paused` to true by the time the handler runs. The `currentTime` clause is
+ // belt-and-braces for an engine that does not reset it.
+ const handleFirstFrameSeek = useCallback(() => {
+ const video = videoRef.current;
+
+ if (!video || !video.paused || video.currentTime > 0) {
+ return;
+ }
+
+ try {
+ video.currentTime = FIRST_FRAME_SEEK_SECONDS;
+ } catch {
+ // Some browsers throw if the seekable range is not populated yet; the poster stays up.
+ }
+ }, []);
const handleRetry = useCallback(() => {
const video = videoRef.current;
@@ -429,6 +465,7 @@ const PreviewVideo = ({
onEmptied={publishCopyAvailability}
onError={handleVideoError}
onLoadedData={publishCopyAvailability}
+ onLoadedMetadata={handleFirstFrameSeek}
onPlaying={publishCopyAvailability}
onResize={publishCopyAvailability}
onSeeked={publishCopyAvailability}
@@ -507,6 +544,10 @@ const encodeCanvasPng = (canvas: HTMLCanvasElement): Promise<Blob | null> =>
const isCanvasSecurityError = (error: unknown): boolean =>
error instanceof DOMException && error.name === 'SecurityError';
+// Far enough from zero for browsers to treat it as a real seek, small enough that playback
+// still starts on frame 0 at any sane frame rate.
+const FIRST_FRAME_SEEK_SECONDS = 0.0001;
+
const VIDEO_STYLE: CSSProperties = {
display: 'block',
height: '100%',
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrameVideo.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrameVideo.browser.test.tsx
index 2001f676b3..fa138df387 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrameVideo.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewFrameVideo.browser.test.tsx
@@ -161,6 +161,35 @@ describe('PreviewFrame native video arm', () => {
expect(host?.querySelector('img[alt="Video clip.mp4"]')).toBeNull();
});
+ it("seeks past the poster to the video's own first frame once metadata lands", async () => {
+ await renderVideo();
+ const video = getVideo();
+ const position = stubPlaybackPosition(video);
+
+ await interact(() => video.dispatchEvent(new Event('loadedmetadata')));
+
+ // `preload="metadata"` alone leaves the 256px poster on screen, upscaled to the whole
+ // stage. The nudge makes the decoder paint frame 0 at the video's native resolution.
+ expect(position.get()).toBeGreaterThan(0);
+ expect(position.get()).toBeLessThan(0.001);
+ // The poster is still the placeholder for the moment before that frame lands, and the
+ // still behind the failure state — the seek supersedes it, it is not removed.
+ expect(video.getAttribute('poster')).toBe(videoSource.poster);
+ });
+
+ it('leaves the playhead alone when the user started playback before metadata arrived', async () => {
+ await renderVideo();
+ const video = getVideo();
+ // The one interleaving where `loadedmetadata` observes a non-paused element: `load()`
+ // resets position and pause state before it fires, so a viewer mid-playback is already
+ // back at zero by then and there is no playhead left for the guard to protect.
+ const position = stubPlaybackPosition(video, { paused: false });
+
+ await interact(() => video.dispatchEvent(new Event('loadedmetadata')));
+
+ expect(position.get()).toBe(0);
+ });
+
it('does not arm a drag or cancel wheel events from the video surface and native controls', async () => {
await interact(() => {
root?.render(
@@ -647,6 +676,26 @@ const installClipboardSupport = () => {
return { clipboardItems, write };
};
+const stubPlaybackPosition = (video: HTMLVideoElement, state: { paused?: boolean } = {}): { get: () => number } => {
+ // A `data:` audio source has no seekable range, so a real assignment to `currentTime`
+ // clamps straight back to 0. Standing in for the accessor is what makes the nudge
+ // observable at all.
+ let currentTime = 0;
+
+ Object.defineProperties(video, {
+ currentTime: {
+ configurable: true,
+ get: () => currentTime,
+ set: (value: number) => {
+ currentTime = value;
+ },
+ },
+ paused: { configurable: true, value: state.paused ?? true },
+ });
+
+ return { get: () => currentTime };
+};
+
const setVideoFrameState = (
video: HTMLVideoElement,
state: { height: number; readyState: number; width: number }