I am trying to capture a video from a webcam stream. The python logic is
If button is pressed, store the current stream until I press the stop button.
Note : I am using OpenCV to stream a webcam video inside a wxPython window.
def record(self, evt):
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
I have tried the above code but it only stores 5.54kb file in the output dirctory?
How to do it?
you have set up a video writer object by
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
but you have not used write
method to write the video frame buffer.
To do it, you need to call write
method of the writer object that you instantiated by:
success, buf = cap.read()
out.write(buf)
This has to be put in a loop or be called by wx.Timer
, otherwise only one frame will be saved.
finally, when you are done with streaming, do out.release()
to close the video file.