Create a Time-Lapse Video Using FFmpeg on Linux
This guide provides a straightforward, step-by-step walkthrough for converting a directory of sequential images into a high-quality time-lapse video using FFmpeg on Linux. You will learn how to prepare your image files, run the core command, adjust frame rates, and optimize the output video format.
Step 1: Prepare Your Image Directory
Before running FFmpeg, your images need to be organized in a single directory and follow a consistent, sequential naming pattern. FFmpeg uses these patterns to read the files in the correct chronological order.
- Sequential Naming: Ensure your files are named
using a consistent numbering scheme, such as
image_0001.jpg,image_0002.jpg, etc. - Zero-Padding: Using zero-padded numbers (like
0001instead of1) is highly recommended to prevent files from being read out of order (e.g.,10coming right after1instead of2).
If your files are randomly named, you can quickly batch-rename them in a Linux terminal using a simple loop:
i=1; for f in *.jpg; do mv "$f" "$(printf "img_%04d.jpg" $i)"; i=$((i+1)); doneStep 2: The Core FFmpeg Command
Once your images are organized, navigate to your directory in the terminal and execute the following command to generate your time-lapse:
ffmpeg -framerate 24 -pattern_type glob -i '*.jpg' -c:v libx264 -pix_fmt yuv420p output.mp4Breakdown of the Command Options
-framerate 24: Defines how many images are displayed per second. A value of 24 or 30 frames per second (fps) provides smooth video playback.-pattern_type glob -i '*.jpg': Tells FFmpeg to look for all files ending in.jpgand stitch them together in alphabetical order.-c:v libx264: Sets the video codec to H.264, which offers excellent compression and universal compatibility across devices and web browsers.-pix_fmt yuv420p: Specifies the pixel format. This specific format ensures that the output video plays correctly on standard media players, as many default camera pixel formats are incompatible with standard video players.output.mp4: The final name of your generated time-lapse video file.
Step 3: Advanced Adjustments (Optional)
You can customize your time-lapse further by tweaking the quality, resolution, or handling strict numerical sequences.
Alternative Input Method (Strict Sequence)
If you prefer not to use the glob pattern, you can tell
FFmpeg to look for a specific number of digits using a placeholder:
ffmpeg -framerate 30 -i img_%04d.jpg -c:v libx264 -pix_fmt yuv420p output.mp4In this scenario, %04d tells FFmpeg to look for a
four-digit, zero-padded integer sequence.
Adjusting Video Quality
To change the quality of the output video, use the Constant Rate
Factor (-crf) flag. The scale ranges from 0 (lossless) to
51 (worst quality). A value between 18 and 23 offers a great balance
between visual quality and file size.
ffmpeg -framerate 24 -pattern_type glob -i '*.jpg' -c:v libx264 -crf 20 -pix_fmt yuv420p high_quality.mp4Handling Odd-Dimension Images
H.264 requires the video width and height to be divisible by 2. If your images have odd dimensions, FFmpeg will throw an error. You can fix this on the fly by adding a scale filter that forces divisible dimensions:
ffmpeg -framerate 24 -pattern_type glob -i '*.jpg' -c:v libx264 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -pix_fmt yuv420p output.mp4