有时,为了存储摄像头视频,我们可以利用OpenCV来实现,具体方法如下:
(1)打开摄像头。
(2)设置好必要参数,包括文件保存位置。
(3)读取摄像头帧图像,保存到指定文件夹下。(由于数据量大,可以加大采集的间隔,通过对cnt变量的控制)
(4)最后要释放资源。
import cv2
cap = cv2.VideoCapture(0)
sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fps = 25
fourcc = cv2.VideoWriter_fourcc(*'mpeg')
vout = cv2.VideoWriter()
vout.open('d:\\output.mp4',fourcc,fps,sz,True)
import time
cnt = 0
while cnt<2000:
cnt += 1
# print(cnt)
_, frame = cap.read()
cv2.putText(frame,"Not Copy!"+time.strftime("%Y-%m-%d %H:%M:%S ", time.localtime())+str(cnt), (10, 20), cv2.FONT_HERSHEY_PLAIN, 1, (0,255,0), 1, cv2.LINE_AA)
print(frame)
vout.write(frame)
if cnt%25==0:
cv2.imwrite('d:\\test\\'+str(cnt)+'.jpg',frame)
vout.release()
cap.release()