title: Extract all video frame from mp4 video using OpenCV and C++
body: <p>I'm following a tutorial to extract video frames. I've read <a href="https://stackoverflow.com/questions/48147178/cant-extract-frames-from-video-using-c-with-opencv">this question</a>, it doesn't work, also queationfrom <a href="http://answers.opencv.org/question/6241/how-to-extract-frames-from-avi-video/" rel="noreferrer">Open CV Answer</a>, but the solution is for capturing current frame. I have a 120fps video and want to extract all of them. Here's my code </p>
  
  <pre><code>#include &lt;opencv2/core/core.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  
  using namespace cv;
  using namespace std;
  int c = 0;
  string int2str(int &amp;);
  
  int main(int argc, char **argv) {
  string s;
  
  VideoCapture cap("test.mp4"); // video
  if (!cap.isOpened())  
  {
      cout &lt;&lt; "Cannot open the video file" &lt;&lt; endl;
      return -1;
  }
  
  double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
  
  cout &lt;&lt; "Frame per seconds : " &lt;&lt; fps &lt;&lt; endl;
  
  namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
  
  while (1)
  {
      Mat frame;
      Mat Gray_frame;
      bool bSuccess = cap.read(frame); // read a new frame from video
  
      if (!bSuccess) 
      {
          cout &lt;&lt; "Cannot read the frame from video file" &lt;&lt; endl;
          break;
      }
      s = int2str(c);
      //cout&lt;&lt;("%d\n",frame )&lt;&lt;endl;
      c++;
      imshow("MyVideo", frame); //show the frame in "MyVideo" window
      imwrite("ig" + s + ".jpg", frame);
      if (waitKey(30) == 27) //esc key
      {
          cout &lt;&lt; "esc key is pressed by user" &lt;&lt; endl;
          break;
      }
  }
  
  return 0;
  
  }
  
  //int to string function
  string int2str(int &amp;i) {
      string s;
      stringstream ss(s);
      ss &lt;&lt; i;
      return ss.str();
  }
  </code></pre>
  
  <p>My problem is, I have a minute video at 120fps. I expected to be able to extract 120 frames. But the extraction went wrong, the video last for 1 minute but I got more than 200 frames stored in my folder.  Did I do something wrong in my code ?</p>