Skip to main content

Function Signature

Description

Extracts frames from a video file or processes a sequence of image frames. Applies smart resizing and frame sampling based on FPS and pixel constraints. Supports multiple video reading backends (torchcodec, decord, torchvision).

Parameters

Dict[str, Any]
required
Dictionary containing video information and processing parameters.Required keys:
  • video: Video source (file path, URL) or list of frame paths
Optional frame sampling keys (mutually exclusive):
  • fps: Target frames per second for extraction (default: 2.0)
  • nframes: Exact number of frames to extract
Optional frame sampling constraints (only with fps):
  • min_frames: Minimum frames to extract (default: 4)
  • max_frames: Maximum frames to extract (default: 768)
Optional time range keys:
  • video_start: Start time in seconds
  • video_end: End time in seconds
Optional resize keys:
  • resized_height: Target height
  • resized_width: Target width
  • min_pixels: Minimum pixels per frame (default: 128 * patch_factor²)
  • max_pixels: Maximum pixels per frame (calculated dynamically)
  • total_pixels: Total pixel budget for all frames (default: MODEL_SEQ_LEN * patch_factor² * 0.9)
int
default:"14"
The patch size used by the vision encoder.Common values:
  • 14 for Qwen2VL and Qwen2.5VL
  • 16 for Qwen3VL
bool
default:"False"
Whether to return the sample FPS alongside the video tensor.
bool
default:"False"
Whether to return video metadata (fps, frame indices, total frames, backend) alongside the video tensor.Required for Qwen3VL models.

Returns

torch.Tensor
Video tensor with shape (T, C, H, W) where:
  • T: Number of frames (divisible by 2)
  • C: Color channels (3 for RGB)
  • H: Frame height (divisible by patch_factor)
  • W: Frame width (divisible by patch_factor)
Dict[str, Any]
Only returned if return_video_metadata=True. Contains:
  • fps: Original video FPS
  • frames_indices: List of extracted frame indices
  • total_num_frames: Total frames in the video
  • video_backend: Backend used (“torchcodec”, “decord”, or “torchvision”)
float
Only returned if return_video_sample_fps=True. The effective FPS of the sampled frames.

Video Reading Backends

The function automatically selects the best available backend:
  1. torchcodec (preferred, fastest)
  2. decord (good performance)
  3. torchvision (fallback, always available)
You can force a specific backend using the environment variable:

Usage Examples

Basic Video Loading

Custom FPS Sampling

Exact Frame Count

Time Range Selection

Frame Sequence Input

Custom Resolution

With Metadata (Qwen3VL)

Frame Sampling Algorithm

The function uses intelligent frame sampling:
  1. Determine frame count:
    • If nframes specified: Use that value (rounded to multiple of 2)
    • If fps specified: Calculate nframes = total_frames / video_fps * fps
    • Apply min_frames and max_frames constraints
  2. Extract frames: Uniformly sample frames across the specified range
  3. Resize frames: Apply smart_resize to each frame

Pixel Budget Management

The function automatically manages pixel budgets to prevent exceeding model limits:
You can customize the total pixel budget:
Or set via environment variable:

Error Handling

Invalid Time Range

Invalid Frame Count

Performance Tips

  1. Use torchcodec for best performance (pip install torchcodec)
  2. Limit max_frames to reduce processing time
  3. Use frame sequences for pre-extracted frames to skip video decoding
  4. Adjust total_pixels based on your model’s sequence length limit

Environment Variables

See Also