Creating Videos from Photos Using FFmpeg

Siva
2 min readFeb 23, 2024

Introduction

In multimedia processing, the need often arises to convert a collection of photos into a cohesive video. This can be useful for creating slideshows, time-lapse videos, or simply merging a series of images into a single video file. FFmpeg, a powerful multimedia framework, provides a convenient command-line interface to achieve this task.

In this blog post, we’ll explore few examples of using FFmpeg to create videos from photos — one resulting in a WebM video and the other in an MP4/MKV video.

Prerequisites

Before getting started, ensure you have FFmpeg installed on your system. You can install it using your package manager. For example, on Debian/Ubuntu systems:

sudo apt-get install ffmpeg

Creating a WebM Video

To create a WebM video from a series of JPEG images, use the following FFmpeg command:

ffmpeg -framerate 1/3 -pattern_type glob -i ‘files*.jpeg’ -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis output.webm

Let’s break down the command:

  • -framerate 1/3: Specifies the frame rate of the output video. In this case, one frame every three seconds.
  • -pattern_type glob -i ‘files*.jpeg’: Uses a glob pattern to match all JPEG files starting with “files” in the current directory.
  • -c:v libvpx: Chooses the libvpx codec for video compression, commonly used in WebM format.
  • -crf 10: Controls the video quality, with lower values indicating higher quality.
  • -b:v 1M: Sets the target video bitrate to 1 Mbps.
  • -c:a libvorbis: Specifies the libvorbis codec for audio compression.

Creating an MP4 Video

To create an MP4 video from JPEG images, use the following FFmpeg command:

ffmpeg -framerate 1/3 -pattern_type glob -i ‘photos*.jpeg’ -c:v libx264 -r 30 -pix_fmt yuv420p -vf “fps=25,format=yuv420p” output.mp4

Breaking down the command:

  • -framerate 1/3: Sets the frame rate to one frame every three seconds.
  • -pattern_type glob -i ‘photos*.jpeg’: Uses a glob pattern to match JPEG files starting with “photos.”
  • -c:v libx264: Chooses the libx264 codec for H.264 video compression, commonly used in MP4 format.
  • -r 30: Sets the output frame rate to 30 frames per second.
  • -pix_fmt yuv420p: Specifies the pixel format for the output video.
  • -vf “fps=25,format=youv420p”: Uses a video filter to adjust the frame rate and pixel format.

Creating an MKV Video

ffmpeg -framerate 1/3 -pattern_type glob -i ‘mkvfiles*.jpeg’ -c:v libx264 -r 30 -pix_fmt yuv420p output.mkv

Here’s an explanation of the command:

  • -framerate 1/3: Sets the frame rate to one frame every three seconds.
  • -pattern_type glob -i ‘mkvfiles*.jpeg’: Uses a glob pattern to match JPEG files starting with “mkvfiles.”
  • -c:v libx264: Chooses the libx264 codec for H.264 video compression, commonly used in MKV format.
  • -r 30: Sets the output frame rate to 30 frames per second.
  • -pix_fmt yuv420p: Specifies the pixel format for the output video.
  • output.mkv: Sets the output file to be in MKV format.

This command creates an MKV video file named output.mkv in the same directory as your JPEG images.

Conclusion

FFmpeg is a versatile tool for handling multimedia tasks, including the creation of videos from photos. Whether you need a WebM or MP4 output, understanding the various parameters allows you to customize the output according to your preferences. Experiment with different options to achieve the desired results for your multimedia projects.

--

--