FFmpeg Zoompan: Zoom to Specific Video Coordinates

This article explains how to use the FFmpeg zoompan filter to zoom into a specific X and Y coordinate on a video. You will learn the mathematical logic behind targeting precise coordinates, how to write the correct FFmpeg command for static zooms, and how to create a smooth, animated zoom effect targeting a specific point.

The Math Behind Zooming to a Coordinate

The zoompan filter works by cropping a portion of the input video and scaling it back up to the output resolution.

To zoom into a specific target coordinate \((T_x, T_y)\) on an input video, you cannot just set the filter's x and y parameters to those exact coordinates. Instead, x and y define the top-left corner of the zoomed bounding box.

To center your zoom on target coordinates \((T_x, T_y)\), use the following formulas:

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

Where:

  • iw and ih are the input video width and height.
  • zoom is your desired zoom factor (e.g., 2 for 2x zoom).

Example 1: Static Zoom to a Specific Point

If you want to zoom in instantly by 2x and focus on the coordinates (500, 300) on a 1920x1080 video, use the following command:

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

Example 2: Smooth Animated Zoom to a Specific Point

To gradually zoom into the coordinates (500, 300) over a period of 150 frames, you must dynamically increase the zoom factor z using the frame counter variable on.

The following command gradually increases the zoom level from 1x to 2x, while dynamically adjusting the x and y parameters to keep the point (500, 300) perfectly centered:

ffmpeg -i input.mp4 -vf "zoompan=z='min(zoom+0.01,2)':x='500-(iw/(2*zoom))':y='300-(ih/(2*zoom))':d=150:s=1920x1080:fps=30" output.mp4

Key Parameter Breakdown

  • z (Zoom): Sets the zoom factor. min(zoom+0.01,2) increases the zoom by 0.01 per frame until it reaches a maximum of 2x.
  • x & y: The top-left position of the zoom box. Using the zoom variable dynamically centers the box as it shrinks.
  • d (Duration): The number of frames the zoom effect runs. For a 5-second zoom at 30 fps, set this to 150.
  • s (Size): The output resolution. If omitted, zoompan will default to a low 1280x720 resolution. Always match your input resolution (e.g., 1920x1080) to maintain quality.
  • fps: Explicitly set this to match your input video's frame rate (e.g., fps=30 or fps=60) to prevent the output video from stuttering.