I have a single script which takes an arbitrary number of frames from a video and saves them as images: here goes the code.
def get_n_frames(n, k, src, dst):
# read n frames from video in src and save them as images on folder dst. If dst does not exists, it is created.
# if n == -1, are saved as many frames as posiible
# k is the number of frames to skip between each pair of frames (ie save only one out of k)
max_intentos = 10
end_of_video = 0
while True:
cap = cv2.VideoCapture(src)
if cap.isOpened():
break
if max_intentos:
raise "Capture is not open. There may be a problem with source {}".format(src)
print("trying again")
max_intentos -=1
try:
i=0
frame_number = 0
while(frame_number < n or n==-1):
# Capture frame-by-frame
ret, frame = cap.read()
if ret:
end_of_video = 0
if i % k == 0:
final_dest = join(dst, '{0}_{1:03}.jpg'.format(datetime.now().strftime('%Y-%m-%d-%H:%M:%S'), frame_number))
if cv2.imwrite(final_dest, frame):
print("Saving frame {}".format(frame_number))
frame_number +=1
i+=1
elif end_of_video < 10:
end_of_video +=1
else:
if n != -1:
print("WARNING: Video is too short, can not take {} frames with k={}".format(n, k))
break
finally:
# When everything done, release the capture
cap.release()
print("{} frames were saved".format(frame_number))
I have used it for a lot of .avi
videos, without problem, but now I'm getting an strange message: [msvideo1 @ 0x5615d5f85280] Packet is too small
I've been searching on the net but this seems very strange, it's not an exception, output look as if program finished ok.
I'm calling it with a .avi video as source, with its relative path, k=10 and n=-1
I'm not sure if those are all frames from the video and more important, what is this strange error?