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

- **ID:** `opencv/videoio-cap-ffmpeg-init-failed-codec`
- **Domain:** opencv
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 75%

## Root Cause

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

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 4.5.5 | active | — | — |
| 4.6.0 | active | — | — |
| 4.7.0 | active | — | — |
| 4.8.0 | active | — | — |

## Workarounds

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.** (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.
   ```
2. **Use a different video codec like 'avc1' or 'mp4v' when writing video: fourcc = cv2.VideoWriter_fourcc(*'avc1')** (70% success)
   ```
   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** (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
   ```

## Dead Ends

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