Ask Your Question

Saving video from webam stream - no file created

asked 2014-10-18 09:27:09 -0500

Kepano gravatar image

updated 2014-10-20 04:51:59 -0500

I have followed the tutorial from this site about saving video streams to a file: Link to tutorial

The code starts streaming from the webcam, but when I press 'Esc' no avi-file is created

My code:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

#fourcc = cv2.VideoWriter_fourcc(*'XVID') <-- this line results in an error 
fourcc ='X','V','I','D')

out = cv2.VideoWriter('/home/pi/video/output.avi',fourcc, 20.0, (640,480))

    ret, frame =
    if ret==True:
        frame = cv2.flip(frame,0)


        if cv2.waitKey(10) == 27:


I am using python 2.7 and OpenCV-2.4.10 on my raspberry Pi.

Also, I have tried to use 2 or 3 different codes on blogs used for the same purspose, and none of them saves a video file, eventhough the rest of the code works well.

Do I need to install some packages,or how can I solve this issue?

Best regards

edit: added / to the file-path.

edit retag flag offensive close merge delete


shouldn't that be: /home/pi/video/output.avi ?

berak gravatar imageberak ( 2014-10-20 01:15:55 -0500 )edit

I tried removing the 2 ' -signs, but that only results in a syntax error. The tutoial use them too.

Kepano gravatar imageKepano ( 2014-10-20 04:02:15 -0500 )edit

sorry, no, meant: additional slash in the front: ' /home/pi/video/output.avi'

berak gravatar imageberak ( 2014-10-20 04:14:27 -0500 )edit

That is a mistake in this post. The slash is in the code (I will edit the OP post to the correct path).

Editing the path to be 'output.avi' does solve the problem either, so it is not the path that is wrong.

Kepano gravatar imageKepano ( 2014-10-20 04:51:10 -0500 )edit

Are you sure that fourcc ='X','V','I','D') is supported on your system? Might want to try out another fourcc code!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-10-20 07:47:31 -0500 )edit

Using the code: fourcc = cv2.VideoWriter_fourcc(*'XVID') gives me the following error:

Traceback (most recent call last): File "", line 7, in <module> fourcc = cv2.VideoWriter_fourcc(*'XVID') AttributeError: 'module' object has no attribute 'VideoWriter_fourcc'

Kepano gravatar imageKepano ( 2014-10-20 08:09:54 -0500 )edit

cv2.VideoWriter_fourcc is for opencv3.0. with opencv2.4, use

but try 'M','J','P','G' or some other combination for the codec

berak gravatar imageberak ( 2014-10-20 08:18:50 -0500 )edit

Now I have tried 'M','J','P','G' and 'M','P','E','G' and 'X','2','6','4' and none works. I found on a different forum that mjpg and mpeg should work on raspbian. I have used this guide to build opencv:

I don't think I chould miss anything.

Kepano gravatar imageKepano ( 2014-10-20 09:04:20 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-01-01 13:02:47 -0500

aydin gravatar image

out = cv2.VideoWriter('output.avi','M','J','P','G'), 20.0, (640,480)) Only use this work...

edit flag offensive delete link more


let's make it a bit clearer:

3.0 version:

out = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc(*'XVID'), 20.0, (640,480))

2.4 version:

out = cv2.VideoWriter('output.avi','M','J','P','G'), 20.0, (640,480))
berak gravatar imageberak ( 2015-01-01 14:18:56 -0500 )edit

Thanks a lot (Y)

tes gravatar imagetes ( 2016-01-28 18:09:42 -0500 )edit

Question Tools


Asked: 2014-10-18 09:27:09 -0500

Seen: 7,185 times

Last updated: Oct 20 '14