cv::VideoCapture.isOpen() returns TRUE even on some errors (bug?)

i’m very new to opencv, so i don’t know if this is expected behaviour or not (but it’s certainly not intuitive…). it seems that when trying to read an MP4 video, even though openh264 returns an error (and emits it to the console), OpenCV says everything is okay. in other words, openh264 can’t decode the frames, cv::VideoCapture.isOpen() nevertheless returns true.

code snippet :

VideoCapture cap(argv[1]);
if (cap.isOpened()) {
    cout << argv[1] << " was opened correctly!\n";
    cap.release();
} else {
    cerr << argv[1] << ": can't open video for reading\n";
}

output :

[libopenh264 @ 0x5c35c0] DecodeFrame failed
danse.mp4 was opened correctly!

additionally, obviously continuing to try to read the file after libopenh264 returns the error gives no results.

shouldn’t VideoCapture.isOpen() return an error condition?

yeah it’s an old and unloved API, with plenty of warts.

in this case though, ffmpeg is to blame, not OpenCV.

someone built OpenCV against a bare ffmpeg, which doesn’t come with the usual H.264 codecs, but only libopenh264 support. that is probably due to IP laws.

you could see about building OpenCV yourself, such that it gets a full-featured ffmpeg (all the H.264 usual codecs), not just one with libopenh264. then, chances are better that it can actually read your video file.

if you can, I’d recommend using ffmpeg APIs directly, and dropping VideoCapture. ffmpeg is a C API, but it’s so much nicer than OpenCV’s VideoCapture.


I would call that a bug, and you should file it (unless it’s already filed). if you decide to do that, please prepare a video file that reproduces the issue.

OpenCV keeps a set of video files as test vectors. if any of those reproduces the problem, you don’t even have to upload anything into the issue: opencv_extra/testdata/highgui/video at 4.x · opencv/opencv_extra · GitHub

you should also make sure that the issue is reproducible on latest OpenCV release (4.13 at this time).