opencv
config_error
ai_generated
partial
OpenCV: FFMPEG: Failed to initialize codec 'libx264' for stream 0
ID: opencv/videoio-cap-ffmpeg-init-failed-codec
75%Fix Rate
82%Confidence
1Evidence
2023-08-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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.htmlWorkarounds
-
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.
-
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')
-
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
中文步骤
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.
Use a different video codec like 'avc1' or 'mp4v' when writing video: fourcc = cv2.VideoWriter_fourcc(*'avc1')
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:
-
90% fail
The precompiled OpenCV wheels often exclude non-free codecs like libx264 for licensing reasons.
-
95% fail
These variables do not enable missing codecs; they only configure existing ones.