title: Can't save a video in opencv
body: <p>I am trying to save my video using opencv write method but the video is saved with 0 kb. what's wrong in my code.</p>
  <pre class="lang-py prettyprint-override"><code>import cv2
  
  cap = cv2.VideoCapture(&quot;k1.mp4&quot;)
  # assert cap.isOpened()
  fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
  fourcc = cv2.VideoWriter_fourcc(*'MP42')
  out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640,480))
  # assert out.isOpened()
  
  while cap.isOpened():
      ret, frame = cap.read()
      # if not ret: break
  
      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      fgmask = fgbg.apply(gray)
      thresh = 2
      maxValue = 255
      ret, th1 = cv2.threshold(fgmask, thresh, maxValue, cv2.THRESH_BINARY)
  
      color_space = cv2.applyColorMap(th1, cv2.COLORMAP_JET)
      result_vid = cv2.addWeighted(frame, 0.7, color_space, 0.7, 0)
  
      out.write(result_vid)
  
      cv2.imshow(&quot;vid&quot;, result_vid)
      if cv2.waitKey(20) == ord('q'):
          break
  
  cap.release()
  out.release()
  cv2.destroyAllWindows()
  </code></pre>