Loop Image and Add Dynamic Overlay with FFmpeg

This guide explains how to use FFmpeg to convert a single static image into a video of a specific duration while applying a dynamic, moving overlay. You will learn the exact FFmpeg commands needed to loop the image input, define the output duration, and overlay an animated element or apply motion coordinates to create a dynamic visual effect.

Looping a Static Image for a Set Duration

To loop a static image, you must place the -loop 1 option before the input image, and set the desired output duration using the -t parameter (in seconds) or -to (in HH:MM:SS format).

Here is the basic syntax to loop an image for 10 seconds:

ffmpeg -loop 1 -t 10 -i background.png -pix_fmt yuv420p output.mp4
  • -loop 1: Enables looping for the single-frame input.
  • -t 10: Limits the output duration to 10 seconds.
  • -pix_fmt yuv420p: Ensures maximum compatibility with standard media players.

Adding a Dynamic Overlay

You can add a dynamic overlay in two ways: by making a static watermark move across the screen using mathematical expressions, or by overlaying an animated video/GIF.

Method 1: Overlaying an Animated GIF or Video

To overlay an animated asset (like a looping GIF or transparent video) over your looped background, use the -ignore_loop 0 flag for GIFs or -stream_loop -1 for videos to loop the overlay infinitely, then merge them using filter_complex.

ffmpeg -loop 1 -t 15 -i background.jpg -ignore_loop 0 -i animated_overlay.gif -filter_complex "[0:v][1:v]overlay=x=10:y=10:shortest=1[out]" -map "[out]" -pix_fmt yuv420p output.mp4
  • -ignore_loop 0: Infinite looping for the input GIF.
  • [0:v][1:v]overlay: Places the second input (the GIF) on top of the first input (the background image).
  • x=10:y=10: Positions the overlay 10 pixels from the top-left corner.
  • shortest=1: Stops the encoding process as soon as the background's 15-second duration limit is reached.

Method 2: Creating a Moving Overlay Using Math Expressions

If you want a static overlay image to move dynamically across the looped background, use variable expressions inside the overlay filter.

The command below moves a watermark horizontally across the screen over a 10-second looped image:

ffmpeg -loop 1 -t 10 -i background.jpg -i watermark.png -filter_complex "[0:v][1:v]overlay=x='t*100':y='H/2-h/2'[out]" -map "[out]" -pix_fmt yuv420p output.mp4
  • x='t*100': Moves the overlay horizontally based on time (t represents current time in seconds). The overlay moves to the right at a speed of 100 pixels per second.
  • y='H/2-h/2': Centers the overlay vertically, where H is the background height and h is the overlay height.

Method 3: Bouncing or Custom Trajectory Movement

For a more complex dynamic movement, such as bouncing, use trigonometric functions within the coordinate values:

ffmpeg -loop 1 -t 10 -i background.jpg -i watermark.png -filter_complex "[0:v][1:v]overlay=x='(W-w)/2 + sin(t)*100':y='(H-h)/2 + cos(t)*100'[out]" -map "[out]" -pix_fmt yuv420p output.mp4
  • sin(t) and cos(t): Create a smooth, circular dynamic path around the center of the background image.