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])
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)
Change cv2.CV_FOURCC('M','J','P','G') to cv2.cv.CV_FOURCC('M','J','P','G').
For OpenCV 3 do this:
cv2.VideoWriter_fourcc(*'MJPG')
credit #SiffgyF
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.
From experience, if on OSX, this is what worked for me when writing out to mp4 files:
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
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
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
I use this code:
fourcc = cv2.CV_FOURCC(*'XVID')
Related
In Python OpenCV 2.4.9, when instantiating a VideoWriter object with the usual instruction:
video = cv2.VideoWriter("output.avi", -1, 25, (640,480))
I get the following annoying dialog box that asks me to choose between the various options of compression modalities.
I need to iteratively create VideoWriter objects in order to construct a large video dataset and I am wondering if there is any way to set the compression modality only once and get rid of that dialog box popping up at every new VideoWriter instantiation.
I really need to automate this process, so any help would be truly appreciated
This works smoothly:
video = cv2.VideoWriter("output.avi", 1, 25, (640,480))
My system:
Python 2.7.15
OpenCV 2.4.9
The answer is in the parameter for the constructor of the VideoWriter() . When you pass -1 for the second parameter, that means you re asking for the window to pop up. If you want to choose the codec beforehand, you can do so by setting it to integer corresponding to the given codec.
So the code would look something like this:
# for OCV == 3.X.X
#fourcc = cv2.VideoWriter_fourcc('X', '2', '6', '4')
#for OCV == 2.X.X
fourcc = cv2.cv.FOURCC(*'X264')
video = cv2.VideoWriter("output.avi", fourcc, 25, (640,480))
I'm trying to create a video using opencv-python. I have to use H.264 codec for my video so I have the following lines:
import cv2
width = ...
height = ...
name = ...
fourcc = cv2.VideoWriter_fourcc(*'H264')
self.video = cv2.VideoWriter(name, fourcc, 30, (width, height))
When I run the code I get the following error:
[h264_nvenc # 0x562490d62d80] Cannot load libcuda.so.1
Could not open codec 'h264_nvenc': Unspecified error
As I see in the error, I need libcuda.so.1 library which comes with nvidia driver afaik, but my laptop has no dedicated graphic card.
Is it possible to write a video using H.264 using my laptop? If yes, please advice how. Thank you!
I'm trying to create a video from a sequence of images and display it in a browser but from some weird reason no matter what codec or file format I use I get the following error:
No video with supported format and mime type found
Here is my code:
ready_images = []
import cv2
for img in videos['Images']:
image = cv2.imread(img.fileName)
ready_images.append(image)
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
video_name = videos['Images'][0].gifLocationPath + "//" + videos['Name']
frame = cv2.imread(videos['Images'][0].fileName)
height, width, layers = frame.shape
video_name = video_name[:-4]+".mp4"
video = cv2.VideoWriter(video_name, fourcc, 20.0, (width, height))
for image in ready_images:
video.write(image)
cv2.destroyAllWindows()
video.release()
The funny thing is that in Firefox or Chrome the videos are not working but in Edge... they actually work.
I don't want to use FFMPEG and would prefer to make it work with OpenCV.
If any of you guys know what format of video (I know the web formats are webm, ogg, mp4) or codec I should use for this, please, just let me know.
Thanks.
MP4V or MPEG-4 part 2 is not supported by most browsers, you may want to try H.264 (MPEG-4 part 10) instead.
To do that, change:
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
to
fourcc = cv2.VideoWriter_fourcc(*'H264')
If you are using Python 3, use the following hexadecimal code instead (there seems to be a bug when using the four bytes notation):
fourcc = 0x00000021
Run the script and you will likely get the following error message:
Failed to load OpenH264 library: openh264-1.6.0-win32msvc.dll
Please check environment and/or download library: https://github.com/cisco/openh264/releases
You need to do as the message says and download the required library from github and place it somewhere accessible by your PATH.
Using H.264 compression you will also get a smaller file which is better for Web.
I know the question is old but for everyone that is looking for a compatible codec + container for web browser :
VP8 or VP80 is a compatible encoder
cv2.VideoWriter_fourcc('V','P','8','0')
I used it with .webM as a container.
Native WebM support by Mozilla Firefox,[7][8] Opera,[9][10] and Google Chrome[11] was announced at the 2010 Google I/O conference
https://en.wikipedia.org/wiki/WebM
it worked like a charm and with pretty good performance even though
for some reason I got this error when creating videoWriter objects :
OpenCV: FFMPEG: tag 0x30385056/'VP80' is not supported with codec id 139 and format 'webm / WebM'
Get .webm suffix audio file.
fourcc = cv2.VideoWriter_fourcc(*'vp80')
video_writer = cv2.VideoWriter('file.webm', fourcc, 20, (640, 480))
In html:
<body>
<video width="320" height="240" controls>
<source src="file.webm" type="video/webm">
</video>
</body>
It works on centos7 and Windows10.
I print the all available mp4 codecs by fourcc=-1.
After that I check codecs which are useful for me. I see there avc1.
So I write the code like:
fourcc = cv2.VideoWriter_fourcc(*'avc1')
When print the codes, you also see they are lowercase.
I'm trying to process frames from a video stream, and it as a new video.
This is what I'm doing :
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('Videos/output.mp4',fourcc, fps, (1080,1080))
I keep getting :
OpenCV: FFMPEG: tag 0x44495658/'XVID' is not supported with codec id 13 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000020/' ???'
I think I'm using the wrong fourcc value... Which one should I use ? I've been trying a lot of them.
I'm using Ubuntu 16.04, Python 2.7.11 and OpenCV 3.1.0
Define the codec and create VideoWriter object like this
fourcc = cv2.VideoWriter_fourcc(*'MPEG')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
For Windows users
I am using OpenCV 2 with Python 3.6, on Windows 10.
The 'XVID' codec, along with producing a .avi file, seems to be the most generic solution (if not the only one that works).
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('test.avi', fourcc, 60, (320, 240))
In addition, only BGR can be directly written with such a VideoWriter declaration. Don't try to write gray frames: the output would be empty.
The problem that you are having is that you are trying to export the frames in XVID format but the name of your output file is ending with .mp4. You should change the export format to MP4V or the name of your output file to .avi.
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter('Videos/output.mp4',fourcc, fps, (1080,1080))
alternative
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('Videos/output.avi',fourcc, fps, (1080,1080))
here you can find more information about the video codecs
The size of the frame(width,height) you are giving, should match the size of the frame you want to save.
so
fw = int(cap.get(3))
fh = int(cap.get(4))
print("w,h",fw,fh)
out = cv2.VideoWriter('fb1.avi',cv2.VideoWriter_fourcc('X','V','I','D'), fps, (fw,fh))
If you want to save video by opencv in mp4 format, let
Follow this link:
you should replace:
fourcc = cv2.VideoWriter_fourcc(*'XVID')
by:
fourcc = cv2.VideoWriter_fourcc(*'FMP4')
I tried and succeeded.
I had the same problem. With me, it turned out that I had switched the height and width of the video, so the frame dimensions did not match the video specifications and as a result nothing was written. Make sure they match exactly.
Also, OpenCV seems to give that same warning if the file extension does not match the codec used. Specifically, it wants .avi for the XVID codec.
I wanted to save as a .mp4, and using *"mp4v" turned out to be the right code, on Linux at least.
Check the example below
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
video = cv2.VideoWriter('output.mp4', fourcc, fps, (width, height))
Install K-Lite Mega Codec Pack: https://filehippo.com/download_klite_mega_codec/
This error occurs because some codecs are not available by default in Windows media player. So by installing this software, the video works fine with same code.
If you are using linux try this
fourcc = 0x00000021
out = cv2.VideoWriter('Videos/output.mp4',fourcc, fps, (1080,1080))
In my case, i use:
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
video = cv2.VideoWriter(video_name, fourcc, FPS, (width,height))
and this work :>
On a mac
writer = cv2.VideoWriter('mysupervideo.mp4', cv2.VideoWriter.fourcc(*'mp4v'), 20, (width, height))
Works better
I'm new in Python (2.7) and I try to work on video processing (with module openCv "cv2"). Starting with tutorials, I try to use the script of this tutorial : paragraph "Saving a video".
Everything works fine excepting that the video I'm saving is empty. I can find output.avi in my directory but its memory size is 0kb an, of course when I run it, no video is displayed.
After a few changes here is my code :
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
# 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()
Does anyone know why it is not working properly ?
Thanks a lot.
Edwin
I never worked with openCV, but I bet the problem is in
cap = cv2.VideoCapture(0)
This is a C version of the VideoCapture method http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture
Maybe you can try to do the same. Something like
cap = cv2.VideoCapture(0)
if (not cap.isOpened()):
print "Error"
EDIT: just downloaded Python and OpenCV and discovered the problem was the codec. Try to change
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
for
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))
and select the codec by hand.
Could be the output resolution is different from the input. Check width and height of cap.
size = (int(cap.get(3)), int(cap.get(4)))
Change either your camera or the output resolution.
I have Windows 10, Python3.7.6, OpenCV 4.2.0. In my case, the problem is video encoder. Both "XVID" and "X264" result in an empty output video. I changed the encoder to "DIVX" and the video is generated successfully.