How to count the total number of frames in OpenCV using C++?

We will learn how to calculate the total number of frames in OpenCV. Using OpenCV, it is elementary to count and show the total number of frames of a video. However, you have to store one thing in mind that we cannot count the total number of real-time video frames. Because a real-time video does not have a specific number of frames.

The following program counts the number of total frames and shows it in the console window.


using namespace std;
using namespace cv;
int main() {
   int frame_Number;//Declaring an integervariable to store the number of total frames//
   VideoCapture cap("video.mp4");//Declaring an object to capture stream of frames from default camera//
   frame_Number = cap.get(CAP_PROP_FRAME_COUNT);//Getting the total number of frames//
   cout << "Total Number of frames are:" << frame_Number << endl;//Showing the number in console window//
   system("pause");//Pausing the system to see the result//
   cap.release();//Releasing the buffer memory//
   return 0;

As the output, we will get an integer value.