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

how to make a video from extracted frames?

我将视频帧提取到名为 "images" 的文件夹中。将图像保存在我的文件夹中后,我使用以下代码再次创建视频。我得到了视频,但帧是随机排列的,我怎样才能按顺序排列它们?感谢 post

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

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

也许您可以尝试不保存图像并再次加载它们,而是从源视频中捕获视频并将其传递给输出对象(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()

如果您的要求需要您存储帧,试试这个

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 i in range(len(images)):
    video.write(cv2.imread(os.path.join(image_folder, 'a'+str(i)+'.jpg')))

cv2.destroyAllWindows()
video.release()

尝试 glob 列出文件并对列表进行排序。我已经使用 glob 编辑了您的代码。 (假设你的图像文件名是你想要的顺序)

import cv2
import os
import glob

video_name = 'video.avi'

images = glob.glob('images/*.jpg')
images.sort()

frame = cv2.imread(images[0])
height, width, layers = frame.shape

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

for image in images:
    video.write(cv2.imread(image))

cv2.destroyAllWindows()
video.release() 

看来你需要自然排序,所以试试natsort library:

from natsort import natsorted

images = natsorted(images)

使用 pip 安装:

pip install natsort

这对我来说很好

import cv2
import os


image_folder = 'c:\m\'
video_name = 'c:\m\avideo.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

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


#25 fps
video = cv2.VideoWriter(video_name, 0, 25, (width,height))

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

cv2.destroyAllWindows()
video.release()

材料和结果Here

  1. 打印您的 images 列表并查看图像的顺序?他们排序了吗?
  2. 你的图片文件名是什么?选择像 00001.jpg00002.jpg 这样的递增数字而不是 1.jpg2.jpg 会很有帮助,这可能会弄乱排序。

    1. 读完os.listdir('.')后的图片排序: 这是 pathlib 库的示例。
for filename in sorted([e for e in path.iterdir() if e.is_file() and str(e).endswith(".png")]):
    print(filename)
    img = cv2.imread(str(filename))
    img_array.append(img)

或者只是使用: for filename in sorted(os.listdir(path))