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

- **ID:** `opencv/videoio-cap-ffmpeg-init-failed-codec`
- **领域:** opencv
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

FFmpeg 未编译所需的编解码器（libx264），或者系统中安装的 FFmpeg 不提供该编解码器。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 4.5.5 | active | — | — |
| 4.6.0 | active | — | — |
| 4.7.0 | active | — | — |
| 4.8.0 | active | — | — |

## 解决方案

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
   ```

## 无效尝试

- **** — The precompiled OpenCV wheels often exclude non-free codecs like libx264 for licensing reasons. (90% 失败率)
- **** — These variables do not enable missing codecs; they only configure existing ones. (95% 失败率)
