Mp4不能用于制作帧外的视频,但avi可以



我使用以下代码从我拥有的图片中创建一个电影,并使用以下代码片段:

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

最新更新