title: How to process images of a video, frame by frame, in video streaming using OpenCV and Python
body: <p>I am a beginner in OpenCV. I want to do some image processing on the frames of a video which is being uploaded to my server. I just want to read the available frames and write them in a directory. Then, wait for the other part of the video to be uploaded and write the frames to the directory. And , I should wait for each frame to be completely uploaded then write it to a file.</p>
  
  <p>Can you tell me how can I do it with OpenCV (Python)?</p>
  
  <p><strong>Edit 1:</strong>
  I wrote this code for capturing the video from a file, while new data are being appended at the end of the file. In other words, the <code>out.mp4</code> file is not a complete video and another program is writing new frames on it. What I'm going to do is, wait for the other program to write new frames then read them and display them.</p>
  
  <p>Here is my code:</p>
  
  <pre class="lang-py prettyprint-override"><code>import cv2
  cap = cv2.VideoCapture("./out.mp4")
  
  while True:
      if cap.grab():
          flag, frame = cap.retrieve()
          if not flag:
              continue
          else:
              cv2.imshow('video', frame)
      if cv2.waitKey(10) == 27:
          break
  </code></pre>
  
  <p>So the problem is the <code>cap.grab()</code> call! When there is no frame, it will return <code>False</code>! And it won't capture frames anymore, even if I wait for a long time.</p>