I have a video file, I know how to save all of the frames,but my question is how to save only one frame of every 20 frames as an image? Thanks.
Advertisement
Answer
This is based on this tutorial: https://theailearner.com/2018/10/15/extracting-and-saving-video-frames-using-opencv-python/
JavaScript
x
16
16
1
import cv2
2
3
# Opens the Video file
4
cap= cv2.VideoCapture('C:/New/Videos/Play.mp4')
5
i=0
6
while(cap.isOpened()):
7
ret, frame = cap.read()
8
if ret == False:
9
break
10
if i % 20 == 0: # this is the line I added to make it only save one frame every 20
11
cv2.imwrite('kang'+str(i)+'.jpg',frame)
12
i+=1
13
14
cap.release()
15
cv2.destroyAllWindows()
16