Duration of a video (OpenCV)

I want to get the fps of a video but I can't with the property CV_CAP_PROP_FPS. I had thought that having the number of frames of the video and the duration I could get it. The problem is that I don't find some method that give the duration. Anyone can help me?

Thanks!

Upvotes: 0

Views: 2210

Answers (1)

cyriel
cyriel

Reputation: 3522

First of all, grab at least one frame from camera/video before trying to get/set some property. Sometimes it may work fine without doing it, but better just do it.
If it won't solve your problem you may try to use different properties:

cv::VideoCapture camera("some_movie.avi");
cv::Mat img;
camera >> img;
std::cout << camera.get(CV_CAP_PROP_FPS) << std::endl;
std::cout << camera.get(CV_CAP_PROP_FRAME_COUNT) << std::endl;
std::cout << camera.get(CV_CAP_PROP_POS_AVI_RATIO) << std::endl;
std::cout << camera.get(CV_CAP_PROP_POS_FRAMES) << std::endl;
std::cout << camera.get(CV_CAP_PROP_POS_MSEC) << std::endl;
std::cout << "setting pos avi ratio to 1" << std::endl;
std::cout << camera.set(CV_CAP_PROP_POS_AVI_RATIO, 1.0) << std::endl;
std::cout << camera.get(CV_CAP_PROP_POS_AVI_RATIO) << std::endl; //i think it's not working - returns bad position(same as the first call of camera.get(CV_CAP_PROP_POS_AVI_RATIO)
std::cout << camera.get(CV_CAP_PROP_POS_FRAMES) << std::endl;
std::cout << camera.get(CV_CAP_PROP_POS_MSEC) << std::endl;

The results:

30
1739
0.0333333
1
33.3333
setting pos avi ratio to 1
1
0.0333333
1739
57966.7

Using total time (time of last frame) and number of frames you can calculate fps on your own. Alternatively you can use position of first frame - just divide 1.0 by position of first frame and you will get fps.

Upvotes: 2

Related Questions