18

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)
1
  • @GM - you need to check which API version you have, all the possibilities are present among the various answers... Commented Jan 16, 2018 at 21:25
17

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

3
  • 2
    or even cv2.cv.CV_FOURCC(*'MJPG')
    – ntg
    Commented Jan 10, 2015 at 20:21
  • 3
    cv module is gone in latest version
    – hoju
    Commented Jun 14, 2015 at 8:48
  • 20
    These days you would use cv2.VideoWriter_fourcc(*'MJPG')
    – SiggyF
    Commented Mar 25, 2016 at 11:46
12

For OpenCV 3 do this:

cv2.VideoWriter_fourcc(*'MJPG')

credit @SiffgyF

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
2

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

fourcc = cv2.VideoWriter_fourcc(*'MP4V')

1

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

1

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

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

-1

I use this code:

fourcc = cv2.CV_FOURCC(*'XVID')
2
  • 1
    Please describe why this answers the question and how the code you supplied should be used.
    – Andy Lamb
    Commented Dec 20, 2016 at 12:46
  • Not working. Gives: 'module' object has no attribute 'CV_FOURCC' Commented Apr 17, 2018 at 13:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.