Python OpenCV2 cv2.cv_fourcc not working with VideoWriter

问题:

As the title states, when I run the cv2.videowriter function I get 'module' object has no attribute CV_FOURCC.

Code:

# Creates a video file from webcam stream
import cv2

Create test window
cv2.namedWindow("cam_out", cv2.CV_WINDOW_AUTOSIZE)

# Create vid cap object
vid = cv2.VideoCapture(1)


# Create video writer object
vidwrite = cv2.VideoWriter(['testvideo', cv2.CV_FOURCC('M','J','P','G'), 25, 
               (640,480),True])

回答1:

Kind of late to the party, but if anyone needs it for newer versions of opencv2, then the command is:

cv2.VideoWriter_fourcc(c1, c2, c3, c4)

From: https://www.e-learn.cn/content/wangluowenzhang/351825

回答2:

Change cv2.CV_FOURCC('M','J','P','G') to cv2.cv.CV_FOURCC('M','J','P','G').


 

回答3:

For OpenCV 3 do this:

cv2.VideoWriter_fourcc(*'MJPG')

credit @SiffgyF


 

回答4:

With OpenCV 3:

# Create video writer object
vidwrite = cv2.VideoWriter('testvideo.avi', cv2.VideoWriter_fourcc(*'XVID'), 25, 
           (640,480),True)

you can find the sample at "opencv_install_dir\sources\doc\py_tutorials\py_gui\py_video_display\py_video_display.markdown"

ps: I can't find any description with 'cv2.VideoWriter_fourcc(...)' in official documentation.


 

回答5:

From experience, if on OSX, this is what worked for me when writing out to mp4 files:

fourcc = cv2.VideoWriter_fourcc(*'MP4V')


 

回答6:

I use this code:

fourcc = cv2.CV_FOURCC(*'XVID')

猜你喜欢

转载自blog.csdn.net/tony2278/article/details/83445045