Google+ My Python Projects: Save Video from WebCam to disk using VideoWriter() Google+

Wednesday, July 17, 2013

Save Video from WebCam to disk using VideoWriter()

download as text from_camsave.py.txt

The VideoWriter() append multiple frames to create a single video file.

FourCC stands for Four Character Code and,represent video/audio format(Here used for video).The VideoWriter() has 5 arguments
  1. Filename of video to be saved
  2. The CV_FOURCC object
  3. Frame rate of video
  4. Aspect Ratio
  5. Flag to represent whether video should be saved in color or not
The FourCC consists of 4 characters which represent the video format find more formats at Video Codecs by FOURCC - fourcc.org.(Many of them won't work with your camera)

Maximum frame rate that can be achieved depends on your camera.Any rate higher than parameter of the camera won't matter.

 Aspect Ratio is the dimensions(width and height) of video/image usually represented in pixels.Which should be a tuple here.The aspect ratio should controlled explicitly using cv2.waitKey() (see previous posts) when recording straight from camera.

And the last argument is a flag which will represent 'gray-scale'(monochrome) if 0
and color if 1.(This is effective in Windows systems only,on my Ubuntu this flag when 0 records nothing)

I used this function to extract and save required frames(to form clippings) from a large video file in one of my projects.

Note: The VideoWriter() will save video channel alone,audio will be absent in the output video.

5 comments:

  1. why did I get a video in 4s, while, it should be about 12s ? it's nearly always one third of the real time!

    ReplyDelete
    Replies
    1. This problem is reported in some other systems too. I guess it occurs due to difference in the frame rate we require and that of the camera.OpenCV saves the video to disk at a higher frame rate that's why you get the video with shorter duration.Sorry I am not an expert I am just guessing that way only.And one thing worth noting is that you still can access it with any frame rate of your choice using OpenCV.

      Delete
  2. its very use ful thnk youuuuuuuuuuuuuuu...!!!!!

    ReplyDelete
  3. it create the avi file but having 0kb i.e nothing ....what the problem??

    ReplyDelete