Ask Your Question

Extract all video frame from mp4 video

asked 2018-06-19 10:47:13 -0500

raisa_ gravatar image

Hi, I'm following a tutorial to extract video frames. I've read this question , but the solution is for capturing current frame. I have a 120fps video and want to extract all of them. Here's my code :

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
#include <sstream>

using namespace cv;
using namespace std;
int c = 0;
string int2str(int &);

int main(int argc, char **argv) {
string s;

VideoCapture cap("test.mp4"); // open the video file for reading
if (!cap.isOpened())  
    cout << "Cannot open the video file" << endl;
    return -1;

double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video

cout << "Frame per seconds : " << fps << endl;

namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

while (1)
    Mat frame;
    Mat Gray_frame;
    bool bSuccess =; // read a new frame from video

    if (!bSuccess) 
        cout << "Cannot read the frame from video file" << endl;
    s = int2str(c);
    //cout<<("%d\n",frame )<<endl;
    imshow("MyVideo", frame); //show the frame in "MyVideo" window
    imwrite("ig" + s + ".jpg", frame);
    if (waitKey(30) == 27) //esc key
        cout << "esc key is pressed by user" << endl;

return 0;


//int to string function
string int2str(int &i) {
    string s;
    stringstream ss(s);
    ss << i;
    return ss.str();

My problem is, I have a minute video at 120fps. I expected to be able to extract 120 frames. But my frame is extracted as long as my program running. So, I reach over than 200 frames extracted. Did I do something wrong in my code ?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-06-19 20:41:55 -0500

Tetragramm gravatar image

A 1 minute video at 120 FPS is 7200 frames.

120 frames/second * 60 seconds / minute

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-06-19 10:47:13 -0500

Seen: 140 times

Last updated: Jun 19 '18