affaan-m/ECC · error · RuntimeError

cannot open video

Error message

cannot open video: {path}

What it means

`probe()` opens a video with OpenCV's `cv2.VideoCapture`; if OpenCV cannot open the container or path, the cap is not marked as opened and the function raises a RuntimeError with the offending path. This is the library's guard against silently probing a nonexistent or unreadable video. It fires before any metadata (width, height, fps, frame count) is read.

Solutions

  1. Verify the path exists and is readable: `Path(path).is_file()` and `os.access(path, os.R_OK)` before calling probe().
  2. Confirm OpenCV can decode the file's codec — check with `ffprobe <file>`; if the codec is unsupported, re-encode with FFmpeg (`ffmpeg -i in.mov -c:v libx264 out.mp4`).
  3. Reinstall OpenCV with a FFmpeg-enabled wheel: `pip install --force-reinstall opencv-python` (avoid headless builds without video IO).
  4. Convert exotic containers (e.g. .braw, HEVC variants) to mp4 before probing.

Example fix

// before
info = probe('footage/clip')          # directory, no extension
// after
p = Path('footage/clip.mp4')
if not p.is_file():
    raise FileNotFoundError(p)
info = probe(p)
Defensive patterns

Strategy: try-catch

Validate before calling

from pathlib import Path
p = Path(path)
assert p.is_file(), f"not a file: {p}"
assert os.access(p, os.R_OK), f"not readable: {p}"

Type guard

def is_probable_video(path: str | Path) -> bool:
    p = Path(path)
    return p.is_file() and p.suffix.lower() in {'.mp4', '.mov', '.mkv', '.avi', '.webm'}

Try / catch

try:
    info = probe(p)
except RuntimeError as e:
    if str(e).startswith('cannot open video'):
        logger.warning('unreadable video %s: %s', p, e)
        return None
    raise

Prevention

When it happens

Trigger: Calling `probe(path)` where the path does not exist, is a directory, has an unsupported/undecodable container or codec, or the file lacks read permissions — any case where `cap.isOpened()` returns False.

Common situations: Typos or wrong relative paths when passing CLI args; videos with codecs OpenCV's backend (FFmpeg build) does not support; corrupted downloads; files on unmounted network shares; OpenCV installed without FFmpeg/GStreamer video-IO support.

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/032e9ce7beb774ed. Report an issue: GitHub.

Appendix: source

Thrown at skills/taste-application/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)