Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV VideoCapture says video has 0 frames (C++ and Python)

Tags:

c++

python

opencv

I'm using OpenCV 2.4.9 and trying to open a video. isOpened() successfully runs, but when I try to look at the number of frames the video has, it returns 0, and I can't access the frames of the video. This happens with every video I have. I can use the same videos on a different machine (the issue happens in my CentOS 7 VM, the videos are in a shared folder and the host OS can access them fine in OpenCV). Ffmpeg is installed and I can ffplay the videos and ffmpeg -i tells me the videos have a nonzero number of frames. My OpenCV was compiled with ffmpeg successfully:

"  Video I/O:\n"
"    DC1394 1.x:                  NO\n"
"    DC1394 2.x:                  YES (ver 2.2.2)\n"
"    FFMPEG:                      YES\n"
"      codec:                     YES (ver 56.26.100)\n"
"      format:                    YES (ver 56.25.101)\n"
"      util:                      YES (ver 54.20.100)\n"
"      swscale:                   YES (ver 3.1.101)\n"
"      gentoo-style:              YES\n"
"    GStreamer:                   \n"
"      base:                      YES (ver 0.10.36)\n"
"      app:                       YES (ver 0.10.36)\n"
"      video:                     YES (ver 0.10.36)\n"
"    OpenNI:                      NO\n"
"    OpenNI PrimeSensor Modules:  NO\n"
"    PvAPI:                       NO\n"
"    GigEVisionSDK:               NO\n"
"    UniCap:                      NO\n"
"    UniCap ucil:                 NO\n"
"    V4L/V4L2:                    Using libv4l (ver 0.9.5)\n"
"    XIMEA:                       NO\n"
"    Xine:                        NO\n"
"\n"

My code correctly compiles and I can read images with OpenCV.

The code is incredibly basic. For Python:

import cv2
cap = cv2.VideoCapture('test.mp4')
print cap.isOpened()
print cap.get(cv2.cv.CV_CAP_PROP_FPS)
print cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
>> True
>> 0.0
>> 0

And for C++:

#include "opencv2/opencv.hpp"
#include <stdio.h>
#include <stdlib.h>

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
    cv::VideoCapture cap;
    cap.open("test.mp4");
    if(cap.isOpened())
    {
        cout >> cap.get(CV_CAP_PROP_FPS) >> endl;
        cout >> cap.get(CV_CAP_PROP_FRAME_COUNT) >> endl;
    }
    return(0);
}

Which prints 0 as well.

like image 980
Hal T Avatar asked Feb 04 '26 02:02

Hal T


1 Answers

This looks similar to the codec issues I had, and described in this stack overflow post. In short: I used ffmpeg to convert the video:

ffmpeg -i input.avi -c:v libx264 -vf format=yuv420p output.mp4

like image 95
barceloco Avatar answered Feb 06 '26 14:02

barceloco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!