Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't write and save a video file using OpenCV and Python

Tags:

python

opencv

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

like image 659
lilian Avatar asked Jul 15 '16 13:07

lilian


People also ask

How do I save a file in OpenCV?

When working with OpenCV Python, images are stored in numpy ndarray. To save an image to the local file system, use cv2. imwrite() function of opencv python library.


2 Answers

Define the codec and create VideoWriter object like this

fourcc = cv2.VideoWriter_fourcc(*'MPEG') out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480)) 
like image 67
Amar Avatar answered Sep 24 '22 16:09

Amar


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.

like image 23
Right leg Avatar answered Sep 23 '22 16:09

Right leg