Agent Wranglers

MED-15Recommended

Probe each media file and refuse it past its limits

Before transcoding, read each file's duration, resolution and frame rate with a probe such as ffprobe. Refuse the file when any value passes the limit of its upload kind.

How to do it

A file under the size limit can still hold hours of tiny video or a few seconds of 8K at 120 frames per second. Either one costs many minutes of encoding. The size limit alone cannot catch them. Read the values from the probe, never from the file name or what the client sent. Also refuse a video kind with no video stream. Keep the limits in the list from Define each kind of upload once. Run the probe inside the sandbox from Run media tools in a sandbox with no network, since probing decodes the file too.

Examples

JavaScript example

const LIMITS = { maxSeconds: 600, maxWidth: 3840, maxHeight: 2160, maxFps: 60 };

export function mediaProblems(probe, limits = LIMITS) {
  const video = probe.streams.find(stream => stream.codec_type === 'video');
  if (!video) return ['no video stream'];
  const [num, den] = video.avg_frame_rate.split('/').map(Number);
  const problems = [];
  if (Number(probe.format.duration) > limits.maxSeconds) problems.push('too long');
  if (video.width > limits.maxWidth || video.height > limits.maxHeight) problems.push('resolution too high');
  if (den && num / den > limits.maxFps) problems.push('frame rate too high');
  return problems;
}

Python example

from fractions import Fraction

LIMITS = {"max_seconds": 600, "max_width": 3840, "max_height": 2160, "max_fps": 60}

def media_problems(probe: dict, limits: dict = LIMITS) -> list[str]:
    video = next((s for s in probe["streams"] if s["codec_type"] == "video"), None)
    if video is None:
        return ["no video stream"]
    problems = []
    if float(probe["format"]["duration"]) > limits["max_seconds"]:
        problems.append("too long")
    if video["width"] > limits["max_width"] or video["height"] > limits["max_height"]:
        problems.append("resolution too high")
    if video["avg_frame_rate"] != "0/0" and Fraction(video["avg_frame_rate"]) > limits["max_fps"]:
        problems.append("frame rate too high")
    return problems

How to check

Upload a 2 second clip at 7680 by 4320 pixels and a 1 hour clip at 64 by 64 pixels, both under the size limit. Both are refused with a problem response naming the limit, and no encoding job starts.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/files-and-media/video-and-audio/media-limits">Agent Wranglers files and media playbook</a>. In code you write, add a comment next to it with the same link.