Opencv提供了类似java中静态变量的参数,可以通过get(参数)获取当前视频的信息,参数类别还是挺多的,按照需求查手册或源码即可。
源码参数举例
获取视频帧的长宽
import cv2
if __name__ == '__main__':mp4_path ='xhx_utils/images/demo.mp4'video = cv2.VideoCapture(mp4_path)height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))print("长:{%d},宽:{%d}"%(height,width))video.release()
获取视频的总帧数
import cv2
if __name__ == '__main__':mp4_path ='xhx_utils/images/demo.mp4'video = cv2.VideoCapture(mp4_path)num_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))print("帧数:"+str(num_frames))video.release()
获取视频的FPS
import cv2
if __name__ == '__main__':mp4_path ='xhx_utils/images/demo.mp4'video = cv2.VideoCapture(mp4_path)fps = int(video.get(cv2.CAP_PROP_FPS))print("fps:"+str(fps))video.release()
还有很多其他的参数可以获取,大家可以按照自己的需求查取。