import cv2 ##打开摄像头 cap = cv2.VideoCapture(0) ##视频大小设置,获取帧宽度,获取帧高度 sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) fps = 20 # 输出格式 # fourcc = cv2.VideoWriter_fourcc('m','p','4','v') fourcc = cv2.VideoWriter_fourcc(*'mp4v') # fourcc = cv2.VideoWriter_fourcc('m','p','e','g') # fourcc = cv2.VideoWriter_fourcc(*'mpeg') ##open and set props vout = cv2.VideoWriter() vout.open('output.mp4',fourcc,fps,sz,True) cnt = 0 while cnt<20: cnt += 1 print(cnt) _, frame = cap.read() ##putText输出到视频上,各参数依次是:照片/添加的文字/左上角坐标/字体/字体大小/颜色/字体粗细 cv2.putText(frame,str(cnt),(10,20),cv2.FONT_HERSHEY_PLAIN,1,(0,255,0),1,cv2.LINE_AA) vout.write(frame) vout.release() cap.release()
Python3 + OpenCV 3.3读取摄像头,保存视频文件
最新推荐文章于 2024-08-20 10:56:00 发布