opencv config_error ai_generated partial

OpenCV: FFMPEG: Failed to initialize codec 'libx264' for stream 0

ID: opencv/videoio-cap-ffmpeg-init-failed-codec

Also available as: JSON · Markdown · 中文
75%Fix Rate
82%Confidence
1Evidence
2023-08-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.5 active
4.6.0 active
4.7.0 active
4.8.0 active

Root Cause

FFmpeg is not compiled with the required codec (libx264), or the codec is not available in the system's FFmpeg installation.

generic

中文

FFmpeg 未编译所需的编解码器(libx264),或者系统中安装的 FFmpeg 不提供该编解码器。

Official Documentation

https://docs.opencv.org/4.x/d4/da8/group__imgcodecs.html

Workarounds

  1. 85% success Build OpenCV from source with FFmpeg and libx264 support: cmake -D WITH_FFMPEG=ON -D BUILD_OPENCV_FFMPEG=ON .. and ensure libx264-dev is installed.
    Build OpenCV from source with FFmpeg and libx264 support: cmake -D WITH_FFMPEG=ON -D BUILD_OPENCV_FFMPEG=ON .. and ensure libx264-dev is installed.
  2. 70% success Use a different video codec like 'avc1' or 'mp4v' when writing video: fourcc = cv2.VideoWriter_fourcc(*'avc1')
    Use a different video codec like 'avc1' or 'mp4v' when writing video: fourcc = cv2.VideoWriter_fourcc(*'avc1')
  3. 60% success Install a custom FFmpeg build with all codecs and point OpenCV to it via LD_LIBRARY_PATH: export LD_LIBRARY_PATH=/path/to/ffmpeg/lib:$LD_LIBRARY_PATH
    Install a custom FFmpeg build with all codecs and point OpenCV to it via LD_LIBRARY_PATH: export LD_LIBRARY_PATH=/path/to/ffmpeg/lib:$LD_LIBRARY_PATH

中文步骤

  1. Build OpenCV from source with FFmpeg and libx264 support: cmake -D WITH_FFMPEG=ON -D BUILD_OPENCV_FFMPEG=ON .. and ensure libx264-dev is installed.
  2. Use a different video codec like 'avc1' or 'mp4v' when writing video: fourcc = cv2.VideoWriter_fourcc(*'avc1')
  3. Install a custom FFmpeg build with all codecs and point OpenCV to it via LD_LIBRARY_PATH: export LD_LIBRARY_PATH=/path/to/ffmpeg/lib:$LD_LIBRARY_PATH

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The precompiled OpenCV wheels often exclude non-free codecs like libx264 for licensing reasons.

  2. 95% fail

    These variables do not enable missing codecs; they only configure existing ones.