sgl-project/sglang · error · IOError

Could not open video file:{video_path}

Error message

Could not open video file:{video_path}

What it means

encode_video_base64 could not open the video file with OpenCV (cv2.VideoCapture.isOpened() false): the path doesn't exist, isn't readable, or the codec/container is unsupported in this opencv build.

Source

Thrown at python/sglang/utils.py:288

    # Convert to bytes
    buffered = BytesIO()

    # frame_format = str(os.getenv('FRAME_FORMAT', "JPEG"))

    im_pil.save(buffered, format="PNG")

    frame_bytes = buffered.getvalue()

    # Return the bytes of the frame
    return frame_bytes


def encode_video_base64(video_path: str, num_frames: int = 16):
    import cv2  # pip install opencv-python-headless

    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        raise IOError(f"Could not open video file:{video_path}")

    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    print(f"target_frames: {num_frames}")

    frame_indices = np.linspace(0, total_frames - 1, num_frames, dtype=int)

    frames = []
    for _ in range(total_frames):
        ret, frame = cap.read()
        if ret:
            frames.append(frame)
        else:
            # Handle the case where the frame could not be read
            # print(f"Warning: Could not read frame at index {i}.")
            pass

    cap.release()

View on GitHub (pinned to 0132848349)

Solutions

  1. Verify the path exists and the file is a valid readable video
  2. Install/switch to an opencv build with the needed codec support (e.g. pip install opencv-python)
  3. Check file permissions
Defensive patterns

Strategy: validation

Validate before calling

import os
assert os.path.isfile(video_path) and os.access(video_path, os.R_OK)

Try / catch

try:
    encode_video_base64(path)
except IOError as e:
    if 'Could not open video' in str(e): fix_path_or_codec()

Prevention

When it happens

Trigger: Calling encode_video_base64 with a wrong/corrupt path, unsupported codec, or missing opencv codec support; used by video benchmark/nextqa helpers.

Common situations: Benchmark script pointing to a non-existent video; opencv-python-headless without ffmpeg backing; permission issues on dataset paths.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/b1c1e0236e3cbb6d. Report an issue: GitHub.