也许你可以尝试不是保存图像并再次加载它们,而是从源视频中进行视频捕获,并将其传递给输出对象(在这种情况下为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))
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)
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()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('demo_output.avi',fourcc, fps_video, (width,height))
for img in result:
frame = cv2.imread(img[1])
out.write(frame)
out.release()