python安装opencv:
pip install opencv-python
提取视频帧 视频转图片:
import os
mp4file = 'input.mp4'
savefile = "./output"
if not os.path.exists(savefile):
os.mkdir(savefile)
import cv2
cap = cv2.VideoCapture(mp4file)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = cap.get(cv2.CAP_PROP_FPS)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print(fps)
print(size)
i = 0
while (cap.isOpened()):
i = i + 1
ret, frame = cap.read()
if ret == True:
cv2.imwrite(savefile + '/' + str(i) + '.jpg', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
print(i)
cap.release()
cv2.destroyAllWindows()
图片转视频:
import os
mp4file = 'out.mp4'
savefile = "./output"
if not os.path.exists(savefile):
os.mkdir(savefile)
import cv2
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = 30
size =(720, 1280)
videoWriter = cv2.VideoWriter(mp4file, fourcc, fps, size) # 最后一个是保存图片的尺寸
for i in range(1, 1402):
frame = cv2.imread(savefile + '/' + str(i) + '.jpg')
videoWriter.write(frame)
videoWriter.release()