I am using the following code to create a movies out of pictures that I have and use the following code snippet:
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()
The problem is that when I change the extension to mp4
it does not work. How can I alter my code so that I can get it to work? The reason for mp4
is the speed of the process which is very slow and I think it is because avi
has more quality than mp4
.
CodePudding user response:
In cv2.VideoWriter
syntax have (filename, fourcc, fps, frameSize)
these parametrs you are missing one parameter called fourcc
(fourcc: 4-character code of codec used to compress the frames)
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()