Is it possible to save a desired amount of images (DESIRED_FRAMES_TO_SAVE
) from a video file spread-out evenly throughout the entire video file footage?
Hopefully this makes sense I have a video file that is 8 seconds in length and I would like to save 60 Frames of the video file in sequential order.
Trying to throw something together in open CV the code works but I know it should be improved. For example the if count % 3 == 0:
I got from calculating the total frames of the video file which is 223
and then dividing by DESIRED_FRAMES_TO_SAVE
or 60
I come up with about ~3.72...so in other words I think about every 3 or 4 frames I should save one to come up with ~DESIRED_FRAMES_TO_SAVE
by the end of the video file. Sorry this is an odd question but would anyone have any advice on how to rewrite this better without to while loops?
import cv2
VIDEO_FILE = "hello.mp4"
DESIRED_FRAMES_TO_SAVE = 60
count = 0
cap = cv2.VideoCapture(VIDEO_FILE)
'''
# calculate total frames
while True:
success, frame = cap.read()
if success == True:
count += 1
else:
break
total_frames = count
print("TOTAL_FRAMES",total_frames)
'''
count = 0
while True:
success, frame = cap.read()
name = './raw_images/' + str(count) + '.jpg'
if success == True:
if count % 3 == 0:
cv2.imwrite(name, frame)
print(count)
elif count > DESIRED_FRAMES_TO_SAVE:
break
count += 1
else:
break
cap.release()
cv2.destroyAllWindows()