Milad R
Milad R

Reputation: 1892

How to find the length of a video in OpenCV?

I want to find the length of a video capture in OpenCV;

int frameNumbers = (int) cvGetCaptureProperty(video2, CV_CAP_PROP_FRAME_COUNT);
int fps = (int) cvGetCaptureProperty(video2, CV_CAP_PROP_FPS);
int videoLength = frameNumbers / fps;

but this give me a result which is less than the real answer. What do I have to do?

Upvotes: 3

Views: 11794

Answers (2)

Kenny
Kenny

Reputation: 51

Actually, I am not sure if there is any issue with the functions that you tried as of today. However, There is an issue with this snippet. Here, it is being assumed that Frames Per Second is an integer value which is not always the case. For example, many videos are encoded at 29.97 FPS, and this code would assume int(29.97) = 29 which obviously results in a larger value in seconds for video length.

The calculation seems to work fine for me if I use floating point values (float) without truncating them.

Upvotes: 5

Sam
Sam

Reputation: 20058

See this similar post. OpenCV cannt (yet) capture correctly the number of frames

OpenCV captures only a fraction of the frames from a video file

Upvotes: 0

Related Questions