opencv config_error ai_generated partial

OpenCV: FFMPEG: 无法为流 0 初始化编解码器 'libx264'

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

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

其他格式: JSON · Markdown 中文 · English
75%修复率
82%置信度
1证据数
2023-08-10首次发现

版本兼容性

版本状态引入弃用备注
4.5.5 active — — —
4.6.0 active — — —
4.7.0 active — — —
4.8.0 active — — —

根因分析

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

English

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

generic

官方文档

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

解决方案

  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

无效尝试

常见但无效的做法:

  1. 90% 失败

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

  2. 95% 失败

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