opencv runtime_error ai_generated partial

OpenCV: FFMPEG: init: Could not find codec parameters for stream 0 (Video: h264, none): unspecified size

ID: opencv/video-capture-ffmpeg-init-failed

Also available as: JSON · Markdown · 中文
85%Fix Rate
86%Confidence
1Evidence
2023-11-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.0 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

Root Cause

FFmpeg cannot parse the video stream's codec parameters, often due to a corrupted or incomplete video file header.

generic

中文

FFmpeg无法解析视频流的编解码器参数,通常由于视频文件头损坏或不完整。

Official Documentation

https://docs.opencv.org/4.x/dd/d43/tutorial_py_video_display.html

Workarounds

  1. 90% success Re-encode the video file using FFmpeg with a clean header: ffmpeg -i corrupted.mp4 -c:v libx264 -preset fast -crf 23 -c:a aac fixed.mp4. Then open the fixed file in OpenCV.
    Re-encode the video file using FFmpeg with a clean header: ffmpeg -i corrupted.mp4 -c:v libx264 -preset fast -crf 23 -c:a aac fixed.mp4. Then open the fixed file in OpenCV.
  2. 70% success Use OpenCV's VideoCapture with the CAP_FFMPEG backend and set the FFMPEG debug flag to get more details: cv::VideoCapture cap; cap.set(cv::CAP_PROP_FFMPEG_DEBUG, 1); cap.open("video.mp4");
    Use OpenCV's VideoCapture with the CAP_FFMPEG backend and set the FFMPEG debug flag to get more details: cv::VideoCapture cap; cap.set(cv::CAP_PROP_FFMPEG_DEBUG, 1); cap.open("video.mp4");

中文步骤

  1. Re-encode the video file using FFmpeg with a clean header: ffmpeg -i corrupted.mp4 -c:v libx264 -preset fast -crf 23 -c:a aac fixed.mp4. Then open the fixed file in OpenCV.
  2. Use OpenCV's VideoCapture with the CAP_FFMPEG backend and set the FFMPEG debug flag to get more details: cv::VideoCapture cap; cap.set(cv::CAP_PROP_FFMPEG_DEBUG, 1); cap.open("video.mp4");

Dead Ends

Common approaches that don't work:

  1. 75% fail

    The codec (h264) is present, but the video file itself is malformed; reinstalling does not fix file corruption.

  2. 60% fail

    Changing the extension does not fix the underlying stream metadata; the file must be properly transcoded.