opencv VideoWriter under OSX producing no output

There are many outdated and incorrect online guides on this topic-- I think I tried almost every one. After looking at the source QTKit-based implementation of VideoWriter on Mac OSX, I was finally able to get VideoWriter to output valid video files using the following code:

fps = 15
capSize = (1028,720) # this is the size of my source video
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') # note the lower case
self.vout = cv2.VideoWriter()
success = self.vout.open('output.mov',fourcc,fps,capSize,True) 

To write an image frame (note that the imgFrame must be the same size as capSize above or updates will fail):

self.vout.write(imgFrame) 

When done be sure to:

vout.release() 
self.vout = None

This works for me on Mac OS X 10.8.5 (Mountain Lion): No guarantees about other platforms. I hope this snippet saves someone else hours of experimentation!


I am on macOS High Sierra 10.13.4, Python 3.6.5, OpenCV 3.4.1.

The below code (source: https://www.learnopencv.com/read-write-and-display-a-video-using-opencv-cpp-python/) opens the camera, closes the window successfully upon pressing 'q', and saves the video in .avi format.

Note that you need to run this as a .py file. If you run it in Jupyter Notebook, the window hangs when closing and you need to force quit Python to close the window.

import cv2
import numpy as np
 
# Create a VideoCapture object
cap = cv2.VideoCapture(0)
 
# Check if camera opened successfully
if not cap.isOpened(): 
  print("Unable to read camera feed")
 
# Default resolutions of the frame are obtained.The default resolutions are system dependent.
# We convert the resolutions from float to integer.
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
 
# Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))
 
while True:
  ret, frame = cap.read()
 
  if ret: 
     
    # Write the frame into the file 'output.avi'
    out.write(frame)
 
    # Display the resulting frame    
    cv2.imshow('frame',frame)
 
    # Press Q on keyboard to stop recording
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break
 
  # Break the loop
  else:
    break 
 
# When everything done, release the video capture and video write objects
cap.release()
out.release()
 
# Closes all the frames
cv2.destroyAllWindows() 

After trying various options, I found that the frame.size that I was using did not fit the size specified in the VideoWriter: So setting it to the default of my iMac 1280x720 made things work!

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
out = cv2.VideoWriter()
succes = out.open('output.mp4v',fourcc, 15.0, (1280,720),True)
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) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

Tags:

Python

Opencv