问题
I'm trying to capture video from a webcam(Logitech c210) and create a video file from it.
This is on Raspbian Wheezy 2013-05-25.
The light on the camera comes on for about 30 seconds but no file is created. I've had the webcam displaying in an OpenCV window.
I'm wondering if this is a codec problem as the script works on windows with the -1 parameter?
If so are there any recommended codecs for Raspberry Pi?
If I buy the mpeg2 codec would that work?
I've listed the codecs that I've tried though none work.
The script is as follows:
import cv2
import time
def InitialiseCamera():
camera = cv2.VideoCapture(0)
if camera is None:
print('Warning: unable to access camera')
else:
print('initialized camera')
return camera
def InitialiseWriter():
fps = 5
size = (640, 480)
destinationFile = 'video.avi'
# These are the codecs I've tried so far
codec = cv2.cv.CV_FOURCC('I','4','2','0')
#codec = cv2.cv.CV_FOURCC('A','V','C','1')
#codec = cv2.cv.CV_FOURCC('Y','U','V','1')
#codec = cv2.cv.CV_FOURCC('P','I','M','1')
#codec = cv2.cv.CV_FOURCC('M','J','P','G')
#codec = cv2.cv.CV_FOURCC('M','P','4','2')
#codec = cv2.cv.CV_FOURCC('D','I','V','3')
#codec = cv2.cv.CV_FOURCC('D','I','V','X')
#codec = cv2.cv.CV_FOURCC('U','2','6','3')
#codec = cv2.cv.CV_FOURCC('I','2','6','3')
#codec = cv2.cv.CV_FOURCC('F','L','V','1')
#codec = cv2.cv.CV_FOURCC('H','2','6','4')
#codec = cv2.cv.CV_FOURCC('A','Y','U','V')
#codec = cv2.cv.CV_FOURCC('I','U','Y','V')
#codec = -1
video = cv2.VideoWriter(destinationFile, codec, fps, size, True);
if video is None:
print('Warning: unable to create video writer')
else:
print('initialized writer')
return video
def CaptureVideo(c,w):
i = 0
while i<150:
i+=1
f,img = c.read()
try:
w.write(img)
except:
print "Unexpected error: ", sys.exec_info()[0]
print('complete')
c.release()
if __name__ == '__main__':
cam = InitialiseCamera()
writer = InitialiseWriter()
CaptureVideo(cam,writer)
回答1:
I eventually went through and tried all of the fourcc codecs and none worked.
Same with the mpeg2 codec.
I ended up using avconv to create and capture the video with this line:
import os
os.system("avconv -f video4linux2 -input_format mjpeg -i /dev/video0 output.avi")
os.system() is used to run a terminal command.
OpenCV can still be used to process the video files at a later time.
Hope this helps someone.
回答2:
If this answer isn't too late, try
CV_FOURCC('M','P','E','G')
works for me on Raspbian. Although, an fps of 5 is probably not supported.
回答3:
check the tutorial on using OpenCV with Pi.
http://thinkrpi.wordpress.com/2013/05/22/opencv-and-camera-board-csi/
来源:https://stackoverflow.com/questions/17102564/no-video-file-created-by-opencv-2-3-1-with-python-on-raspberry-pi