Saving video from webam stream - no file created
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.
shouldn't that be: /home/pi/video/output.avi ?
I tried removing the 2 ' -signs, but that only results in a syntax error. The tutoial use them too.
sorry, no, meant: additional slash in the front: ' /home/pi/video/output.avi'
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.
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!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'
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
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.