Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to play a video with a fixed fps?

I am extracting frames from video using the following code. However, i want a fixed fps(fps=10) for every videos used to extract frames, but it is not giving me the same number of frames for each video, as they are of different time. How can I play every videos with a fixed fps? please help.. I want to extract frames at 10 fps.

#include "opencv2/opencv.hpp"
#include <time.h>
#include <stdio.h>
#include<string>
#include<ctime>

using namespace cv;
 using namespace std;


     const char* videofilename = "C:/Users/Desktop/Movements/Shooting.mp4";
 VideoCapture cap(videofilename); // open a video file
int main(int argc, char** argv)
 {

if (!cap.isOpened())  // check if succeeded
{
    cout << "file " << videofilename << " not found or could not be opened" << endl;
    return -1;
}

namedWindow("output");

//unsigned long counter = 0;


double fps = cap.set(CV_CAP_PROP_FPS, 10);//set the fps of video

//double fps = cap.get(CV_CAP_PROP_FPS);
//cout << "Frame per seconds : " << fps << endl;
Mat frame;

int counter = 0;
    // read frames until end of video:
    while (cap.read(frame))
    {

        if (counter % 2 == 0) {

            cap >> frame;
        }
        else
        {
            counter++;
            continue;
        }

        // display frame
    imshow("output", frame);



                   // adjust the filename by incrementing a counter
    std::stringstream filename(std::stringstream::in | std::stringstream::out);
    filename << "image" << counter++ << ".jpg";

    std::cout << "writing " << filename.str().c_str() << " to disk" << std::endl;

    // save frame to file: image0.jpg, image1.jpg, and so on...
    imwrite(filename.str().c_str(), frame);

     }
    waitKey(0);

return 0;
     }