Ask Your Question
0

Problems with the video writer in OpenCV-3.0.0

asked 2015-07-17 09:11:08 -0600

desri gravatar image

Hi,

I asked this question last week about the video writer in OpenCV-3.0.0 with Python 2.7 not working properly, but didn't get any answers. I am surprised nobody else is facing similar problems.

I am using Opencv-3.0.0 through Python 2.7. I am not able to create video from image arrays, using the cv2.VideoWriter(). The process of inserting and encoding image frames into the video stream does not yield any errors, but the resulting video is only 7 KB in size and when opening through VLC, I get the error that "could not demultiplex stream."

**The following are my commands:

fourcc = cv2.VideoWriter_fourcc('M','J','P','G') video_out = cv2.VideoWriter('Motion_correction.avi', fourcc, 60, (640, 480))**

When I use video_out.write(frame_array), nothing gets written in the buffer. I really need help in this regard. The documentation is not helpful at all.

edit retag flag offensive close merge delete

5 answers

Sort by ยป oldest newest most voted
0

answered 2015-07-17 11:37:40 -0600

unxnut gravatar image

Try changing fourcc to -1. Then, it will give you a choice of video codecs to use for writing. I have seen that uncompressed video choice works fine for most of the platforms.

edit flag offensive delete link more
0

answered 2015-07-17 12:10:02 -0600

wunjo gravatar image

These could be the potential problems:

  1. As mentioned previously, you can try different codecs and see which one works best for you. However, in my experience, all of the native codecs on your system will usually result in you having massive file sizes. Think on the scale of a Gigabyte for a couple of seconds of uncompressed video. So my solution to this is to go to: FourCC Codecs and scoll to the bottom and select the 'X264' codec and download the "FFDShow MPEG-4 Video Decoder". This codec will then be called in your code by: fourCC = cv2.VideoWriter_fourcc('X','V','I','D'). Or you can use "-1" in place of the codec flag and you can select one from the list. Using this codec results in high-quality, small size .avi files which work great in OpenCV and on most computers.
  2. This potential problem is so ridiculous because I don't see it being mentioned anywhere in the documentation. It is also go for reading in files as well. Take a look at the file path for the video you are either reading or writing to in your code. You need to change the "\" to "/" for OpenCV to read the file properly. Example: 'C:/Project/Videos/myVideo.avi'. So your code would look something like: out = cv2.VideoWriter(C:/Project/Videos/myVideo.avi', fourCC, 30, (960,720))

Hope this helps!

edit flag offensive delete link more

Comments

Hi,

Thanks for your reply. I am working from Ubuntu 14.04, the "FFDShow MPEG-4 Video Decoder" seems to be an .EXE file. How do I obtain a Linux version for the decoder. Btw, the I use the slashes ("/"). correctly. When I put fourcc = -1, I get the following error: " cv2.error: /home/s0r2637/Downloads/opencv-3.0.0-beta/modules/videoio/src/cap_gstreamer.cpp:1304: error: (-210) Gstreamer Opencv backend does not support this codec. in function CvVideoWriter_GStreamer::open"

desri gravatar imagedesri ( 2015-07-17 14:20:55 -0600 )edit

Hi, I am getting same error.have u solved this ???

Heena gravatar imageHeena ( 2019-08-27 04:08:12 -0600 )edit
0

answered 2017-01-30 13:04:01 -0600

out = cv2.VideoWriter('output.avi',fourcc, 20.0,(1280,720)) Make sure you specify the right size for the video. Videowriter files wont open unless the size is right.

edit flag offensive delete link more
0

answered 2018-03-26 13:19:50 -0600

I hope this helps.

videoFilePath = "/to/your/video.avi"

videoFile =cv2.VideoCapture(videoFilePath)

frame_width = int( videoFile.get(cv2.CAP_PROP_FRAME_WIDTH))

frame_height =int( videoFile.get( cv2.CAP_PROP_FRAME_HEIGHT))

fourcc = cv2.VideoWriter_fourcc(*'XVID')

out = cv2.VideoWriter('output.avi', fourcc, 1, (frame_height, frame_width))

edit flag offensive delete link more
0

answered 2018-06-01 11:28:21 -0600

I don't know if anyone out there is still having problems with this, but here are the problems I ran into and how I solved them: in opencv 3: 'cv2.VideoWriter(filename, fourcc,fps,framesize,apiPreference)'

  1. Size must be correct for framesize argument. This argument takes a tuple (x,y). X should be the width of your image, and Y the height. I recommend pulling this from the image you're working with. However, if you're dealing with a numpy array, you want to reverse the output (if you're using np.shape to get the size, for example), so the columns are x and rows are y. If the size is wrong, you will get an empty video (denoted by a fixed size file regardless of recording time/number of frames, and errors when attempting playback).

  2. The image must be in BGR format or it will not write. To do this, take your image and convert with opencv's color convertor: cv2.cvtColor(imgname,cv2.COLOR_GRAY2BGR) or cv2.cvtColor(imgname,cv2.COLOR_RGB2BGR) There are other conversion types out there. It won't make a video if it's not BGR format, though.

  3. Getting an apiPreference error (typeError)? Try setting the apiPreference to 0 (cv2.CAP_ANY) or 1900 (cv2.CAP_FFMPEG). There's a list of settings out there somewhere, I tried to find it to link it but haven't come across it again, my bad! Alternatively, you can try changing the codec for the video (fourcc). I found success with MPEG (entered as:

opencv3:cv2.VideoWriter_fourcc('M','P',E','G') older versions: cv2.cv.CV_FOURCC('M','P,'E','G')

You can also try MJPG, XVID, and H264 if you're on Linux, like me. You have to write it in that same XXXX format as demonstrated above.

Lastly, as far as I can tell, this thing only spits out avi. A huge bummer, especially if you're shooting for lossless videos, but hey, it's better than no video at all? Eh? ::Kanye shrug::

Hope this helps.

edit flag offensive delete link more

Comments

... However, if you're dealing with a numpy array, you want to reverse the output (if you're using np.shape to get the size, for example)... This is the problem I was facing for the last few hours. Because in the opencv documentation size of a Mat object is similar to size of a numpy array, I thought that this frameSize should also be (height, width). This was not the case, however. Thanks.

alpersunter gravatar imagealpersunter ( 2019-04-27 05:23:19 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2015-07-17 09:11:08 -0600

Seen: 64,329 times

Last updated: Jul 17 '15