In this tutorial, we will learn how to create a video from images using FFmpeg. FFmpeg can do this easily if the input is fed as a sequence of images that have been named in an appropriate and easy-to-parse manner.
Creating a video from images is very useful, especially when you indulge in stop-motion photography or into time-lapse movies. In time-lapse movies, you basically set up your camera to take a picture and then go to sleep for N seconds. It then wakes up and takes another picture, and goes back to sleep. You do process this long enough, and you have a 100s of images shot from a single position, and when you make a movie out of them, the result is spectacular.
If you are interested in making a time-lapse video, then take a look at this tutorial that shows you how to make a video from images using FFmpeg.
Table of Contents
Step 0: Preparing the Images
As mentioned, for this method to work as desired, it is necessary to prepare the input correctly. FFmpeg uses string matching to identify and line up the input images — and so it is important that the input images are sequentially numbered.
FFmpeg provides support for approximately three different patterns. The patterns are as follows:
- Filenames with leading zeroes, e.g.
filename-%03d.pngcorresponds to images named from
- Filenames without leading zeroes, e.g.
filename-%d.pngcorrespond to images named from
- Prefixed without a regular pattern, e.g.
filename-*.pngcorrespond to all images that start with the prefix
filename-but do not strictly follow a sequential numbering order. If you use this option, then you need to provide an additional parameter for the image to video conversion and we’ll see this in a later section.
Note: If the filename contains the character ‘%’, it can be solved by adding “%%” at the positioning of the literal to “escape” this problem!
Now that we have the images prepared, let’s demonstrate how to create a video using these images.
For this demo, I used the previously published article Thumbnails & Screenshots using FFmpeg – 3 Efficient Techniques to generate the images required. As you can see from the screenshot below, the images are sequentially numbered which makes the images-to-video conversion much easier!
Create a Video from Sequential Images using FFmpeg
The most basic form of the command to create a video from images using FFmpeg is as follows:
ffmpeg -framerate 10 -i filename-%03d.jpg output.mp4
-framerate parameter is not provided, FFmpeg selects the default rate of 25 fps. Depending on the format used to prepare the images, substitute the appropriate string matching pattern.
Using glob for Pattern Matching When Sequential Input Is Not Provided
If a sequential order is not provided, but the input filenames are appropriately prefixed and follow a logical order, then you can use the
glob parameter to create the video. For example, you might have
input-30.png, etc. Here is an example of the command line for creating a video from such images.
ffmpeg -framerate 10 -pattern_type glob -i "filename-*.jpg" output.mp4
- It is necessary to quote the
globpattern, else the command will fail due to the command interpreting the files as multiple arguments.
- Windows has a problem with globbing as it does not have access to
glob.h. See here and here. The easiest way out is to actually ensure that the files are sequentially ordered.
Here are some examples of videos created from images using FFmpeg with different frame rates.
Framerate = 25 fps (default)
Notice how FAST the video progresses, which is because we have 25 frames every second. This is unwatchable, in my opinion! It could be a great option if you are making a stop motion movie, or a time lapse movie.
Framerate = 2 fps
Notice how slow the video progresses which is because we have only two frames every second.
Framerate = 10 fps
That’s it. I hope you were able to successfully create a video from images using FFmpeg from this tutorial. If you are interested in FFmpeg, please check out the rest of the FFmpeg tuorials on OTTVerse.com.
Akshay is a student at the NYU Tandon School of Engineering, completing his M.S. in Computer Engineering. His area of interest includes software system design, machine learning, and cybersecurity.
Akshay also serves as a Contributing Author on OTTVerse.com