Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio video: drain palettegen per frame in the two-pass GIF export (#14958)

Daniel Han9e8873460291
diff --git a/studio/backend/core/inference/video_gallery.py b/studio/backend/core/inference/video_gallery.py
index f0b5cb0ff33..7e307d01a0a 100644
--- a/studio/backend/core/inference/video_gallery.py
+++ b/studio/backend/core/inference/video_gallery.py
@@ -526,6 +526,8 @@ def _transcode_gif_palette(
frame.pts = count
frame.time_base = time_base
buffer.push(frame)
+ # palettegen emits only at EOF, but pulling lets FFmpeg consume each frame instead of queueing the whole clip
+ _drain_sink(sink)
if held is not None:
held.append(frame)
count += 1
diff --git a/studio/backend/tests/test_video_gallery.py b/studio/backend/tests/test_video_gallery.py
index f7562b51e38..278719db084 100644
--- a/studio/backend/tests/test_video_gallery.py
+++ b/studio/backend/tests/test_video_gallery.py
@@ -682,6 +682,63 @@ def test_gif_export_uses_one_clip_wide_palette(monkeypatch, hold):
assert sum(info["delays"]) in (99, 100, 101)
+_GIF_PEAK_RSS_SCRIPT = r"""
+from fractions import Fraction
+import av, numpy as np
+import core.inference.video_gallery as vg
+
+vg._GIF_HOLD_BYTES = 0
+side, n = 720, 300
+rgb = np.zeros((side, side, 3), np.uint8)
+rgb[..., 0] = np.arange(side, dtype = np.uint8)[None, :]
+
+
+def _frames(path, step, width, height):
+ for _ in range(n):
+ yield av.VideoFrame.from_ndarray(rgb, format = "rgb24")
+
+
+def _peak_kib():
+ # VmHWM is per address space; ru_maxrss would carry over the forking parent's peak
+ with open("/proc/self/status") as f:
+ return int(next(line for line in f if line.startswith("VmHWM:")).split()[1])
+
+
+vg._gif_scaled_frames = _frames
+before = _peak_kib()
+gif = vg._transcode_gif_palette(None, Fraction(12), 1, side, side, n)
+assert gif.startswith(b"GIF8")
+print((_peak_kib() - before) // 1024)
+"""
+
+
+def test_gif_export_two_pass_does_not_queue_the_clip_in_palettegen():
+ """With frames over _GIF_HOLD_BYTES the palette pass must not hold them either: 300 720p frames are 445 MiB if
+ FFmpeg queues them all before palettegen sees EOF."""
+ import subprocess
+ import sys
+
+ from core.inference import video_gallery as vg
+
+ pytest.importorskip("numpy")
+ if not os.path.exists("/proc/self/status"):
+ pytest.skip("peak RSS is read from /proc/self/status (Linux only)")
+ if not vg._gif_palette_supported():
+ pytest.skip("this PyAV build has no palettegen / paletteuse / gif encoder")
+ backend = Path(vg.__file__).resolve().parents[2]
+ result = subprocess.run(
+ [sys.executable, "-c", _GIF_PEAK_RSS_SCRIPT],
+ cwd = backend,
+ env = {**os.environ, "PYTHONPATH": str(backend)},
+ capture_output = True,
+ text = True,
+ timeout = 300,
+ )
+ assert result.returncode == 0, result.stderr[-2000:]
+ growth_mib = int(result.stdout.split()[-1])
+ assert growth_mib < 160, f"peak RSS grew {growth_mib} MiB"
+
+
def test_gif_export_palette_path_caps_frames_and_edge(monkeypatch):
from core.inference import video_gallery as vg