我正在使用以下代碼從我擁有的圖片中創建一部電影,并使用以下代碼片段:
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
的原因是這個過程的速度非常慢,我認為這是因為avi
比mp4
有更多的質量。
在
cv2.VideoWriter
語法中有(filename, fourcc, fps, frameSize)
這些參數,您缺少一個名為fourcc
的參數(用于壓縮幀的編解碼器的fourcc:4-character代碼)