20

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])
2
  • What is the question exactly? Commented Mar 28, 2013 at 4:19
  • @KyleMaxwell The questions is ”How do I fix the error ‘'module' object has no attribute CV_FOURCC’ when I run the cv2.videowriter function?” Commented Aug 20, 2019 at 20:26

9 Answers 9

35

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)
Sign up to request clarification or add additional context in comments.

1 Comment

@GM - you need to check which API version you have, all the possibilities are present among the various answers...
17

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

3 Comments

or even cv2.cv.CV_FOURCC(*'MJPG')
cv module is gone in latest version
These days you would use cv2.VideoWriter_fourcc(*'MJPG')
12

For OpenCV 3 do this:

cv2.VideoWriter_fourcc(*'MJPG')

credit @SiffgyF

Comments

8

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.

1 Comment

3

if you want to write to Mp4 file just use this codec it's for 2020 opencv4.2 and plus

fourcc = cv2.VideoWriter_fourcc(*'X264')
video_writer = cv2.VideoWriter('outeringe.mp4', fourcc, 20, (640, 480))

use x264 instead of MP4V

Comments

2

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

fourcc = cv2.VideoWriter_fourcc(*'MP4V')

Comments

2

For those encountering a "VideoWriter_fourcc" is not a known member of module "cv2" error from pylance, it seems the method is not defined in the type stubs definition file (.pyi); There's discussion related to such on the opencv GH repo #24818. TLDR; Fix is targeted for v4.10.

Many examples shows something like:

fourcc = cv2.VideoWriter_fourcc(*"mp4v")
vwriter = cv2.VideoWriter("outvideo.mp4", fourcc, 20, (640, 480))

As mentioned, pylance will likely complain with the above, An alternative that will work without falsly trigging pylance is:

fourcc = cv2.VideoWriter.fourcc(*"mp4v")
vwriter = cv2.VideoWriter("outvideo.mp4", fourcc, 20, (640, 480))

in the source code fourcc is marked as a static method of videowriter class, and needs to be called on an instance of the class.

Note this is for v.4.9 and I don't know which version this was introduced

Comments

0

I've the same error (on macos ) in opencv-python 4.2.0.32 upgrading to opencv-python-4.4.0.46 solve it , without any need for code changes

Comments

-1

I use this code:

fourcc = cv2.CV_FOURCC(*'XVID')

2 Comments

Please describe why this answers the question and how the code you supplied should be used.
Not working. Gives: 'module' object has no attribute 'CV_FOURCC'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.