Python cv2 播Video
如有误,请指正。
VideoCapture Video file 请自选
import cv2# open video filecap = cv2.VideoCapture('rain.mp4')#--- video information frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))FPS = int(cap.get(cv2.CAP_PROP_FPS))frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))print(f'width {frame_width} height {frame_height}')print(f'FPS: {FPS} frame_count {frame_count}')#--- setup window for show cv2.namedWindow('video',cv2.WINDOW_KEEPRATIO) cv2.resizeWindow('video', int(frame_width/3), int(frame_height/3))cv2.moveWindow('video',300,200)# read frame by framewhile(True): ret, frame = cap.read() cv2.imshow('video',frame) #播灰阶 #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #cv2.imshow('video',gray) #waitKey 单位msec 1000msec = 1sec #FPS=24 表示1sec播24张,所以1000msec/24=25msec #每张可停格 25msec=waitKey(25) 可设定20~25之间,肉眼无法区分快慢 #Escape key to leave if cv2.waitKey(20)== 27: break#--- ending cap.release()cv2.destroyAllWindows()