affaan-m/ECC · error · RuntimeError

cannot open

Error message

cannot open {src}

What it means

grade_clip_direct reads frames itself and pipes raw RGB into ffmpeg. It uses OpenCV's VideoCapture to read the source, and if cap.isOpened() is False it raises RuntimeError('cannot open <src>'). OpenCV reports open failure for missing files and, more commonly, for files its backend cannot demux/decode. This check runs after sample_frames/probe, so the file usually exists and the failure is codec/backend related.

Solutions

  1. Confirm the file plays with ffplay src / decodes with ffmpeg -i src; if ffmpeg fails too, the file itself is bad - re-copy or re-export it.
  2. Re-encode to a universally decodable intermediate first: ffmpeg -i src -c:v libx264 -pix_fmt yuv420p -c:a aac h264.mp4, then grade the intermediate.
  3. Reinstall OpenCV with video backends: pip install opencv-python (not headless), or an OPENCV_FFMPEG-enabled build; set OPENCV_VIDEOIO_PRIORITY list if needed.
  4. Try opening it yourself before calling to see the backend error: cap = cv2.VideoCapture(src); print(cap.isOpened(), cap.get(cv2.CAP_PROP_FOURCC)).
  5. Verify the path is reachable and complete: check file size > 0 and that network volumes are mounted.

Example fix

// before
grade_clip_direct('clip_h265.mov', 'out.mp4')  # RuntimeError: cannot open clip_h265.mov
// after
subprocess.run(['ffmpeg','-y','-i','clip_h265.mov','-c:v','libx264','-pix_fmt','yuv420p','clip_h264.mp4'], check=True)
grade_clip_direct('clip_h264.mp4', 'out.mp4')
Defensive patterns

Strategy: fallback

Validate before calling

import cv2
cap = cv2.VideoCapture(str(src))
ok = cap.isOpened()
cap.release()
if not ok:
    raise SystemExit(f'OpenCV cannot open {src}; re-encode to H.264 first')

Try / catch

try:
    grade_clip_direct(src, dst)
except RuntimeError as e:
    if 'cannot open' in str(e):
        fixed = Path(src).with_suffix('.h264.mp4')
        subprocess.run(['ffmpeg','-y','-i',str(src),'-c:v','libx264','-pix_fmt','yuv420p',str(fixed)], check=True)
        grade_clip_direct(fixed, dst)
    else:
        raise

Prevention

When it happens

Trigger: VideoCapture(str(src)) fails on: a file with a codec OpenCV's backend cannot decode (e.g. HEVC/H.265 or ProRes with a plain ffmpeg-less OpenCV build), a container OpenCV cannot demux, a zero-byte or truncated download, or a valid file whose extension confuses backend selection.

Common situations: OpenCV (opencv-python-headless) built without FFMPEG/GStreamer support so almost no video opens; phone-recorded HEVC clips on a machine without HEVC support; files streamed/downloaded and still incomplete; NAS/network mount not mounted so path exists check passed but read fails.

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

Appendix: source

Thrown at skills/taste-distillation/scripts/taste/grade.py:779

    half a gigabyte at no real cost in throughput.

    Use this for the automated pipeline, where accuracy is what matters and
    nobody is looking at the intermediate. Keep ``look.cube`` for Resolve,
    where an artist wants a node they can dial back, reorder, or override -
    and where a couple of units of chroma error is a starting point, not a
    defect.
    """
    import cv2 as _cv2

    src, dst = Path(src), Path(dst)
    from . import frames as _frames

    source = analyze(_frames.sample_frames(src, n=n_measure))
    info = _frames.probe(src)

    cap = _cv2.VideoCapture(str(src))
    if not cap.isOpened():
        raise RuntimeError(f"cannot open {src}")

    dst.parent.mkdir(parents=True, exist_ok=True)
    cmd = [
        "ffmpeg", "-nostdin", "-loglevel", "error", "-y",
        "-f", "rawvideo", "-pix_fmt", "rgb24",
        "-s", f"{info.width}x{info.height}", "-r", f"{info.fps:.6f}", "-i", "-",
        "-i", str(src), "-map", "0:v", "-map", "1:a?", "-c:a", "copy",
        "-c:v", "libx264", "-crf", str(crf), "-pix_fmt", "yuv420p", str(dst),
    ]
    proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)

    buf: list[np.ndarray] = []

    def flush() -> None:
        if not buf:
            return
        arr = np.stack(buf)
        out = transfer(arr, target=target, source=source, strength=strength,

View on GitHub (pinned to 8321021c54)