Tutorial: Create a Video from images using FFmpeg

In this tutorial, we will learn how to Create a Video from images using FFmpeg. We will merge up to 10 images from a single folder to create a video using FFmpeg on a Linux Debian machine. We will start with simple iterations and gradually add more complex features to our video.

With this tutorial, you will be able to create a video from images of different resolutions and formats, add background music, and create videos in different resolutions.

Prerequisites to Create a Video from Images using FFmpeg

  • A Linux Debian machine with FFmpeg installed. You can install FFmpeg by running the command: $ sudo apt-get install ffmpeg.
  • A folder containing the images you want to merge into a video. All images should be in the same format (e.g., .jpg) and have the same resolution.

Sample video generated using ffmpeg

Iteration 1: Merging Images into a Video

Let’s start by merging our images into a video with the following specifications:

  • The first image becomes the thumbnail of the video with a duration of 4 seconds.
  • Each subsequent image slides in with a duration of 4 seconds.
  • The last image remains for 3 seconds.

Here’s the command to achieve this:

$ ffmpeg -framerate 1/4 -i %03d.jpg -vf scale=1280:720 -c:v libx264 -crf 18 output.mp4

Let’s break down the command:

  • ffmpeg: The command to execute FFmpeg.
  • -framerate 1/4: Sets the frame rate to 1 frame every 4 seconds.
  • -i %03d.jpg: Specifies the input files. %03d is a placeholder for the file number, and .jpg is the extension. The %03d format means that the file numbers will be padded with zeros to a minimum width of 3 digits (e.g., 001.jpg, 002.jpg, etc.).
  • -vf scale=1280:720: Scales the video to a resolution of 1280×720.
  • -c:v libx264: Specifies the video codec as H.264.
  • -crf 18: Sets the Constant Rate Factor for quality (lower values mean better quality).
  • output.mp4: The output file name and format.

Iteration 2: Adding Text to the Images

Now, let’s add text to our images:

  • The first image: “Namaskar” at the top middle.
  • Each subsequent image: “Sample Text” at the bottom middle.
  • The last image: “Dhanyawaad” at the top middle.

We’ll use FFmpeg’s drawtext filter to achieve this:

$ ffmpeg -framerate 1/4 -i %03d.jpg -vf "drawtext=text='Namaskar':x=w/2:y=24:fontsize=24:fontcolor=white,drawtext=text='Sample Text':x=w/2:y=h-24:fontsize=24:fontcolor=white[1-8],drawtext=text='Dhanyawaad':x=w/2:y=24:fontsize=24:fontcolor=white[9]" -vf scale=1280:720 -c:v libx264 -crf 18 output.mp4

Here’s what’s changed:

  • We added three drawtext filters:
    • The first filter adds “Namaskar” to the first image.
    • The second filter adds “Sample Text” to images 2-8.
    • The third filter adds “Dhanyawaad” to the last image.

Iteration 3: Adding Background Music

Let’s add some background music to our video:

$ ffmpeg -framerate 1/4 -i %03d.jpg -vf "drawtext=text='Namaskar':x=w/2:y=24:fontsize=24:fontcolor=white,drawtext=text='Sample Text':x=w/2:y=h-24:fontsize=24:fontcolor=white[1-8],drawtext=text='Dhanyawaad':x=w/2:y=24:fontsize=24:fontcolor=white[9]" -vf scale=1280:720 -c:v libx264 -crf 18 -i background_music.mp3 -c:a aac -b:a 128k output.mp4

Here’s what’s changed:

  • We added the background_music.mp3 file as an input.
  • We specified the audio codec as AAC and the bitrate as 128 kbps.
ffmpeg on linux terminal. Create a Video from Images using FFmpeg
AI generated image: Sora

Iteration 4: Handling Different Image Resolutions and Formats

Let’s modify our command to handle images of different resolutions and formats (e.g., .jpg, .png, .webp):

$ ffmpeg -framerate 1/4 -i %03d.* -vf scale=1280:720 -c:v libx264 -crf 18 output.mp4

Here’s what’s changed:

  • We replaced .jpg with .*, which allows FFmpeg to input files with any extension.

Creating a 1080p Video

To create a 1080p video, simply modify the scale filter:

$ ffmpeg -framerate 1/4 -i %03d.* -vf scale=1920:1080 -c:v libx264 -crf 18 output_1080p.mp4

Creating a 4K Video

To create a 4K video, modify the scale filter:

Creating a 4K Video

$ ffmpeg -framerate 1/4 -i %03d.* -vf scale=3840:2160 -c:v libx264 -crf 18 output_4k.mp4

That’s it! You should now be able to successfully create a video from images, add text to the images, and create videos in different resolutions using FFmpeg on a Linux Debian machine.