-2 opencv runtime_error ai_generated partial

cv2.error: OpenCV(4.8.0) /modules/videoio/src/cap_images.cpp: error: (-2:Unspecified error) could not find encoder for codec 0x47504A4D

ID: opencv/video-writer-codec-not-found

Also available as: JSON · Markdown · 中文
75%Fix Rate
88%Confidence
1Evidence
2023-06-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

Root Cause

The specified codec (e.g., MJPG, H264) is not supported by the installed FFmpeg or encoder backend on the system.

generic

中文

指定的编解码器(例如 MJPG、H264)不被系统上安装的 FFmpeg 或编码器后端支持。

Official Documentation

https://docs.opencv.org/4.x/dd/d9e/classcv_1_1VideoWriter.html

Workarounds

  1. 80% success List available encoders with 'ffmpeg -encoders' and use a codec that appears in the list. Common working codecs: 'mp4v' (for MP4), 'XVID' (for AVI), 'MJPG' for motion JPEG.
    List available encoders with 'ffmpeg -encoders' and use a codec that appears in the list. Common working codecs: 'mp4v' (for MP4), 'XVID' (for AVI), 'MJPG' for motion JPEG.
  2. 70% success Install a more complete FFmpeg build (e.g., from conda-forge or compile with --enable-libx264) to add missing codecs.
    Install a more complete FFmpeg build (e.g., from conda-forge or compile with --enable-libx264) to add missing codecs.

中文步骤

  1. List available encoders with 'ffmpeg -encoders' and use a codec that appears in the list. Common working codecs: 'mp4v' (for MP4), 'XVID' (for AVI), 'MJPG' for motion JPEG.
  2. Install a more complete FFmpeg build (e.g., from conda-forge or compile with --enable-libx264) to add missing codecs.

Dead Ends

Common approaches that don't work:

  1. Reinstall OpenCV with --with-ffmpeg flag 65% fail

    The issue is not build-time but runtime; FFmpeg may be installed but the specific codec might be missing or disabled in the FFmpeg build.

  2. Change codec ID to a random fourcc code 90% fail

    Random codecs are even less likely to be supported; must use a codec known to work with the installed FFmpeg.

  3. Use .avi extension instead of .mp4 50% fail

    The extension does not determine the codec; the fourcc code must match a supported encoder.