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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: stop a short chat video attachment from taking minutes to prefill (#10973)
oobabooga4f56de1c2368
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.pyindex f6ca1f217fc..badc49b49d2 100644--- a/studio/backend/core/inference/llama_cpp.py+++ b/studio/backend/core/inference/llama_cpp.py@@ -4991,6 +4991,69 @@ def _extra_args_mmproj_offload_value(extra_args: Optional[Sequence[str]]) -> Opt_MMPROJ_OFFLOAD_ENV_VAR = "LLAMA_ARG_MMPROJ_OFFLOAD"_MMPROJ_OFFLOAD_NEG_ENV_VAR = "LLAMA_ARG_NO_MMPROJ_OFFLOAD"+# Gemma 4 expects video at up to 1 fps; llama-server defaults to 4.+_LLAMA_VIDEO_FPS = "1"+_VIDEO_FPS_ENV_VAR = "LLAMA_ARG_VIDEO_FPS"+++def _video_fps_flags(+ server_caps: Mapping[str, object], env: Optional[Mapping[str, str]] = None+) -> list[str]:+ """Return the default FPS flag unless unsupported or set in the environment.++ User extras are appended later and retain their last-wins override.+ """+ if not server_caps.get("supports_video_fps"):+ return []+ source = os.environ if env is None else env+ if source.get(_VIDEO_FPS_ENV_VAR) is not None:+ return []+ return ["--video-fps", _LLAMA_VIDEO_FPS]+++def requested_video_fps(+ extra_args: Optional[Sequence[str]] = None, env: Optional[Mapping[str, str]] = None+) -> Optional[float]:+ """The frame rate the USER asked llama-server for, if they asked at all.++ Preprocessing has to know this: it transcodes before llama-server samples,+ and the server can only duplicate frames the transcode already dropped. So+ an explicit 8 fps has to reach the encoder, or the override silently buys+ nothing but duplicates.++ ARGV FIRST, then the environment. llama.cpp reads the env var while+ registering the option and argv overrides it afterwards, so an inherited+ LLAMA_ARG_VIDEO_FPS=1 alongside an advanced `--video-fps 8` samples at 8.+ Verified against b10976 on a 5s clip: env=1 alone costs 2796 prompt tokens,+ env=8 alone 19392, and env=1 with argv=8 also 19392.++ Within argv the LAST occurrence wins, which is how llama.cpp resolves a+ repeated flag ("only last value will be used"). Matching goes through+ `_flag_name`, so the underscore spelling llama.cpp also accepts+ (`--video_fps`, `--video_fps=8`) is caught rather than silently missed.++ None when the user said nothing, or said something that is not a rate.+ """+ args = [str(a) for a in (extra_args or ())]+ raw = None+ for i in range(len(args) - 1, -1, -1):+ if _flag_name(args[i]) != "--video-fps":+ continue+ raw = (+ args[i].split("=", 1)[1]+ if "=" in args[i]+ else (args[i + 1] if i + 1 < len(args) else None)+ )+ break+ if raw is None:+ source = os.environ if env is None else env+ raw = source.get(_VIDEO_FPS_ENV_VAR)+ try:+ rate = float(raw)+ except (TypeError, ValueError):+ return None+ return rate if rate > 0 else None+def _env_sets_mmproj_offload(env: Optional[Mapping[str, str]] = None) -> bool:"""True when the child's environment names the projector placement at all.@@ -8326,6 +8389,7 @@ class LlamaCppBackend:"supports_metrics": False,"supports_slot_save": False,"supports_no_mmproj_offload": False,+ "supports_video_fps": False,"supports_load_mode": False,"spec_draft_ngl_flag": None,"spec_draft_cache_k_flag": None,@@ -8373,6 +8437,7 @@ class LlamaCppBackend:supports_metrics = Falsesupports_slot_save = Falsesupports_no_mmproj_offload = False+ supports_video_fps = Falsesupports_load_mode = Falsespec_draft_ngl_flag = Nonespec_draft_cache_k_flag = None@@ -8598,6 +8663,7 @@ class LlamaCppBackend:supports_metrics = _is_real("--metrics")supports_slot_save = _is_real("--slot-save-path")supports_no_mmproj_offload = _is_real("--no-mmproj-offload")+ supports_video_fps = _is_real("--video-fps")# --load-mode supersedes --mlock / --no-mmap, which are deprecated.# Pre-initialised above: a failed probe must fall back, not raise.supports_load_mode = _is_real("--load-mode")@@ -8690,6 +8756,7 @@ class LlamaCppBackend:"supports_metrics": supports_metrics,"supports_slot_save": supports_slot_save,"supports_no_mmproj_offload": supports_no_mmproj_offload,+ "supports_video_fps": supports_video_fps,"supports_load_mode": supports_load_mode,"spec_draft_ngl_flag": spec_draft_ngl_flag,"spec_draft_cache_k_flag": spec_draft_cache_k_flag,@@ -24497,6 +24564,9 @@ class LlamaCppBackend:"llama-server has no --cache-ram; skipping the requested %s MiB.",cache_ram,)+ # On every launch, not only where Studio emits --mmproj: a projector+ # from the extras or LLAMA_ARG_MMPROJ takes video too.+ cmd.extend(_video_fps_flags(server_caps))# Report a clean public model id (matching GET /v1/models) rather# than the raw -m path in llama-server's own /v1/models and thediff --git a/studio/backend/core/inference/llama_video_input.py b/studio/backend/core/inference/llama_video_input.pynew file mode 100644index 00000000000..b020a273a9e--- /dev/null+++ b/studio/backend/core/inference/llama_video_input.py@@ -0,0 +1,276 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Rework GGUF chat videos before llama-server samples their frames."""++from __future__ import annotations++import base64+import binascii+import json+import logging+import shutil+import subprocess+import tempfile+from pathlib import Path+from typing import Optional++from utils.native_path_leases import child_env_without_native_path_secret+from utils.subprocess_compat import windows_hidden_subprocess_kwargs++logger = logging.getLogger(__name__)++# An area, not an edge, so portrait and landscape clips get the same budget.+MAX_FRAME_PIXELS = 640 * 360+# llama-server's --video-fps default: encoding above it buys frames mtmd drops,+# and those bytes count against max_bytes.+MAX_FRAME_RATE = 4+# `fps` places its first output frame half an interval in, so at --video-fps 1 a+# clip under ~0.5s decodes to NO frames. A whole second leaves user-rate headroom.+MIN_SAMPLED_SECONDS = 1.0+_PROBE_TIMEOUT_S = 30+_SHRINK_TIMEOUT_S = 300+++def _run(argv: list[str], timeout: float) -> subprocess.CompletedProcess:+ return subprocess.run(+ argv,+ stdin = subprocess.DEVNULL,+ capture_output = True,+ timeout = timeout,+ env = child_env_without_native_path_secret(),+ **windows_hidden_subprocess_kwargs(),+ )+++def _frame_geometry(+ ffprobe: str, clip: Path+) -> tuple[Optional[int], Optional[float], Optional[float]]:+ """Any field is None when ffprobe cannot answer; the caller then leaves the clip alone."""+ result = _run(+ [+ ffprobe,+ "-v",+ "error",+ "-select_streams",+ "v:0",+ "-show_entries",+ "stream=width,height,avg_frame_rate",+ "-show_entries",+ "format=duration",+ "-of",+ "json",+ str(clip),+ ],+ _PROBE_TIMEOUT_S,+ )+ if result.returncode != 0:+ return None, None, None+ # A build answering `-of json` with a non-object must fall back, not raise+ # AttributeError past the caller's except clause.+ payload = json.loads(result.stdout or b"{}")+ if not isinstance(payload, dict):+ return None, None, None+ streams = payload.get("streams") or []+ if not streams or not isinstance(streams[0], dict):+ return None, None, None+ width = int(streams[0].get("width") or 0)+ height = int(streams[0].get("height") or 0)+ area = width * height if width > 0 and height > 0 else None+ container = payload.get("format")+ duration = None+ if isinstance(container, dict):+ try:+ duration = float(container.get("duration"))+ except (TypeError, ValueError):+ duration = None+ if duration is None:+ duration = _packet_duration(ffprobe, clip)+ return area, _frame_rate(streams[0].get("avg_frame_rate")), duration+++def _packet_duration(ffprobe: str, clip: Path) -> Optional[float]:+ """Duration from packet timestamps, for containers that carry none: a raw+ elementary stream has no `format.duration`, so the sub-second floor never fired+ on one. Bounded to twice the floor, past which a clip is long enough anyway."""+ result = _run(+ [+ ffprobe,+ "-v",+ "error",+ "-select_streams",+ "v:0",+ "-read_intervals",+ f"%+{MIN_SAMPLED_SECONDS * 2:g}",+ "-show_entries",+ "packet=pts_time,duration_time",+ "-of",+ "json",+ str(clip),+ ],+ _PROBE_TIMEOUT_S,+ )+ if result.returncode != 0:+ return None+ payload = json.loads(result.stdout or b"{}")+ if not isinstance(payload, dict):+ return None+ end = 0.0+ total = 0.0+ for packet in payload.get("packets") or []:+ if not isinstance(packet, dict):+ continue+ try:+ span = float(packet.get("duration_time"))+ except (TypeError, ValueError):+ span = 0.0+ total += span+ try:+ end = max(end, float(packet.get("pts_time")) + span)+ except (TypeError, ValueError):+ # Annex B carries no timestamps, only durations: summing is the only+ # reading available for a raw stream.+ continue+ return (end or total) or None+++def _frame_rate(value: object) -> Optional[float]:+ """ffprobe's `num/den` rate as a float; None when it is absent or 0/0."""+ if not isinstance(value, str) or "/" not in value:+ return None+ numerator, _, denominator = value.partition("/")+ try:+ den = float(denominator)+ return float(numerator) / den if den else None+ except ValueError:+ return None+++def _scale_filter(max_pixels: int) -> str:+ # ffmpeg applies display rotation before filters, so use filter-time dimensions.+ factor = f"sqrt({max_pixels}/(iw*ih))"+ return f"scale=w='max(2,trunc(iw*{factor}/2)*2)':h='max(2,trunc(ih*{factor}/2)*2)':flags=area"+++def _rate_ceiling(sampled_fps: Optional[float]) -> float:+ """The highest rate worth encoding. A user asking for more than MAX_FRAME_RATE+ raises it: the server samples AFTER this transcode and can only duplicate what+ was dropped here."""+ if sampled_fps is not None and sampled_fps > MAX_FRAME_RATE:+ return float(sampled_fps)+ return float(MAX_FRAME_RATE)+++def _filter_chain(+ max_pixels: int,+ source_rate: Optional[float],+ duration: Optional[float],+ oversized: bool,+ sampled_fps: Optional[float] = None,+) -> str:+ """Tail pad, then rate cap, then scale, each only when needed. Order matters:+ the cap shares the blind spot the pad covers (`fps=4` on a 0.1s clip also emits+ nothing, leaving tpad no frame to clone), the cap only goes downwards since+ `fps` DUPLICATES when asked for more than the source has, and scale is skipped+ inside the budget where it would UPSCALE."""+ ceiling = _rate_ceiling(sampled_fps)+ chain = []+ if duration is not None and 0 < duration < MIN_SAMPLED_SECONDS:+ chain.append(f"tpad=stop_mode=clone:stop_duration={MIN_SAMPLED_SECONDS - duration:.3f}")+ if source_rate is not None and source_rate > ceiling:+ chain.append(f"fps={ceiling:g}")+ if oversized:+ chain.append(_scale_filter(max_pixels))+ return ",".join(chain)+++def shrink_video_for_llama(+ video_b64: str,+ max_bytes: int,+ max_pixels: int = MAX_FRAME_PIXELS,+ sampled_fps: Optional[float] = None,+) -> str:+ """Make a clip cheap and safe for llama-server to sample, as bare base64.++ ``sampled_fps`` raises the rate ceiling. Keeps only video and the timing of every+ real frame. Missing tools, failures and an over-``max_bytes`` result pass through.+ """+ ffmpeg = shutil.which("ffmpeg")+ ffprobe = shutil.which("ffprobe")+ if not ffmpeg or not ffprobe:+ return video_b64+ try:+ raw = base64.b64decode(video_b64)+ except (binascii.Error, ValueError):+ return video_b64+ try:+ with tempfile.TemporaryDirectory(prefix = "unsloth-video-") as tmp:+ source = Path(tmp) / "clip"+ source.write_bytes(raw)+ area, rate, duration = _frame_geometry(ffprobe, source)+ if area is None:+ return video_b64+ oversized = area > max_pixels+ too_short = duration is not None and 0 < duration < MIN_SAMPLED_SECONDS+ if not oversized and not too_short:+ return video_b64+ # Keeping the decoded copy alive just to log its length costs 64 MiB.+ raw_bytes = len(raw)+ del raw+ shrunk = Path(tmp) / "shrunk.mkv"+ result = _run(+ [+ ffmpeg,+ "-nostdin",+ "-hide_banner",+ "-loglevel",+ "error",+ "-i",+ str(source),+ "-map",+ "0:v:0",+ "-an",+ "-sn",+ "-dn",+ "-vf",+ _filter_chain(max_pixels, rate, duration, oversized, sampled_fps),+ "-c:v",+ "mpeg4",+ # Widely available, but outgrows a low-bitrate source: -fs+ # bounds at the upload cap, not near the original.+ "-q:v",+ "5",+ "-fs",+ str(max_bytes),+ "-f",+ "matroska",+ str(shrunk),+ ],+ _SHRINK_TIMEOUT_S,+ )+ if result.returncode != 0 or not shrunk.is_file() or shrunk.stat().st_size == 0:+ logger.warning(+ "Could not shrink the video clip, forwarding it unchanged: %s",+ (result.stderr or b"").decode("utf-8", "replace").strip()[-500:],+ )+ return video_b64+ if shrunk.stat().st_size >= max_bytes:+ logger.warning(+ "The shrunk video clip reached the %d byte cap, forwarding it unchanged",+ max_bytes,+ )+ return video_b64+ logger.info(+ "Reworked video clip for sampling: %d -> at most %d pixels, "+ "%.2fs -> at least %.2fs (%d to %d bytes)",+ area,+ max_pixels if oversized else area,+ duration if duration is not None else -1.0,+ MIN_SAMPLED_SECONDS if too_short else (duration or -1.0),+ raw_bytes,+ shrunk.stat().st_size,+ )+ return base64.b64encode(shrunk.read_bytes()).decode("ascii")+ except (OSError, subprocess.SubprocessError, ValueError) as exc:+ logger.warning("Could not shrink the video clip, forwarding it unchanged: %s", exc)+ return video_b64diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex cdf4ce8e4d4..b5858f01c29 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -127,6 +127,8 @@ from core.inference.llama_admission import (peek_llama_admission_snapshot,)from core.inference.tool_stream_exec import TOOL_APPROVAL_FLUSH_DELAY_S+from core.inference.llama_cpp import requested_video_fps+from core.inference.llama_video_input import shrink_video_for_llamadef _positive_int_or_none(value: Any) -> Optional[int]:@@ -19919,7 +19921,8 @@ _MAX_AUDIO_RAW_BYTES = STT_AUDIO_RAW_MAX_BYTES_MAX_AUDIO_B64_CHARS = STT_AUDIO_B64_MAX_CHARS# The composer's 64 MB cap as padded base64: 4 chars per 3 bytes, rounded up.# Flooring instead refused a file of exactly the size the composer allows.-_MAX_VIDEO_B64_CHARS = 4 * math.ceil((64 * 1024 * 1024) / 3)+_MAX_VIDEO_BYTES = 64 * 1024 * 1024+_MAX_VIDEO_B64_CHARS = 4 * math.ceil(_MAX_VIDEO_BYTES / 3)_MAX_AUDIO_SECONDS = 30 * 60# The duration cap alone is rate-relative, so a high-rate container retains far# more memory for the same 30 minutes: at 48 kHz that is 86M float32 samples,@@ -20610,7 +20613,7 @@ def _inject_video_part(messages: list[dict], video_b64: str) -> None:"""Append an input_video part to the last user message, in place.llama-server samples the clip into frames itself (ffmpeg via mtmd), so the- container is forwarded untouched. Rides the message list like image_url and+ part carries a container, not frames. Rides the message list like image_url andinput_audio, so it flows through the plain and tool-calling paths alike.Ref: llama.cpp tools/server/server-common.cpp, `type == "input_video"`."""@@ -23604,8 +23607,7 @@ async def produce_openai_chat_completions(logger.warning("Audio decode failed: %s", e, exc_info = True)raise _reject(400, "Could not decode the provided audio file.")- # Forwarded whole: llama-server owns the frame sampling, and takes the- # clip only when /props reports modalities.video.+ # llama-server samples frames but encodes each at the clip's resolution.video_b64 = Noneif payload.video_base64:if not getattr(llama_backend, "_has_video_input", False):@@ -23617,6 +23619,21 @@ async def produce_openai_chat_completions(video_b64, video_rejection = _video_b64_rejection(payload.video_base64)if video_rejection is not None:raise _reject(*video_rejection)+ try:+ # The transcode runs BEFORE llama-server samples and the server can+ # only duplicate what was dropped, so an explicit rate must reach it.+ video_b64 = await asyncio.to_thread(+ shrink_video_for_llama,+ video_b64,+ _MAX_VIDEO_BYTES,+ sampled_fps = requested_video_fps(getattr(llama_backend, "extra_args", None)),+ )+ except Exception as e:+ # The helper absorbs its own ffmpeg failures, so reaching here means+ # something it does not model went wrong. Reject like the audio path+ # above, rather than letting a raw 500 leak the monitor entry with it.+ logger.warning("Video preprocessing failed: %s", e, exc_info = True)+ raise _reject(400, "Could not process the provided video file.")gguf_messages, _ = await _openai_messages_for_gguf_chat_async(payload,diff --git a/studio/backend/tests/test_llama_video_frame_budget.py b/studio/backend/tests/test_llama_video_frame_budget.pynew file mode 100644index 00000000000..f8929a767ae--- /dev/null+++ b/studio/backend/tests/test_llama_video_frame_budget.py@@ -0,0 +1,590 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Tests for Studio's llama-server video frame budget."""++from __future__ import annotations++import base64+import json+import shutil+import subprocess+import sys+from pathlib import Path++import pytest++_TESTS_DIR = Path(__file__).resolve().parent+if str(_TESTS_DIR) not in sys.path:+ sys.path.insert(0, str(_TESTS_DIR))++from core.inference import llama_video_input # noqa: E402+from core.inference.llama_cpp import _LLAMA_VIDEO_FPS, _video_fps_flags # noqa: E402+from core.inference.llama_video_input import ( # noqa: E402+ MAX_FRAME_PIXELS,+ MAX_FRAME_RATE,+ MIN_SAMPLED_SECONDS,+ shrink_video_for_llama,+)+from test_llama_flag_catalog import _probe_with_help # noqa: E402+from test_mmproj_placement_policy import _backend, _launch, _write_gguf # noqa: E402++_HAS_FFMPEG = shutil.which("ffmpeg") is not None and shutil.which("ffprobe") is not None+needs_ffmpeg = pytest.mark.skipif(not _HAS_FFMPEG, reason = "ffmpeg and ffprobe are not on PATH")+_CAP = 64 * 1024 * 1024+++def _video_backend(tmp_path, **caps):+ backend, gguf = _backend(tmp_path, memory = [(0, 12_000, 24_000)])+ probe = backend.probe_server_capabilities++ def with_caps(_binary = None):+ return {**probe(), **caps}++ backend.probe_server_capabilities = with_caps+ return backend, gguf+++def test_a_vision_load_samples_video_at_one_frame_per_second(tmp_path, monkeypatch):+ monkeypatch.delenv("LLAMA_ARG_VIDEO_FPS", raising = False)+ backend, gguf = _video_backend(tmp_path, supports_video_fps = True)++ cmd = _launch(backend, gguf)["cmd"]++ assert cmd.count("--video-fps") == 1+ assert cmd[cmd.index("--video-fps") + 1] == _LLAMA_VIDEO_FPS == "1"+++def test_a_hand_typed_frame_rate_still_wins(tmp_path, monkeypatch):+ monkeypatch.delenv("LLAMA_ARG_VIDEO_FPS", raising = False)+ backend, gguf = _video_backend(tmp_path, supports_video_fps = True)++ cmd = _launch(backend, gguf, extra_args = ["--video-fps", "4"])["cmd"]++ positions = [i for i, token in enumerate(cmd) if token == "--video-fps"]+ assert cmd[positions[-1] + 1] == "4"+++def test_an_inherited_frame_rate_is_not_overridden(tmp_path, monkeypatch):+ monkeypatch.setenv("LLAMA_ARG_VIDEO_FPS", "2")+ backend, gguf = _video_backend(tmp_path, supports_video_fps = True)++ assert "--video-fps" not in _launch(backend, gguf)["cmd"]+++def test_a_build_without_the_flag_is_not_sent_it(tmp_path, monkeypatch):+ monkeypatch.delenv("LLAMA_ARG_VIDEO_FPS", raising = False)+ backend, gguf = _video_backend(tmp_path)++ assert "--video-fps" not in _launch(backend, gguf)["cmd"]+++def _without_studio_projector(tmp_path):+ backend, gguf = _video_backend(tmp_path, supports_video_fps = True)+ backend._resolve_launch_mmproj_path = lambda **_kw: None+ return backend, gguf+++def test_a_projector_from_mmproj_auto_is_sampled_at_the_default_rate(tmp_path, monkeypatch):+ """--mmproj itself is denied in the advanced arguments; --mmproj-auto is the+ allowed way to have llama-server find a projector on its own."""+ monkeypatch.delenv("LLAMA_ARG_VIDEO_FPS", raising = False)+ backend, gguf = _without_studio_projector(tmp_path)++ cmd = _launch(backend, gguf, extra_args = ["--mmproj-auto"])["cmd"]++ assert "--mmproj" not in cmd+ assert cmd.index("--video-fps") < cmd.index("--mmproj-auto")+ assert cmd[cmd.index("--video-fps") + 1] == "1"+++def test_a_projector_from_the_environment_is_sampled_at_the_default_rate(tmp_path, monkeypatch):+ monkeypatch.delenv("LLAMA_ARG_VIDEO_FPS", raising = False)+ backend, gguf = _without_studio_projector(tmp_path)+ monkeypatch.setenv("LLAMA_ARG_MMPROJ", str(_write_gguf(tmp_path / "env-mmproj.gguf")))++ result = _launch(backend, gguf)++ assert "--mmproj" not in result["cmd"]+ assert result["env"].get("LLAMA_ARG_MMPROJ")+ assert result["cmd"][result["cmd"].index("--video-fps") + 1] == "1"+++def test_the_flag_helper_reads_the_environment_it_is_given():+ caps = {"supports_video_fps": True}+ assert _video_fps_flags(caps, env = {}) == ["--video-fps", "1"]+ assert _video_fps_flags(caps, env = {"LLAMA_ARG_VIDEO_FPS": ""}) == []+ assert _video_fps_flags({}, env = {}) == []+++def test_the_probe_reads_the_flag_from_help(monkeypatch, tmp_path):+ with_flag = _probe_with_help(+ monkeypatch,+ tmp_path,+ "--video-fps N target video frame rate (default: 4.0)\n",+ )+ assert with_flag["supports_video_fps"] is True++ older = tmp_path / "older"+ older.mkdir()+ without = _probe_with_help(+ monkeypatch, older, "--top-k N top-k sampling (default: 40)\n"+ )+ assert without["supports_video_fps"] is False+++def _clip(+ tmp_path: Path,+ name: str,+ size: str,+ rotation: int = 0,+ rate: int = 10,+ seconds: int = 2,+) -> str:+ path = tmp_path / name+ ffmpeg = ["ffmpeg", "-nostdin", "-loglevel", "error", "-y"]+ subprocess.run(+ [+ *ffmpeg,+ *("-f", "lavfi", "-i", f"testsrc2=size={size}:rate={rate}"),+ *("-t", str(seconds)),+ *("-pix_fmt", "yuv420p", str(path)),+ ],+ check = True,+ )+ if rotation:+ # Match phone orientation metadata without re-encoding.+ rotated = tmp_path / f"rotated-{name}"+ subprocess.run(+ [+ *ffmpeg,+ "-display_rotation",+ str(rotation),+ "-i",+ str(path),+ "-c",+ "copy",+ str(rotated),+ ],+ check = True,+ )+ path = rotated+ return base64.b64encode(path.read_bytes()).decode("ascii")+++def _probe(tmp_path: Path, clip_b64: str) -> dict:+ path = tmp_path / "probe-input"+ path.write_bytes(base64.b64decode(clip_b64))+ result = subprocess.run(+ [+ "ffprobe",+ "-v",+ "error",+ "-select_streams",+ "v:0",+ "-show_entries",+ "stream=width,height:stream_side_data=rotation:format=duration",+ "-of",+ "json",+ str(path),+ ],+ check = True,+ capture_output = True,+ )+ data = json.loads(result.stdout)+ stream = data["streams"][0]+ rotation = [s.get("rotation") for s in stream.get("side_data_list") or [] if "rotation" in s]+ return {+ "width": stream["width"],+ "height": stream["height"],+ "rotation": rotation[0] if rotation else 0,+ "duration": float(data["format"]["duration"]),+ }+++@needs_ffmpeg+def test_a_720p_clip_is_shrunk_to_the_frame_budget(tmp_path):+ clip = _clip(tmp_path, "wide.mp4", "1280x720")++ shrunk = shrink_video_for_llama(clip, _CAP)++ info = _probe(tmp_path, shrunk)+ assert info["width"] * info["height"] <= MAX_FRAME_PIXELS+ assert (info["width"], info["height"]) == (640, 360)+ # Sampling stays llama-server's responsibility.+ assert info["duration"] == pytest.approx(2.0, abs = 0.2)+++@needs_ffmpeg+def test_a_rotated_phone_clip_comes_out_upright(tmp_path):+ clip = _clip(tmp_path, "portrait.mp4", "1280x720", rotation = 90)+ assert _probe(tmp_path, clip)["rotation"] != 0++ info = _probe(tmp_path, shrink_video_for_llama(clip, _CAP))++ assert (info["width"], info["height"]) == (360, 640)+ assert info["rotation"] == 0+++@needs_ffmpeg+def test_a_clip_already_within_the_budget_is_forwarded_untouched(tmp_path):+ clip = _clip(tmp_path, "small.mp4", "640x360")++ assert shrink_video_for_llama(clip, _CAP) is clip+++@needs_ffmpeg+def test_a_shrunk_clip_that_outgrows_the_cap_is_forwarded_untouched(tmp_path):+ clip = _clip(tmp_path, "wide.mp4", "1280x720")++ assert shrink_video_for_llama(clip, 20_000) is clip+++def _frame_count(tmp_path: Path, clip_b64: str, name: str) -> int:+ path = tmp_path / name+ path.write_bytes(base64.b64decode(clip_b64))+ result = subprocess.run(+ [+ "ffprobe",+ "-v",+ "error",+ "-select_streams",+ "v:0",+ "-count_frames",+ "-show_entries",+ "stream=nb_read_frames",+ "-of",+ "json",+ str(path),+ ],+ check = True,+ capture_output = True,+ )+ return int(json.loads(result.stdout)["streams"][0]["nb_read_frames"])+++@needs_ffmpeg+def test_frames_beyond_the_sampled_rate_are_not_re_encoded(tmp_path):+ # Re-encoding at the source rate spends the byte budget on frames mtmd drops,+ # which pushed a long clip over the cap and threw the conversion away.+ clip = _clip(tmp_path, "fast.mp4", "1280x720", rate = 30, seconds = 2)+ assert _frame_count(tmp_path, clip, "fast-source") == 60++ shrunk = shrink_video_for_llama(clip, _CAP)++ assert _frame_count(tmp_path, shrunk, "fast-shrunk") == MAX_FRAME_RATE * 2+ # Same span, so llama-server samples the same moments it would have before.+ assert _probe(tmp_path, shrunk)["duration"] == pytest.approx(2.0, abs = 0.3)+++@needs_ffmpeg+def test_a_clip_slower_than_the_cap_keeps_every_frame(tmp_path):+ # `fps` DUPLICATES when asked for more than the source has, so a timelapse+ # must not be padded up to the ceiling.+ clip = _clip(tmp_path, "slow.mp4", "1280x720", rate = 2, seconds = 3)+ assert _frame_count(tmp_path, clip, "slow-source") == 6++ shrunk = shrink_video_for_llama(clip, _CAP)++ assert _frame_count(tmp_path, shrunk, "slow-shrunk") == 6+++@needs_ffmpeg+def test_a_long_clip_still_fits_the_cap_and_is_really_shrunk(tmp_path):+ # The regression that motivated the cap: a source-rate re-encode outgrows+ # max_bytes, so the guard forwarded the original after paying to convert it.+ clip = _clip(tmp_path, "long.mp4", "1280x720", rate = 30, seconds = 30)++ shrunk = shrink_video_for_llama(clip, _CAP)++ assert shrunk is not clip+ assert len(base64.b64decode(shrunk)) < len(base64.b64decode(clip))+ assert _probe(tmp_path, shrunk)["width"] == 640+++def _frames_at(tmp_path: Path, clip_b64: str, name: str, fps: int) -> int:+ """Frames llama-server would actually get, via the same `-vf fps=` it runs."""+ path = tmp_path / name+ path.write_bytes(base64.b64decode(clip_b64))+ # Dimensions only: a raw stream has no container duration, so _probe would+ # KeyError before the frames could be counted.+ dims = subprocess.run(+ [+ "ffprobe",+ "-v",+ "error",+ "-select_streams",+ "v:0",+ *("-show_entries", "stream=width,height"),+ *("-of", "json", str(path)),+ ],+ check = True,+ capture_output = True,+ )+ stream = json.loads(dims.stdout)["streams"][0]+ raw = subprocess.run(+ [+ "ffmpeg",+ "-nostdin",+ "-loglevel",+ "error",+ "-i",+ str(path),+ "-vf",+ f"fps={fps}",+ "-f",+ "rawvideo",+ "-pix_fmt",+ "rgb24",+ "-",+ ],+ check = True,+ capture_output = True,+ ).stdout+ return len(raw) // (stream["width"] * stream["height"] * 3)+++@needs_ffmpeg[email protected]("seconds", [0.1, 0.3, 0.4])+def test_a_sub_second_clip_still_reaches_the_model(tmp_path, seconds):+ # At the 1 fps Studio pins, anything under ~0.5s decodes to NOTHING. These+ # worked at the old 4 fps default, so this is a regression guard.+ clip = _clip(tmp_path, f"tiny{seconds}.mp4", "320x180", rate = 30, seconds = seconds)+ assert _frames_at(tmp_path, clip, f"tiny-src{seconds}", 1) == 0++ shrunk = shrink_video_for_llama(clip, _CAP)++ assert _frames_at(tmp_path, shrunk, f"tiny-out{seconds}", 1) >= 1+ assert _probe(tmp_path, shrunk)["duration"] == pytest.approx(MIN_SAMPLED_SECONDS, abs = 0.15)+++@needs_ffmpeg+def test_a_long_enough_clip_within_the_budget_is_still_left_alone(tmp_path):+ clip = _clip(tmp_path, "ok.mp4", "640x360", rate = 10, seconds = 2)++ assert shrink_video_for_llama(clip, _CAP) is clip+++def test_a_probe_that_is_not_an_object_forwards_the_clip_untouched(monkeypatch):+ # A build answering `-of json` with a non-object must fall back, not raise+ # AttributeError past the caller's except.+ class _Result:+ returncode = 0+ stdout = b"[]"+ stderr = b""++ monkeypatch.setattr(llama_video_input, "_run", lambda *a, **k: _Result())+ monkeypatch.setattr(llama_video_input.shutil, "which", lambda name: f"/usr/bin/{name}")++ assert shrink_video_for_llama("AAAA", _CAP) == "AAAA"+++@needs_ffmpeg+def test_an_undecodable_clip_is_forwarded_untouched(tmp_path):+ clip = base64.b64encode(b"\x00\x00\x00\x18ftypmp42" + b"\x00" * 64).decode("ascii")++ assert shrink_video_for_llama(clip, _CAP) is clip+++def test_a_machine_without_ffmpeg_forwards_the_clip_untouched(monkeypatch):+ monkeypatch.setattr(llama_video_input.shutil, "which", lambda _name: None)+ called = []+ monkeypatch.setattr(llama_video_input, "_run", lambda *a, **k: called.append(a))++ assert shrink_video_for_llama("AAAA", _CAP) == "AAAA"+ assert called == []+++def test_a_timed_out_shrink_forwards_the_clip_untouched(monkeypatch):+ monkeypatch.setattr(llama_video_input.shutil, "which", lambda name: f"/bin/{name}")++ def timeout(argv, _timeout):+ raise subprocess.TimeoutExpired(argv, _timeout)++ monkeypatch.setattr(llama_video_input, "_run", timeout)++ assert shrink_video_for_llama("AAAA", _CAP) == "AAAA"+++def test_the_gguf_route_shrinks_the_clip_after_the_size_check_and_before_injection():+ source = (Path(__file__).resolve().parent.parent / "routes" / "inference.py").read_text(+ encoding = "utf-8"+ )+ start = source.index('"Video provided but the current GGUF model cannot take video input. "')+ check = source.index("_video_b64_rejection(payload.video_base64)", start)+ # Bare name: the call has been reflowed twice, and each time an anchor on the+ # full call raised ValueError instead of reporting an ordering problem.+ shrink = source.index("shrink_video_for_llama,", start)+ inject = source.index("_inject_video_part(gguf_messages, video_b64)", start)+ assert check < shrink < inject+++def test_the_conversions_result_is_what_gets_injected():+ """The ordering test above matches source text, so it cannot see whether the+ conversion's RESULT is used. Dropping just the assignment silently disables+ the whole feature -- the clip is still converted, then thrown away and the+ original injected -- while every substring it searches for stays put.++ Read the structure instead: the awaited call must be bound to a name, and+ that same name must be the one handed to _inject_video_part.+ """+ import ast++ source = (Path(__file__).resolve().parent.parent / "routes" / "inference.py").read_text(+ encoding = "utf-8"+ )+ tree = ast.parse(source)++ def is_shrink_call(node) -> bool:+ call = node.value if isinstance(node, ast.Await) else node+ if not isinstance(call, ast.Call):+ return False+ return any(isinstance(a, ast.Name) and a.id == "shrink_video_for_llama" for a in call.args)++ bound_to = {+ target.id+ for node in ast.walk(tree)+ if isinstance(node, ast.Assign) and is_shrink_call(node.value)+ for target in node.targets+ if isinstance(target, ast.Name)+ }+ assert bound_to, (+ "shrink_video_for_llama's result is not assigned to anything: the clip "+ "is converted and then discarded, leaving the original to be injected"+ )++ injected = {+ arg.id+ for node in ast.walk(tree)+ if isinstance(node, ast.Call)+ and isinstance(node.func, ast.Name)+ and node.func.id == "_inject_video_part"+ for arg in node.args+ if isinstance(arg, ast.Name)+ }+ assert bound_to & injected, (+ f"the shrunk clip is bound to {sorted(bound_to)} but _inject_video_part "+ f"is given {sorted(injected)}"+ )+++def test_the_route_hands_the_helper_bare_base64_with_any_data_header_gone():+ """`shrink_video_for_llama` documents a bare-base64 contract, and the route+ is what has to honour it. A data: URL reaching ffmpeg would decode to+ garbage and be forwarded unchanged, losing the speedup silently.+ """+ import routes.inference as inference_route++ payload = base64.b64encode(b"not-really-a-clip").decode("ascii")++ bare, rejection = inference_route._video_b64_rejection(f"data:video/mp4;base64,{payload}")+ assert rejection is None+ assert bare == payload++ already_bare, rejection = inference_route._video_b64_rejection(payload)+ assert rejection is None+ assert already_bare == payload+++def test_an_oversize_upload_is_refused_before_any_conversion_runs():+ """The 413 has to come first: spawning ffmpeg for a clip the route is about+ to reject burns a worker thread and up to the shrink timeout for nothing.+ """+ import routes.inference as inference_route++ too_big = "A" * (inference_route._MAX_VIDEO_B64_CHARS + 4)++ _, rejection = inference_route._video_b64_rejection(too_big)++ assert rejection is not None+ assert rejection[0] == 413+++def test_the_requested_rate_is_read_from_the_extras_and_the_environment():+ from core.inference.llama_cpp import requested_video_fps++ assert requested_video_fps(None, env = {}) is None+ assert requested_video_fps(["--video-fps", "8"], env = {}) == 8+ assert requested_video_fps(["--video-fps=8"], env = {}) == 8+ # llama.cpp resolves a repeated flag last-wins, so the encoder must agree.+ assert requested_video_fps(["--video-fps", "8", "--video-fps", "2"], env = {}) == 2+ # argv beats the environment: llama.cpp reads the env var when it registers+ # the option, and argv overrides it afterwards (measured on b10976).+ assert requested_video_fps(["--video-fps", "8"], env = {"LLAMA_ARG_VIDEO_FPS": "1"}) == 8+ assert requested_video_fps([], env = {"LLAMA_ARG_VIDEO_FPS": "8"}) == 8+ # llama.cpp accepts the underscore spelling too, so an exact match on the+ # hyphen would silently miss it.+ assert requested_video_fps(["--video_fps", "8"], env = {}) == 8+ assert requested_video_fps(["--video_fps=8"], env = {}) == 8+ assert requested_video_fps(["--video_fps", "8", "--video-fps", "2"], env = {}) == 2+ assert requested_video_fps(["--video-fps", "abc"], env = {}) is None+ assert requested_video_fps(["--video-fps", "0"], env = {}) is None+ assert requested_video_fps(["--video-fps"], env = {}) is None+++def test_a_higher_requested_rate_raises_the_encode_ceiling():+ # The server samples AFTER this transcode, so a dropped rate can only come+ # back as duplicates. Below the default the ceiling holds.+ chain = llama_video_input._filter_chain(MAX_FRAME_PIXELS, 30.0, 5.0, True, 8.0)+ assert "fps=8" in chain+ assert llama_video_input._rate_ceiling(None) == MAX_FRAME_RATE+ assert llama_video_input._rate_ceiling(1.0) == MAX_FRAME_RATE+ assert llama_video_input._rate_ceiling(2.0) == MAX_FRAME_RATE+ assert llama_video_input._rate_ceiling(24.0) == 24.0+++@needs_ffmpeg+def test_an_eight_fps_override_keeps_eight_fps_of_frames(tmp_path):+ clip = _clip(tmp_path, "fast8.mp4", "1280x720", rate = 30, seconds = 2)++ default = shrink_video_for_llama(clip, _CAP)+ override = shrink_video_for_llama(clip, _CAP, sampled_fps = 8.0)++ assert _frame_count(tmp_path, default, "d8") == MAX_FRAME_RATE * 2+ assert _frame_count(tmp_path, override, "o8") == 8 * 2+++@needs_ffmpeg+def test_a_short_clip_with_no_container_duration_is_still_padded(tmp_path):+ # A raw stream carries no format.duration, so the sub-second floor never+ # fired. Annex B has no packet timestamps either, only durations, so the+ # fallback has to sum those rather than read a presentation time.+ path = tmp_path / "raw.h264"+ subprocess.run(+ [+ "ffmpeg",+ "-nostdin",+ "-loglevel",+ "error",+ "-y",+ *("-f", "lavfi", "-i", "testsrc2=size=320x180:rate=30"),+ *("-t", "0.3", "-c:v", "libx264", "-bsf:v", "h264_mp4toannexb"),+ *("-f", "h264", str(path)),+ ],+ check = True,+ )+ clip = base64.b64encode(path.read_bytes()).decode("ascii")+ ffprobe = shutil.which("ffprobe")+ assert llama_video_input._frame_geometry(ffprobe, path)[2] == pytest.approx(0.3, abs = 0.05)+ assert _frames_at(tmp_path, clip, "raw-src", 1) == 0++ shrunk = shrink_video_for_llama(clip, _CAP)++ assert _frames_at(tmp_path, shrunk, "raw-out", 1) >= 1+++@needs_ffmpeg+def test_the_packet_fallback_is_only_consulted_when_the_container_is_silent(tmp_path, monkeypatch):+ # It reads packets, so a long ordinary clip must never reach it.+ called = []+ real = llama_video_input._packet_duration+ monkeypatch.setattr(+ llama_video_input,+ "_packet_duration",+ lambda *a, **k: called.append(1) or real(*a, **k),+ )+ clip = _clip(tmp_path, "ordinary.mp4", "1280x720", rate = 30, seconds = 2)++ shrink_video_for_llama(clip, _CAP)++ assert not called