affaan-m/ECC · error · RuntimeError

cannot open video

Error message

cannot open video: {path}

What it means

probe() opens the video with OpenCV's VideoCapture and raises immediately if the backend cannot open the file. It means the file path does not exist, is unreadable, or is in a format/codec OpenCV's backend cannot decode.

Solutions

  1. Verify the path exists and is a readable file: Path(path).is_file() and size > 0
  2. Check the file plays with ffprobe/ffplay to rule out corruption or exotic codecs
  3. Install an OpenCV build with FFmpeg (pip install opencv-python) or re-encode the video to H.264 MP4
  4. Confirm you are passing a local file path, not a URL (download it first)

Example fix

// before
probe("out/video.mp4")  # FileNotFoundError -> RuntimeError: cannot open video
// after
path = Path("out/video.mp4")
if not path.is_file() or path.stat().st_size == 0:
    raise FileNotFoundError(path)
info = probe(path)
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path
p = Path(path)
assert p.is_file() and p.stat().st_size > 0, f"bad video input: {p}"

Try / catch

try:
    info = probe(path)
except RuntimeError as e:
    if str(e).startswith("cannot open video"):
        reencode_with_ffmpeg(path); info = probe(path)
    else: raise

Prevention

When it happens

Trigger: Calling probe() with a nonexistent path, a path without read permission, a corrupted file, or a codec/container (e.g. some HEVC/AV1 variants) unsupported by the installed OpenCV build.

Common situations: Typo'd relative path; downloading failed earlier so the file is missing/zero bytes; OpenCV built without FFmpeg support for the codec; trying to probe a URL or a directory instead of a file.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


AI-assisted analysis of affaan-m/ECC@8321021c54 (2026-09-16). Data as JSON: /api/errors/727558edd4c373c2. Report an issue: GitHub.

Appendix: source

Thrown at skills/taste-distillation/scripts/taste/frames.py:31

@dataclass
class VideoInfo:
    path: Path
    width: int
    height: int
    fps: float
    frame_count: int

    @property
    def duration(self) -> float:
        return self.frame_count / self.fps if self.fps else 0.0


def probe(path: str | Path) -> VideoInfo:
    path = Path(path)
    cap = cv2.VideoCapture(str(path))
    if not cap.isOpened():
        raise RuntimeError(f"cannot open video: {path}")
    info = VideoInfo(
        path=path,
        width=int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
        height=int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
        fps=float(cap.get(cv2.CAP_PROP_FPS)) or 24.0,
        frame_count=int(cap.get(cv2.CAP_PROP_FRAME_COUNT)),
    )
    cap.release()
    return info


def sample_frames(
    path: str | Path,
    n: int = 48,
    max_edge: int = 512,
    skip_edges: float = 0.02,
) -> list[np.ndarray]:
    """Evenly sample ``n`` frames as float32 RGB in [0, 1].

View on GitHub (pinned to 8321021c54)