Crafting Videos from Stills: A Step-by-Step Guide to Image Sequences with FFmpeg

Nkugwa Mark William
3 min readJul 25, 2023

--

Turning a sequence of images into a video can be a powerful way to convey a story, demonstrate a process, or showcase a portfolio. FFmpeg, the multimedia workhorse, offers a simple yet efficient approach to accomplish this. This guide provides a walkthrough on how to craft videos from still images using FFmpeg and ends with a ready-to-use template for your convenience.

1. Preparing Your Images

Before you begin, ensure that:

  • All images are of the same size. If they aren’t, you may need to resize them for consistency.
  • Images are named sequentially (e.g., img001.jpg, img002.jpg) to maintain order.

2. Basic Video Creation

To create a video from images, you can use the following command:

ffmpeg -framerate 24 -i img%03d.jpg output.mp4

This command takes images named img001.jpg, img002.jpg, and so on, and creates a video named output.mp4 with a frame rate of 24.

3. Adding Audio to Your Video

To combine your image sequence with an audio track:

ffmpeg -framerate 24 -i img%03d.jpg -i audio.mp3 -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest output.mp4

This command ensures the video takes the length of the shortest input, either the image sequence duration or the audio track length.

4. Adjusting Frame Rate

The -framerate parameter controls how long each image is displayed. A higher frame rate will make the video play faster, while a lower frame rate will do the opposite.

5. Fine-Tuning Video Quality

To control the quality of the video output, you can adjust the -crf parameter (ranges from 0 to 51, where 0 is lossless, 23 is default, and 51 is worst):

ffmpeg -framerate 24 -i img%03d.jpg -c:v libx264 -crf 18 output.mp4

6. Video Editing Template

For your convenience, here’s a template that summarizes the essential commands:

# Basic video creation from images
ffmpeg -framerate {frame_rate} -i {image_prefix}%{padding}.jpg {output_name}.mp4

# Adding audio
ffmpeg -framerate {frame_rate} -i {image_prefix}%{padding}.jpg -i {audio_file} -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest {output_name}.mp4

# Adjusting video quality
ffmpeg -framerate {frame_rate} -i {image_prefix}%{padding}.jpg -c:v libx264 -crf {quality} {output_name}.mp4

Replace placeholders {frame_rate}, {image_prefix}, {padding}, {output_name}, {audio_file}, and {quality} with your desired values.

7. Crafting Videos in Python with FFmpeg

For Python, the imageio-ffmpeg library acts as a convenient wrapper around FFmpeg.

Installation:

pip install imageio[ffmpeg]

Example:

import imageio

def create_video_from_images(image_list, output_name, frame_rate=24):
with imageio.get_writer(output_name, mode='I', fps=frame_rate) as writer:
for image_path in image_list:
image = imageio.imread(image_path)
writer.append_data(image)

# Usage
images = ["img001.jpg", "img002.jpg", "img003.jpg"]
create_video_from_images(images, 'output.mp4')

8. Crafting Videos in JavaScript with FFmpeg

Using FFmpeg in JavaScript typically involves either calling the FFmpeg binary from Node.js or utilizing libraries like ffmpeg.js.

Example with Node.js:

First, you would use the child_process module:

const { exec } = require("child_process");

function createVideoFromImages(imagePrefix, numberOfImages, outputName, frameRate=24) {
const cmd = `ffmpeg -framerate ${frameRate} -i ${imagePrefix}%03d.jpg ${outputName}.mp4`;

exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error.message}`);
return;
}
if (stderr) {
console.error(`FFmpeg stderr: ${stderr}`);
return;
}
console.log(`Video created: ${stdout}`);
});
}

// Usage
createVideoFromImages("img", 3, "output.mp4");

Example with ffmpeg.js:

After including the library:

const ffmpeg = require('ffmpeg.js');

function createVideoFromImages(files, outputName, frameRate=24) {
ffmpeg({
arguments: ['-framerate', frameRate, '-i', 'input%03d.jpg', outputName],
files: files
});
}

// Usage would involve crafting the 'files' input based on your needs

--

--

Nkugwa Mark William
Nkugwa Mark William

Written by Nkugwa Mark William

Nkugwa Mark William is a Chemical and Process engineer , entrepreneur, software engineer and a technologists with Apps on google play store and e commerce sites

No responses yet