Create video with images in Python
In this tutorial, we are going to learn how to create a video with images in Python. We are going to create a video slide show of images using Python. It’s very easy, let’s see how it will work.
There are various methods available for doing this task, we can do this by cv2 library also. But for now, we are going to use one of the libraries available in Python that is MoviePy and Path library.
Moviepy Python library is used for video editing, cutting, video composing, processing, and creating custom effects.
Before proceeding towards our task we need to import these libraries to our system. We can do this using the from command as shown below in your terminal or shell. In the next step let’s see the full code.
from moviepy.editor import * from pathlib import Path
img_clips =  path_list= #accessing path of each image for image in os.listdir('img_folder/'): if image.endswith(".jpg"): path_list.append(os.path.join('img_folder/', image)) #creating slide for each image for img_path in path_list: slide = ImageClip(img_path,duration=2) img_clips.append(slide) #concatenating slides video_slides = concatenate_videoclips(img_clips, method='compose') #exporting final video video_slides.write_videofile("output_video.mp4", fps=24)
Here first, we are creating two lists img_clips and path_list for storing slides of image and path of the image. Then we are using for loop and checking files that are ending with ‘.jpg’ extensions. Path of that each image will be added to path_list using append function of list.
Later on from the img_folder we are accessing given images. for each path in path_list, there is an image associated with it and we are creating a slide of that image in our video. By use of
ImageClip() function available in Moviepy library
Here duration=2 is the duration of that slide is given in function. And then we add that slide to the img_clip.
For video we are creating variable video_slides, in this, we are joining all clips by using the
concatenate_videoclips function of moviepy.
Lastly, we are extracting that video using the write_videofile function. giving 24 fps rate as a parameter. As a result, output_video.mp4 file is saved.
[MoviePy] >>>> Building video output_video.mp4 [MoviePy] Writing video output_video.mp4 100%|█████████▉| 384/385 [00:16<00:00, 22.96it/s] [MoviePy] Done. [MoviePy] >>>> Video ready: output_video.mp4
Also read: Extract speech text from video in Python