Zoom into Specific Coordinates Using FFmpeg Zoompan

This guide explains how to use the FFmpeg zoompan filter to zoom into a specific X and Y coordinate on a video. You will learn the mathematics behind centering the zoom on your target coordinates, how to structure the FFmpeg command, and how to apply smooth transitions.

Understanding the Zoompan Coordinate System

The zoompan filter works by cropping a portion of the input video and scaling it back up to the output resolution. By default, the filter calculates the zoom from the top-left corner (0,0) of the video frame.

To zoom into a specific coordinate \((X_{target}, Y_{target})\), you must offset the crop window's top-left corner (\(x\) and \(y\)) so that your target coordinate remains in the center of the zoomed area.

The formulas to center the zoom window on your target coordinates are:

  • X Coordinate: x = X_target - (iw / (2 * zoom))
  • Y Coordinate: y = Y_target - (ih / (2 * zoom))

Where:

  • iw and ih are the input video's width and height.
  • zoom is the current zoom factor (e.g., 1.5, 2, or a dynamic variable).

The Basic FFmpeg Command

To zoom into a static target coordinate—for example, \(X=500\) and \(Y=300\) on a 1920x1080 video with a zoom factor of 2—use the following command:

ffmpeg -i input.mp4 -vf "zoompan=z='2':x='500-(iw/(2*zoom))':y='300-(ih/(2*zoom))':d=125:s=1920x1080" -c:v libx264 output.mp4

Parameter Breakdown:

  • z='2': Sets the zoom factor to a constant 2x.
  • x='500-(iw/(2*zoom))': Centers the X axis on coordinate 500.
  • y='300-(ih/(2*zoom))': Centers the Y axis on coordinate 300.
  • d=125: Sets the duration of the effect in frames (e.g., 5 seconds at 25 fps).
  • s=1920x1080: Defines the output resolution to prevent loss of video quality.

Creating a Dynamic Zoom-In Effect

To create a smooth, gradual zoom-in effect towards a specific coordinate over time, change the zoom parameter z to increase incrementally on every frame.

The following command zooms in smoothly toward coordinate \((1200, 800)\) up to a maximum zoom of 1.5x:

ffmpeg -i input.mp4 -vf "zoompan=z='min(zoom+0.003,1.5)':x='1200-(iw/(2*zoom))':y='800-(ih/(2*zoom))':d=150:s=1920x1080" -c:v libx264 output.mp4

How the Dynamics Work:

  • z='min(zoom+0.003,1.5)': Increases the zoom level by 0.003 per frame until it reaches the maximum limit of 1.5x.
  • The x and y equations automatically recalculate the crop box positions frame-by-frame as the zoom variable increases, keeping the camera dynamically focused on \((1200, 800)\).