使用 python 录制网络摄像头视频

Record a Webcam video with python

我正在尝试从网络摄像头流中捕捉视频。 python 逻辑是

If button is pressed, store the current stream until I press the stop button.

注意:我正在使用 OpenCV 在 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))

我试过上面的代码,但它在输出目录中只存储了 5.54kb 的文件?

怎么做?

您已通过

设置了视频编写器对象

out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

但是您还没有使用write方法来写入视频帧缓冲区。

为此,您需要调用您实例化的编写器对象的 write 方法:

success, buf = cap.read()
out.write(buf)

这必须放在循环中或由wx.Timer调用,否则只会保存一帧。

最后,当您完成流式传输后,请执行 out.release() 关闭视频文件。