{"record":{"id":"b1c1e0236e3cbb6d","repo":"sgl-project/sglang","slug":"could-not-open-video-file-video-path","errorCode":null,"errorMessage":"Could not open video file:{video_path}","messagePattern":"Could not open video file:(.+?)","errorType":"exception","errorClass":"IOError","httpStatus":null,"severity":"error","filePath":"python/sglang/utils.py","lineNumber":288,"sourceCode":"    # Convert to bytes\n    buffered = BytesIO()\n\n    # frame_format = str(os.getenv('FRAME_FORMAT', \"JPEG\"))\n\n    im_pil.save(buffered, format=\"PNG\")\n\n    frame_bytes = buffered.getvalue()\n\n    # Return the bytes of the frame\n    return frame_bytes\n\n\ndef encode_video_base64(video_path: str, num_frames: int = 16):\n    import cv2  # pip install opencv-python-headless\n\n    cap = cv2.VideoCapture(video_path)\n    if not cap.isOpened():\n        raise IOError(f\"Could not open video file:{video_path}\")\n\n    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))\n    print(f\"target_frames: {num_frames}\")\n\n    frame_indices = np.linspace(0, total_frames - 1, num_frames, dtype=int)\n\n    frames = []\n    for _ in range(total_frames):\n        ret, frame = cap.read()\n        if ret:\n            frames.append(frame)\n        else:\n            # Handle the case where the frame could not be read\n            # print(f\"Warning: Could not read frame at index {i}.\")\n            pass\n\n    cap.release()\n","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/utils.py#L270-L306","documentation":"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.","triggerScenarios":"Calling encode_video_base64 with a wrong/corrupt path, unsupported codec, or missing opencv codec support; used by video benchmark/nextqa helpers.","commonSituations":"Benchmark script pointing to a non-existent video; opencv-python-headless without ffmpeg backing; permission issues on dataset paths.","solutions":["Verify the path exists and the file is a valid readable video","Install/switch to an opencv build with the needed codec support (e.g. pip install opencv-python)","Check file permissions"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import os\nassert os.path.isfile(video_path) and os.access(video_path, os.R_OK)","typeGuard":null,"tryCatchPattern":"try:\n    encode_video_base64(path)\nexcept IOError as e:\n    if 'Could not open video' in str(e): fix_path_or_codec()","preventionTips":["Check file existence/permissions before opening","Install opencv with codec (ffmpeg) support"],"tags":["video","opencv","file-io","multimodal"],"backgroundTag":"file-open-failed","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}