0

I am trying to grab frames from a webcam and then writing them in a video. It works and the video shows something but it is useless.

Here you can see an example of the videos I get:

The kind of video I get

THe script is quite big so I will try to pick the relevant pieces for this problem:

import cv
capture = cv.CaptureFromCAM(1) # from webcam
frame  = cv.QueryFrame(capture)
newvideo = 'Videos/%d_%d_%d_%d_%d_%d.avi' % (localtime()[0],localtime()[1],localtime()[2],localtime()[3],localtime()[4],localtime()[5])
video = cv.CreateVideoWriter(newvideo, cv.CV_FOURCC('D','I','V','X'), 30, cv.GetSize(frame), 1)

while(1):
frame  = cv.QueryFrame(capture)
cv.WriteFrame(video, frame)
key = cv.WaitKey( int((1/30.)*1000)+1 )
Dr Sokoban
  • 1,638
  • 4
  • 20
  • 34

1 Answers1

1

Tip: start coding defensively and check the return of the calls you make. For instance:

video = cv.CreateVideoWriter(newvideo, cv.CV_FOURCC('D','I','V','X'), 30, cv.GetSize(frame), 1)    
if not video :
    print "Error in creating video writer"
    sys.exit(1)

This might be a codec related problem, so try to create your video with other codecs:

video = cv.CreateVideoWriter(newvideo, cv.CV_FOURCC('F','L','V','1'), 30, cv.GetSize(frame), 1)  

It might be a good idea to update the ones you've installed.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • I tried all the comments and got the same kind of output. It doesn't return an error. I updated my codecs using "perian pack" or even the official divx pack, and the same output. So I guess I am doing something wrong someway. – Dr Sokoban May 11 '12 at 10:26