title: How to record and save a video using OpenCV and Python?
body: <p>I have taken the following code from <a href="https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html" rel="nofollow noreferrer">this website</a>.</p>
  
  <pre><code>import cv2
  
  cap = cv2.VideoCapture(0)
  
  # Define the codec and create VideoWriter object
  fourcc = cv2.VideoWriter_fourcc(*'avc1')
  out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
  
  while cap.isOpened():
      ret, frame = cap.read()
      if ret:
          out.write(frame)
          cv2.imshow('Video', 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>The problem I am facing is that the video is being stored, but I am not able to open it. The video size is about 6KB, but the duration is 0 seconds. How can I fix this?</p>
  
  <p>I did check the other questions similar to this, but none of them solve the issue I am facing.</p>