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.

harden representative-frame extraction per adversarial review

Lincoln Stein761e712327f7
diff --git a/invokeai/app/api/routers/videos.py b/invokeai/app/api/routers/videos.py
index dccbad0133..65f5c03b23 100644
--- a/invokeai/app/api/routers/videos.py
+++ b/invokeai/app/api/routers/videos.py
@@ -182,14 +182,15 @@ def _is_mp4_file(path: Path) -> bool:
def _probe_decodable_video(path: Path) -> tuple[tuple[int, int, float, Optional[float]], Optional[PILImage.Image]]:
- """Probes metadata and proves the video has a decodable first frame.
+ """Probes metadata and proves the video has a decodable frame.
Returns the metadata plus the decoded frame so the save path can reuse it as the
thumbnail source instead of spawning another decode worker. The frame is taken ~1s into
the clip (see representative_thumbnail_frame_index) rather than at index 0 — first
frames are routinely unrepresentative (fade-ins, the synthesized waveform track's empty
- first window) — with a frame-0 fallback inside the helper, so acceptance semantics are
- unchanged. A decode timeout is contention on a loaded server, not evidence the video is
+ first window) — with a frame-0 fallback inside the helper. Acceptance is thereby
+ slightly WIDER than before: a file whose frame 0 is corrupt but whose ~1s frame decodes
+ is now accepted rather than 415'd. A decode timeout is contention on a loaded server, not evidence the video is
bad — probe_video already succeeded — so it yields (metadata, None) and the upload
proceeds, with save-time thumbnail extraction as the backstop.
"""
diff --git a/invokeai/app/util/video_thumbnails.py b/invokeai/app/util/video_thumbnails.py
index a546c0c782..b7e9e8fcb7 100644
--- a/invokeai/app/util/video_thumbnails.py
+++ b/invokeai/app/util/video_thumbnails.py
@@ -436,17 +436,26 @@ def extract_representative_video_frame(
) -> Optional[Image.Image]:
"""Extracts the representative thumbnail frame, falling back to frame 0.
- The fallback covers containers whose duration metadata overstates the decodable range
- (the computed index then has no frame): such files still get a thumbnail rather than a
- gallery placeholder. A timeout is NOT retried at frame 0 — it is contention or an
- adversarial file, not evidence about the index, and a second decode would double the
- time an adversarial upload can hold a request worker; with ``raise_on_timeout`` it
- propagates as VideoDecodeTimeoutError, otherwise it returns None as today.
+ The fallback covers containers whose metadata overstates the decodable range (the
+ computed index then has no frame): such files still get a thumbnail rather than a
+ gallery placeholder. In the worst case — a slow decode *failure* at the representative
+ index — the fallback costs a second decode budget, bounding one call at two timeouts.
+
+ A timeout is never retried, in either mode: it is contention or an adversarial file,
+ not evidence about the index, and a retry would hold a request worker for another full
+ budget. Timeouts are detected by always raising internally; with ``raise_on_timeout``
+ the VideoDecodeTimeoutError propagates, otherwise the call returns None as
+ ``extract_video_frame`` would.
"""
frame_index = representative_thumbnail_frame_index(duration, fps)
- frame = extract_video_frame(video_path, frame_index=frame_index, timeout=timeout, raise_on_timeout=raise_on_timeout)
- if frame is None and frame_index > 0:
- frame = extract_video_frame(video_path, frame_index=0, timeout=timeout, raise_on_timeout=raise_on_timeout)
+ try:
+ frame = extract_video_frame(video_path, frame_index=frame_index, timeout=timeout, raise_on_timeout=True)
+ if frame is None and frame_index > 0:
+ frame = extract_video_frame(video_path, frame_index=0, timeout=timeout, raise_on_timeout=True)
+ except VideoDecodeTimeoutError:
+ if raise_on_timeout:
+ raise
+ return None
return frame
diff --git a/tests/app/util/test_video_thumbnails.py b/tests/app/util/test_video_thumbnails.py
index e78658157c..fc2f0dc92a 100644
--- a/tests/app/util/test_video_thumbnails.py
+++ b/tests/app/util/test_video_thumbnails.py
@@ -781,3 +781,39 @@ class TestRepresentativeThumbnailFrame:
tmp_path / "v.mp4", duration=10.0, fps=24.0, raise_on_timeout=True
)
assert calls == [24]
+
+ def test_timeout_in_non_raise_mode_returns_none_without_retry(
+ self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+ ) -> None:
+ """The no-retry-on-timeout rule must hold in non-raise mode too (the disk store's
+ mode): a timeout there must not trigger a second full-budget frame-0 decode."""
+ calls: list[int] = []
+
+ def fake_extract(path, frame_index=0, timeout=0.0, raise_on_timeout=False):
+ calls.append(frame_index)
+ raise video_thumbnails.VideoDecodeTimeoutError("busy")
+
+ monkeypatch.setattr(video_thumbnails, "extract_video_frame", fake_extract)
+ result = video_thumbnails.extract_representative_video_frame(
+ tmp_path / "v.mp4", duration=10.0, fps=24.0, raise_on_timeout=False
+ )
+ assert result is None
+ assert calls == [24]
+
+ def test_timeout_during_fallback_is_not_swallowed_in_raise_mode(
+ self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+ ) -> None:
+ calls: list[int] = []
+
+ def fake_extract(path, frame_index=0, timeout=0.0, raise_on_timeout=False):
+ calls.append(frame_index)
+ if frame_index > 0:
+ return None
+ raise video_thumbnails.VideoDecodeTimeoutError("busy")
+
+ monkeypatch.setattr(video_thumbnails, "extract_video_frame", fake_extract)
+ with pytest.raises(video_thumbnails.VideoDecodeTimeoutError):
+ video_thumbnails.extract_representative_video_frame(
+ tmp_path / "v.mp4", duration=10.0, fps=24.0, raise_on_timeout=True
+ )
+ assert calls == [24, 0]