Set Frame Rate in FFmpeg for Image Sequence
Converting a sequence of still images into a video is a common task
in FFmpeg, but setting the correct frame rate requires placing the
appropriate flags in the right order. This article explains how to
define the input and output frame rates using the
-framerate and -r options, the differences
between them, and how to avoid common pitfalls like dropped or
duplicated frames.
The Standard
Method: Use the -framerate Option
To define how many images FFmpeg reads per second, use the
-framerate option before the input flag
(-i). This ensures that each image in your sequence
corresponds to exactly one video frame at your desired playback
speed.
ffmpeg -framerate 30 -i input_%04d.png -c:v libx264 -pix_fmt yuv420p output.mp4In this command:
-framerate 30instructs FFmpeg to read 30 images per second.-i input_%04d.pngspecifies the sequential image pattern (e.g.,input_0001.png,input_0002.png).-c:v libx264encodes the video using the H.264 codec.-pix_fmt yuv420pensures broad compatibility across standard media players.
Setting Separate Input and Output Frame Rates
If you want your source images to display at a specific rate, but you
need the final video container to conform to a standard broadcast or
display rate, combine -framerate (input) with
-r (output).
Place -framerate before the input and -r
after the input:
ffmpeg -framerate 24 -i input_%04d.png -r 60 -c:v libx264 -pix_fmt yuv420p output.mp4- Input (
-framerate 24): FFmpeg reads 24 distinct images per second of playback. - Output (
-r 60): FFmpeg resamples the stream to 60 frames per second by duplicating frames, maintaining the intended playback speed while meeting the 60 fps container requirement.
Why You Should
Avoid Using -r as an Input Flag
While -r can technically be placed before
-i, it behaves differently than -framerate
when dealing with image demuxers. Using -r before
-i can cause FFmpeg to drop or duplicate frames during the
initial read phase, leading to stutter or skipped images. Always use
-framerate for the input image sequence and reserve
-r for defining the output container rate.
Using Glob Patterns (Linux and macOS)
If your image filenames are not strictly sequential numbers, you can
use pattern matching with the -pattern_type glob option,
while still applying -framerate:
ffmpeg -framerate 25 -pattern_type glob -i '*.png' -c:v libx264 -pix_fmt yuv420p output.mp4This reads all matching .png files in alphabetical order
at 25 frames per second.