如何从提取的帧中制作视频?

问题描述 投票:0回答:2

我使用以下代码将视频帧提取到名为“ images”的文件夹中:

import cv2

# Opens the Video file
cap= cv2.VideoCapture('path to video/video.mp4')
i=0
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == False:
        break
    cv2.imwrite('a'+str(i)+'.jpg',frame)
    i+=1

cap.release()
cv2.destroyAllWindows()

在将图像保存到文件夹后,我使用以下代码再次创建该视频。我得到了视频,但是帧是随机排序的,如何按顺序排列呢?感谢您的帖子

import cv2
import os


image_folder = 'images'
video_name = 'video.avi'

images = [img for img in os.listdir(image_folder) if img.endswith(".jpg")]
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape

video = cv2.VideoWriter(video_name, 0, 1, (width,height))

for image in images:
    video.write(cv2.imread(os.path.join(image_folder, image)))

cv2.destroyAllWindows()
video.release()

请告知,我该如何解决?我希望视频的速率与原始视频的速率相同,并且帧要按顺序排列。

python image opencv video frame
2个回答
0
投票

也许您可以尝试代替保存图像并再次加载它们,从源视频中捕获视频并将其传递给输出对象(在这种情况下为demo_output.avi ...……):>

import cv2

cap= cv2.VideoCapture('path to video/video.mp4')

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('demo_output.avi',fourcc, 8.0, (640,480)) ##640, 480 can be set with your width,height values


ret, frame= cap.read()

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()      

0
投票

如果您需要存储框架,请尝试此操作

© www.soinside.com 2019 - 2024. All rights reserved.