How to create a video using a list of images with python ?

Published: March 08, 2023

Tags: Python;

DMCA.com Protection Status

To generate a video from multiple images utilizing Python, there are numerous options available.

Using ffmpeg

For an easy way to construct a video, utilize the powerful ffmpeg tool.

Assemble all of your images in one folder:

image_01.png
image_02.png
image_02.png
...

and enter this command:

ffmpeg -framerate 10 -pattern_type glob -i '*.png' -c:v libx264 -pix_fmt yuv420p out.mp4

10 here is the FPS (Frame Per Second). See ffmpeg documentation to learn more about options available.

Note: if you got the error ffmpeg not found. A temporary solution is to enter the full path to ffmpeg:

/Users/JohnDoe/Desktop/ffmpeg -framerate 10 -pattern_type glob -i '*.png' -c:v libx264 -pix_fmt yuv420p out.mp4

If you're looking to integrate ffmpeg with Python, the process is simple! First, import os; this grants access to system commands. Then create a command variable which will include all your desired parameters for ffmpeg -framerate 10 , pattern_type glob , etc. Finally use os.system(cmd) and –ffmepg is now synced up with python:

import os

cmd = 'ffmpeg -framerate 10 -pattern_type glob -i '*.png' -c:v libx264 -pix_fmt yuv420p out.mp4'

os.system(cmd)

Using OpenCV

OpenCV is an open source library of computer vision algorithms which can be used to process digital images and videos. It has various functions related to image processing such as filtering and transforming that can be used to create a video from a list of images. To use OpenCV in Python, you need to install the appropriate library with pip or conda. Once this is done, you can start writing code in Python to process the images and create your video.

Additional Notes

Create a gif images

With Python, you can also craft GIF images just as easily as making a video see

How to create an animated image (GIF) using Imagemagick and python ?

References

Links Site
ffmpeg ffmpeg
opencv opencv.org