使用opencv2在python中编写流视频 [英] using opencv2 write streaming video in python

查看:106
本文介绍了使用opencv2在python中编写流视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我想保存流视频.

In my project i want to save streaming video.

import cv2;
if __name__ == "__main__":
     camera =  cv2.VideoCapture(0);
     while True:
          f,img = camera.read();
          cv2.imshow("webcam",img);
          if (cv2.waitKey (5) != -1):
                break;

` 使用以上代码,可以从网络摄像头流式传输视频.如何将此流视频写入文件?

` using above code it is possible to stream video from the web cam. How to write this streaming video to a file?

推荐答案

您可以简单地将抓取的帧保存到图像中:

You can simply save the grabbed frames into images:

camera = cv2.VideoCapture(0)
i = 0
while True:
   f,img = camera.read()
   cv2.imshow("webcam",img)
   if (cv2.waitKey(5) != -1):
       break
   cv2.imwrite('{0:05d}.jpg'.format(i),img)
   i += 1

或观看这样的视频

camera = cv2.VideoCapture(0)
video  = cv2.VideoWriter('video.avi', -1, 25, (640, 480));
while True:
   f,img = camera.read()
   video.write(img)
   cv2.imshow("webcam",img)
   if (cv2.waitKey(5) != -1):
       break
video.release()    

在创建VideoWriter对象时,需要提供几个可以从输入流中提取的参数.可以在此处找到教程.

When creating VideoWriter object, you need to provide several parameters that you can extract from the input stream. A tutorial can be found here.

这篇关于使用opencv2在python中编写流视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆