Mp4 不适用于制作无帧视频,但 avi 可以

Mp4 does not work for making videos out of frames but avi works

我正在使用以下代码从我拥有的图片创建电影并使用以下代码片段:

import cv2
import os

image_folder = 'shots'
video_name = 'video.avi'
fps = 25

images = [img for img in os.listdir(image_folder) if img.endswith(".png")]

images = sorted(images)[:][:steps]

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

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

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

cv2.destroyAllWindows()
video.release()

问题是,当我将扩展名更改为 mp4 时,它不起作用。我怎样才能改变我的代码,让它工作? mp4 的原因是这个过程的速度非常慢,我认为这是因为 avimp4 质量更高。

cv2.VideoWriter 语法中有 (filename, fourcc, fps, frameSize) 这些参数,您缺少一个名为 fourcc 的参数(fourcc:用于压缩帧的编解码器的 4 字符代码)

import cv2
import os

image_folder = 'shots'
video_name = 'video.mp4'
fps = 25

images = [img for img in os.listdir(image_folder) if img.endswith(".png")]

images = sorted(images)[:][:steps]

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

video = cv2.VideoWriter(video_name,cv2.VideoWriter_fourcc(*'MP4V'), fps, (width, height))

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

cv2.destroyAllWindows()
video.release()