from os import mkdir
from os.path import isdir
import datetime
from time import sleep
from threading import Thread
import cv2
def write():
while cap.isOpened():
ret, frame = cap.read()
if ret: #写入视频文件
aviFile.write(frame)
aviFile.release()
cap = cv2.VideoCapture(0) #参数0为笔记本自带摄像头
now = str(datetime.datetime.now())[:19].replace(':', '_') #当前日期时间,如2019-05-24 23:11:00
dirName = now[:10] #目录名
tempAviFile = dirName+'\\'+now+'.avi' #视频文件名
if not isdir(dirName): #创建目录
mkdir(dirName)
#创建视频文件
aviFile = cv2.VideoWriter(tempAviFile,cv2.VideoWriter_fourcc('M','J','P','G'),25, (640,480)) #帧速和视频宽度、高度
t=Thread(target=write).start()
input('Press any key to end the recording.')
cap.release()
Python摄像头录制视频保存到本地
猜你喜欢
转载自blog.csdn.net/weixin_43873198/article/details/107631034
今日推荐
周排行