# 加载libraries
import cv2
cv2.__version__
'3.4.2'
# 为了获取视频,要创建一个videoCapture对象,传递的参数是一段车辆视频
videoCapture = cv2.VideoCapture('slow_traffic_small.mp4')
type(videoCapture)
cv2.VideoCapture
# 获取视频的帧率(FPS,frames per second)
fps = videoCapture.get(cv2.CAP_PROP_FPS)
fps
29.97002997002997
# 获取视频的帧宽(frame width)和帧高(frame height)
size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
size
(640, 360)
# 为了保存视频,要先指定FourCC编码,该编码可从fourcc.org查到
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
# 然后创建一个videoWriter对象
videoWriter = cv2.VideoWriter('out.avi', fourcc, fps, size)
# 通过read读取视频中的帧,每一帧是一副基于BGR格式的图像,然后通过write将图像一帧帧写入要保存的视频
success, frame = videoCapture.read()
while success:
videoWriter.write(frame)
success, frame = videoCapture.read()