1.摄像头录制视频
import cv2
cam = cv2.VideoCapture()
cam.open( 0, apiPreference = cv2.CAP_V4L2)
cam.set(cv2.CAP_PROP_FRAME_WIDTH,640)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
cam.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'MJPG'))
cam.set(cv2.CAP_PROP_FPS,60)
#注意当前的目录 变量2文件格式 变量3存放的视频帧率 变量3视频帧率
outvid = cv2.VideoWriter('videos/mycam.avi',cv2.VideoWriter_fourcc(*'xvid'),30,(640,480))
while True:
ret,frame = cam.read()
if ret == False:
break
cv2.imshow('USB Camera' , frame)
cv2.moveWindow('USB Camera' , 0 ,0)
#将每一帧的画面写入文件中
outvid.write(frame)
if cv2.waitKey(1) == ord('q'):
break
print(cam.get(3),cam.get(4),cam.get(5),cam.get(6))
cam.release()
#视频录制释放 就是停止并退出
outvid.release()
cv2.destroyAllWindows()
2.读取录制视频
import cv2
#cam = cv2.VideoCapture()
#读取录制的文件
cam = cv2.VideoCapture('videos/mycam.avi')
#cam.open( 0, apiPreference = cv2.CAP_V4L2)
cam.set(cv2.CAP_PROP_FRAME_WIDTH,640)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
cam.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'MJPG'))
cam.set(cv2.CAP_PROP_FPS,60)
#注意当前的目录 变量2文件格式 变量3存放的视频帧率 变量3视频帧率
#outvid = cv2.VideoWriter('videos/mycam.avi',cv2.VideoWriter_fourcc(*'xvid'),30,(640,480))
while True:
ret,frame = cam.read()
if ret == False:
break
cv2.imshow('USB Camera' , frame)
cv2.moveWindow('USB Camera' , 0 ,0)
#将每一帧的画面写入文件中
#outvid.write(frame)
if cv2.waitKey(1) == ord('q'): #如果视频读取的太快 可以sleep 也可以在这里改变等待值的大小
break
print(cam.get(3),cam.get(4),cam.get(5),cam.get(6))
cam.release()
#视频录制释放 就是停止并退出
#outvid.release()
cv2.destroyAllWindows()