How to Create a Video from Images with FFmpeg
In this tutorial, we will learn how to create a video from a collection of images using FFmpeg. This will be useful for scientific animations, stop-motion movies and picture slideshows.
Create a Directory of Images
The first step is to create a directory and add your images inside it. The name of each image should be a number starting at 0 and counting up. All the file extensions should be the same too.
mkdir image-sequence
0.jpg
1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
Now move to the image-sequence directory in the terminal:
cd image-sequence
The FFmpeg Command
In the FFmpeg command, specify the input file as %d.jpg
followed by the name of the output file.
%d
is a placeholder for any number. The default behaviour of FFmpeg is to start at 0 and continue counting up until no more files are found when it is given a placeholder number.
ffmpeg -i %d.jpg output.mp4
You can tell FFmpeg to start counting at a number other than 0 using the -start_number
flag like this:
ffmpeg -start_number 3 -i %d.jpg output.mp4
If you get an error saying; height not divisible by 2, scale the output like this to fix the problem:
ffmpeg -i %d.jpg scale=-2:720 output.mp4
Replace 720 with the height in pixels that the video should be.
Setting a Custom Frame Rate
You will notice that the image sequence in your output video moves super fast. This is because the default framerate for FFmpeg is 25fps. You can reduce or increase this value using the -framerate
flag. Typically for slideshows, you will want a slow framerate of maybe 0.1fps.
ffmpeg -framerate 0.1 -i %d.jpg output.mp4
Note – you must specify the -framerate
as the first argument of the FFmpeg command or it will not work.