1

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)

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

can you please explain your answer a bit more. Did you simply include the line: os.system("avconv -f video4linux2... output.avi") in while loop of your previous code?
Of course, sorry for the delay. Avconv provides multimedia libraries for dealing with multimedia. I mentioned os.system as it's run from the terminal. Yes basically it was in a while loop that ran for a certain length of time: while (time.time()-StartTime()) < lengthOfTime: CaptureVideo(waitTime) time.sleep(1)
1

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 Comments

I did thanks; it was about half way down the fourcc.org/codecs list.
Something I checked recently, MJPG works on my Raspbian. You might consider update and upgrade on the Linux.
Cool. Indeed I'll have a look later on this week.
0

check the tutorial on using OpenCV with Pi.

http://thinkrpi.wordpress.com/2013/05/22/opencv-and-camera-board-csi/

1 Comment

Thanks for the tutorial, unfortunately I don't have the rPi camera so I'm unable to test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.