Python OpenCV2 cv2.cv_fourcc 不适用于 VideoWriter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15584608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:28:38  来源:igfitidea点击:

Python OpenCV2 cv2.cv_fourcc not working with VideoWriter

pythonopencv

提问by user2197517

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

正如标题所述,当我运行 cv2.videowriter 函数时,我得到“模块”对象没有属性 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])

回答by armadefuego

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

更改cv2.CV_FOURCC('M','J','P','G')cv2.cv.CV_FOURCC('M','J','P','G')

回答by cgf

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

聚会有点晚了,但是如果有人需要它来使用较新版本的 opencv2,那么命令是:

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

回答by JohnAllen

For OpenCV 3 do this:

对于 OpenCV 3,请执行以下操作:

cv2.VideoWriter_fourcc(*'MJPG')

cv2.VideoWriter_fourcc(*'MJPG')

credit @SiffgyF

信用@SiffgyF

回答by user3077861

I use this code:

我使用这个代码:

fourcc = cv2.CV_FOURCC(*'XVID')

回答by snailx

With OpenCV 3:

使用 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"

您可以在“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.

ps:我在官方文档中找不到任何关于 'cv2.VideoWriter_fourcc(...)' 的描述。

回答by twocrush

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

根据经验,如果在 OSX 上,这就是写 mp4 文件时对我有用的方法:

fourcc = cv2.VideoWriter_fourcc(*'MP4V')

fourcc = cv2.VideoWriter_fourcc(*'MP4V')

回答by codé Facil

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

如果您想写入 Mp4 文件,请使用此编解码器,它适用于 2020 opencv4.2 及以上

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

use x264 instead of MP4V

使用 x264 而不是 MP4V