我有一个脚本,它从视频中获取任意数量的帧并将它们保存为图像:这里是代码。
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))
我已经将它用于很多.avi
视频,没有问题,但现在我收到一条奇怪的消息:[msvideo1 @ 0x5615d5f85280] Packet is too small
我一直在网上搜索,但这似乎很奇怪,这不是一个例外,输出看起来好像程序完成了。
我用.avi视频作为源来调用它,其相对路径为k = 10且n = -1
我不确定这些是视频中的所有帧,更重要的是,这个奇怪的错误是什么?