首先ubuntu下用pip安装opencv-python
截取视频每一帧保存为图片
代码如下:
import cv2
# load video from your path
vc = cv2.VideoCapture('./video/20180327_IMG_1225.avi')
c = 0
rval=vc.isOpened()
while rval:
c = c + 1
rval, frame = vc.read()
if rval:
cv2.imwrite('./picture/7/output-'+str(c) + '.jpg', frame)
else:
break
vc.release()
这样解析出来每帧之间差别不大,如果想隔一个固定的帧数间隔进行截取,可以看下面的代码。下面的代码对多个视频进行批处理,按每10帧截1帧的方式解析视频:
import cv2
import os
for i in range(1,10):
# picture save path
path = '/home/ogai/cvtest/picture/pic/'+str(i)+'/'
if not os.path.exists(path):
os.mkdir(path)
# load video from video path
vc = cv2.VideoCapture('/home/ogai/cvtest/video/wu/WeChatSight'+str(i)+'