title: OpenCV: Get frame by timestamp
body: <p>I am trying to to get a specific frame from a video by a timestamp. To check if my program works right my video has the recording time in the bottom right corner. Example:</p>
  
  <p><a href="https://i.stack.imgur.com/IQQmt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IQQmt.jpg" alt="enter image description here"></a></p>
  
  <p>My problem: If I look for the frame with the timestamp 6000 I get the frame I showed abouth (Which is fine). But if I look for the frame with the Timestamp 60000 I would expect to get the Frame with <em>20:45:00</em> <strong>But I get <em>20:44:54</em></strong></p>
  
  <p>I am pretty sure that I am doing something wrong, but I can´t figure out what causes this gap of 6 seconds:</p>
  
  <p>Here is my Code:</p>
  
  <pre><code>    #include &lt;iostream&gt;
  #include "opencv2/core.hpp"
  #include "opencv2/core/mat.hpp"
  #include "opencv2/highgui.hpp"
  #include "opencv2/imgcodecs.hpp"
  #include "opencv2/calib3d/calib3d.hpp"
  
  using namespace std;
  using namespace cv;
  
  int main(const int argc, char *argv[])
  {
      int inTimestamp = atoi(argv[2]);
  
      cv::VideoCapture capture(argv[1]);
  
      if(!capture.isOpened())
      {
          //error!
          cout &lt;&lt; "Can not load video";
      }
      else
      {
          double prevtimestamp = 0;   
          Mat frame;
  
          while(1)
          {
              capture.grab();
              double timestamp = capture.get(CV_CAP_PROP_POS_MSEC);
  
              if(inTimestamp &gt;= prevtimestamp &amp;&amp; inTimestamp &lt;= timestamp )
              {
                      //imshow("Frame",frame);
                      capture &gt;&gt; frame; //new frame   
                      cout &lt;&lt; timestamp;
                      imwrite("test.jpg",frame);
                      break;
              }
  
              prevtimestamp = timestamp;
  
          }
      }
  
      return 0;
  }
  </code></pre>