FFmpeg Zoompan: How to Smoothly Zoom and Hold

This article provides a complete guide on how to use the FFmpeg zoompan filter to smoothly zoom into a video over a 10-second duration and hold the final zoomed frame. You will learn the exact mathematical expressions required for the filter, how to keep the camera centered during the zoom, and how to avoid the common jitter issues associated with this filter.

The FFmpeg Command

To achieve a smooth zoom over 10 seconds and then hold, use the following FFmpeg command. This example assumes an input video of 30 frames per second (fps), meaning 10 seconds equals 300 frames (30 fps × 10 seconds).

ffmpeg -i input.mp4 -vf "scale=iw*2:ih*2,zoompan=z='if(lte(on,300),1+0.5*(on/300),1.5)':x='iw/2-(iw/zoom)/2':y='ih/2-(ih/zoom)/2':d=1:s=1920x1080,scale=1920x1080" -c:v libx264 -pix_fmt yuv420p output.mp4

How It Works (Step-by-Step)

The zoompan filter requires precise control over the zoom factor (z) and the camera coordinates (x and y) for every frame.

1. Scaling Upfront to Prevent Jitter

  • scale=iw*2:ih*2
  • Why: The zoompan filter can create a shaking or jittery effect because it truncates sub-pixel positions to integers. Scaling the input video up to double its size before applying the zoompan filter gives FFmpeg more pixels to work with, resulting in a perfectly smooth motion.

2. The Zoom Expression (z)

  • z='if(lte(on,300),1+0.5*(on/300),1.5)'
  • on: The current output frame number (starts at 1).
  • lte(on,300): A conditional check. "If the current frame number is less than or equal to 300..."
  • 1+0.5*(on/300): The zoom-in calculation. Over 300 frames, the zoom factor will linearly scale from 1.0 (no zoom) up to 1.5 (50% zoom).
  • 1.5: The fallback value. Once the frame number exceeds 300, the zoom factor stops increasing and holds steady at 1.5 for the remainder of the video.

3. Centering the Zoom (x and y)

  • x='iw/2-(iw/zoom)/2':y='ih/2-(ih/zoom)/2'
  • By default, zoompan anchors the zoom to the top-left corner of the frame. These algebraic formulas calculate the dynamic center point of the frame based on the active zoom level, ensuring the camera zooms directly into the middle of the video.

4. Frame Rate and Resolution Control

  • d=1: Set the frame duration to 1, ensuring a 1:1 mapping of input frames to output frames.
  • s=1920x1080: Specifies the internal resolution output of the zoompan filter.
  • scale=1920x1080: Scales the final video back down to standard 1080p resolution to complete the process.

Customizing the Script

If your video specifications differ, you can easily adjust the parameters:

  • Change the Duration: If your frame rate is 60 fps, 10 seconds would be 600 frames. Change 300 in the script to 600.
  • Change the Zoom Depth: To zoom in further (e.g., to double size), change 1.5 to 2.0 and update the formula to 1+1.0*(on/300).