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
# 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 sys
import tempfile
from pathlib import Path
from typing import Mapping, 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; higher rates only add bytes mtmd drops.
MAX_FRAME_RATE = 4
# fps starts half an interval in, so clips under ~0.5s give NO frames at 1 fps; one whole second leaves user-rate headroom.
MIN_SAMPLED_SECONDS = 1.0
_PROBE_TIMEOUT_S = 30
_SHRINK_TIMEOUT_S = 300
def video_tools(ffmpeg_dir: Optional[str] = None) -> Optional[tuple[str, str]]:
names = ("ffmpeg.exe", "ffprobe.exe") if sys.platform == "win32" else ("ffmpeg", "ffprobe")
if ffmpeg_dir:
found = [shutil.which(name, path = ffmpeg_dir) for name in names]
else:
found = [shutil.which(name) for name in names]
return (found[0], found[1]) if found[0] and found[1] else None
def _run(
argv: list[str],
timeout: float,
env: Optional[Mapping[str, str]] = None,
) -> subprocess.CompletedProcess:
return subprocess.run(
argv,
stdin = subprocess.DEVNULL,
capture_output = True,
timeout = timeout,
env = child_env_without_native_path_secret(env),
**windows_hidden_subprocess_kwargs(),
)
def _frame_geometry(
ffprobe: str,
clip: Path,
env: Optional[Mapping[str, str]] = None,
) -> 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",
# A probed playlist would otherwise open the URLs it names.
"-protocol_whitelist",
"file",
str(clip),
],
_PROBE_TIMEOUT_S,
env = env,
)
if result.returncode != 0:
return None, None, None
# A build answering -of json with a non-object must fall back, not raise AttributeError past except.
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, env = env)
return area, _frame_rate(streams[0].get("avg_frame_rate")), duration
def _packet_duration(
ffprobe: str,
clip: Path,
env: Optional[Mapping[str, str]] = None,
) -> 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",
"-protocol_whitelist",
"file",
str(clip),
],
_PROBE_TIMEOUT_S,
env = env,
)
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 has no timestamps, only durations, so summing is the only option 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 frame rate worth encoding; a user asking for more than MAX_FRAME_RATE raises it,
since the server samples after this transcode and can only duplicate frames 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:
"""Pad, then rate cap, then scale; order matters since the cap also emits nothing on tiny clips, leaving tpad no frame; cap only goes down."""
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,
ffmpeg_dir: Optional[str] = None,
env: Optional[Mapping[str, str]] = 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 or
an over-max_bytes result pass through."""
from utils.media_playlist import refuse_playlist
try:
raw = base64.b64decode(video_b64)
except (binascii.Error, ValueError):
return video_b64
# Before the tool check: without ffmpeg the clip still reaches llama-server's own FFmpeg.
refuse_playlist(raw)
tools = video_tools(ffmpeg_dir)
if tools is None:
return video_b64
ffmpeg, ffprobe = tools
try:
with tempfile.TemporaryDirectory(prefix = "unsloth-video-") as tmp:
source = Path(tmp) / "clip"
source.write_bytes(raw)
area, rate, duration = _frame_geometry(ffprobe, source, env = env)
if area is None:
return video_b64
oversized = area > max_pixels
too_short = duration is not None and 0 < duration < MIN_SAMPLED_SECONDS
raw_bytes = len(raw)
if not oversized and not too_short and raw_bytes <= max_bytes:
return video_b64
# Keeping the decoded copy alive just to log its length costs up to the upload cap.
del raw
shrunk = Path(tmp) / "shrunk.mkv"
# A size-only re-encode has no filter, and ffmpeg refuses an empty -vf.
chain = _filter_chain(max_pixels, rate, duration, oversized, sampled_fps)
filters = ["-vf", chain] if chain else []
result = _run(
[
ffmpeg,
"-nostdin",
"-hide_banner",
"-loglevel",
"error",
"-protocol_whitelist",
"file",
"-i",
str(source),
"-map",
"0:v:0",
"-an",
"-sn",
"-dn",
*filters,
"-c:v",
"mpeg4",
# mpeg4 is widely supported but can outgrow low-bitrate sources; -fs caps output at the upload limit.
"-q:v",
"5",
"-fs",
str(max_bytes),
"-f",
"matroska",
str(shrunk),
],
_SHRINK_TIMEOUT_S,
env = env,
)
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_b64