# 查看opencv版本的命令
pkg-config --modversion opencv
"""视频读帧保存"""
import cv2 as cv
import numpy as np
from PIL import Image
cap = cv.VideoCapture('/home/ivy/Desktop/ls.flv')
total = cap.get(cv.CAP_PROP_FRAME_COUNT) # 总帧数
fps = cap.get(cv.CAP_PROP_FPS) # 帧率
print(int(total / fps)) # 视频时间
step = (total - 100) / 9 # 间隔时间
lis_img = []
for i in range(100, int(total), int(step)):
try:
cap.set(cv.CAP_PROP_POS_FRAMES, i)
cap.grab() # 解码并返回捕获的视频帧
except Exception as e:
print(e)
ret, frame = cap.read()
if not ret:
continue
cv.imwrite(str(i) + '.jpg', frame) # 存储为图像
cv.waitKey(5)
cap.release()
cv.destroyAllWindows()
2018.12.15 有效