Ask Your Question
0

How to play a video with a fixed fps?

asked 2017-02-12 10:26:55 -0600

Nuz gravatar image

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;
     }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-02-12 11:38:10 -0600

LBerger gravatar image

Similar question then similar answer...

VideoCapture vRead;
vRead.open("G:/Lib/opencv/samples/data/Megamind.avi");
if (!vRead.isOpened())
{
    cout<<"File not found";
    return ;
}
double xFps;
xFps = vRead.get(CV_CAP_PROP_FPS);
cout << "FPS = " << xFps << "\n";
double x = vRead.get(CV_CAP_PROP_FRAME_COUNT);
cout << "# frame = " << x << "\n";
int fourcc = vRead.get(CV_CAP_PROP_FOURCC);
string fourcc_str = format("%c%c%c%c", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
cout<<"Fourcc "<< fourcc_str<<endl;
cout<<"Time to play : "<< x/xFps<<" s\n";
TickMeter t;
t.start();
bool test=true;
Mat frame;
while (test)
{
    vRead>> frame;
    if (frame.empty())
        test=false;
    else
        imshow("video",frame);
    waitKey(1);
}
t.stop();
cout<<"Play video in "<<t.getTimeMilli()<<" ms\n";
vRead.set(CV_CAP_PROP_POS_FRAMES,0);
test = true;
int nbFrame=0;
t.reset();
t.start();
while (test)
{
    vRead >> frame;

    if (frame.empty())
        test = false;
    else
    {
        nbFrame++;
        imshow("video", frame);
        waitKey(1000 / xFps);
    }
}
t.stop();
cout << "Play video in " << t.getTimeMilli() << " ms\n";
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-02-12 10:26:55 -0600

Seen: 4,877 times

Last updated: Feb 12 '17