Ask Your Question

Revision history [back]

Okay first of all the cap interface isn't the best at retrieving properties. Many of it underlying interfaces for camera capture don't support more than half of the get and set parameters. Getting the total amount of frames will be 100% correctly if you just calculated it manually. If this suits your needs and time is not an issue, then simply go for that approach.

A quick code sample

VideoCapture interface("location_to_file"); int counter = 0; Mat frame; while(cap >> frame){ if(frame.empty()){ break; } counter++ }

Okay first of all the cap interface isn't the best at retrieving properties. Many of it underlying interfaces for camera capture don't support more than half of the get and set parameters. Getting the total amount of frames will be 100% correctly if you just calculated it manually. If this suits your needs and time is not an issue, then simply go for that approach.

A quick code sample

VideoCapture interface("location_to_file");
int counter = 0;
Mat frame;
while(cap >> frame){
if(frame.empty()){
break;
}
counter++
}

}

Okay first of all the cap interface isn't the best at retrieving properties. Many of it underlying interfaces for camera capture don't support more than half of the get and set parameters. Getting the total amount of frames will be 100% correctly if you just calculated it manually. If this suits your needs and time is not an issue, then simply go for that approach.

A quick code sample

VideoCapture interface("location_to_file");
int counter = 0;
Mat frame;
while(cap >> frame){
   if(frame.empty()){
      break;
   }
   counter++
}

Update:

Since the introduction of this solution, there was a new videocapture property introduced, called CAP_PROP_FRAME_COUNT, which can be retrieved by invoking the .get() function on the VideoCapture element.