视频按帧截取 python程序
- 代码注释很清晰,请自行阅读,
- 参考部分网络资源,由于较难追溯,不记出处。
import cv2 as cv
import os
video_path = "/home/username/Videos/file.mp4"
cap = cv.VideoCapture(video_path)
frameRate = 5
aim_path = "./capture_image/"
if not os.path.exists(aim_path):
os.mkdir(aim_path)
fCount = 0
while (True):
ret, frame = cap.read()
fCount += 1
if ret:
if (fCount % frameRate == 0):
cv.imshow("frame", frame)
cv.waitKey(100)
key = cv.waitKey()
if key == ord("n"):
cv.destroyAllWindows()
continue
elif key == 27:
cv.destroyAllWindows()
break
elif key == 32:
cv.destroyAllWindows()
print("开始截取视频第:" + str(fCount) + " 帧")
cv.imwrite(aim_path + str("210925%05d" %
fCount) + '.jpg', frame)
print("Save this frame!")
else:
print("Retype please!")
cv.destroyAllWindows()
else:
print("已无帧可存。")
break
cap.release()