First time here? Check out the FAQ!

Ask Your Question
0

Saving video from webam stream - no file created

asked Oct 18 '14

Kepano gravatar image

updated Oct 20 '14

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 = cv2.cv.CV_FOURCC('X','V','I','D')

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

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(10) == 27:
            break
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

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.

Preview: (hide)

Comments

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

berak gravatar imageberak (Oct 20 '14)edit

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

Kepano gravatar imageKepano (Oct 20 '14)edit

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

berak gravatar imageberak (Oct 20 '14)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 (Oct 20 '14)edit

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

StevenPuttemans gravatar imageStevenPuttemans (Oct 20 '14)edit

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

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

Kepano gravatar imageKepano (Oct 20 '14)edit
1

cv2.VideoWriter_fourcc is for opencv3.0. with opencv2.4, use cv2.cv.CV_FOURCC()

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

berak gravatar imageberak (Oct 20 '14)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: http://robertcastle.com/2014/02/installing-opencv-on-a-raspberry-pi/

I don't think I chould miss anything.

Kepano gravatar imageKepano (Oct 20 '14)edit

1 answer

Sort by » oldest newest most voted
1

answered Jan 1 '15

aydin gravatar image

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

Preview: (hide)

Comments

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',cv2.cv.CV_FOURCC('M','J','P','G'), 20.0, (640,480))
berak gravatar imageberak (Jan 1 '15)edit

Thanks a lot (Y)

tes gravatar imagetes (Jan 29 '16)edit

Question Tools

Stats

Asked: Oct 18 '14

Seen: 8,617 times

Last updated: Oct 20 '14