title: OpenCV Python Video playback - How to set the right delay for cv2.waitKey()
body: <p>I used the following code to capture a video file, flip it and save it.</p>
  
  <pre><code>#To save a Video File
  
  import numpy as np
  import cv2
  
  cap = cv2.VideoCapture(0)
  
  # Define the codec and create VideoWriter object
  fourcc = cv2.cv.CV_FOURCC(*'XVID')
  out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
  
  while(cap.isOpened()):
      ret, frame = cap.read()
      if ret==True:
          frame = cv2.flip(frame,0)
  
          # write the flipped frame
          out.write(frame)
  
          cv2.imshow('frame',frame)
          if cv2.waitKey(1) &amp; 0xFF == ord('q'):
              break
      else:
          break
  
  # Release everything if job is finished
  cap.release()
  out.release()
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>This program saves the output as output.avi</p>
  
  <p>Now, to playback the video file I used the following program</p>
  
  <pre><code>#Playing Video from File
  
  import numpy as np
  import cv2
  
  cap = cv2.VideoCapture('output.avi')
  
  print cap.get(5) #to display frame rate of video
  #print cap.get(cv2.cv.CV_CAP_PROP_FPS)
  
  while(cap.isOpened()): 
      ret, frame = cap.read()
  
      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #convert to grayscale
  
      cv2.imshow('frame',gray)
      if cv2.waitKey(1) &amp; 0xFF == ord('q'): 
          break
  
  cap.release()
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>This program plays the video file output.avi saved from the first program. The thing is, this video appears fast forward. So, I tried changing the delay value for cv2.waitKey(). The video looked fine when I put 100. How do I know which value to put there? Should it be related to the frame rate? I checked the frame rate of output.avi (see line cap.get(5) in second program) and got 20. But if I use 20 as delay for cv2.waitKey() the video is still too fast.</p>
  
  <p>Any help would be appreciated.</p>