Может быть, вы могли бы попробовать вместо сохранения изображений и их повторной загрузки, сделать захват видео из исходного видео и передать его объекту out (demo_output.avi в данном случае) ... что-то вроде этого:
import cv2
cap = cv2.VideoCapture('path to video/video.mp4')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
ret, frame = cap.read()
fps_video=cap.get(cv2.CAP_PROP_FPS)
height,width = frame.shape[:2]
out = cv2.VideoWriter('demo_output.avi',fourcc, fps_video, (width,height)) ##can be set with your width,height values
while ret:
frame = cv2.resize(frame, None, fx=1.0, fy=1.0, interpolation=cv2.INTER_AREA)
out.write(frame)
ret, frame = cap.read()
cap.release()
out.release()
cv2.destroyAllWindows()
ОБНОВЛЕНИЕ:
Если вам нужны изображения, а затем сохранить видео файл:
import cv2
import os
import numpy as np
vidcap = cv2.VideoCapture('path to video/video.mp4')
success,image = vidcap.read()
fps_video = vidcap.get(cv2.CAP_PROP_FPS)
height,width = image.shape[:2]
count = 0
while success:
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
success,image = vidcap.read()
print('Read a new frame: ', success)
count += 1
vidcap.release()
lista = [[x[5:-4],x] for x in os.listdir() if x.endswith('jpg')]
result=[]
for x in lista:
t1,t2 = np.int(x[0]),x[1]
result.append([t1,t2])
result.sort()
#recording video back
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('demo_output.avi',fourcc, fps_video, (width,height)) ##can be set with your width,height values
for img in result:
frame = cv2.imread(img[1])
out.write(frame)
out.release()