Configure Zoom Factor and Pan Speed in FFmpeg

This article provides a quick guide on how to configure the zoom factor and panning speed using the FFmpeg zoompan video filter. You will learn the core parameters, how to manipulate the zoom expression, and how to control the direction and velocity of the panning camera effect.

Understanding the Zoompan Filter Syntax

The zoompan filter works by cropping a portion of the input frame and scaling it back to the output size. The basic syntax is:

zoompan=z='zoom_expr':x='x_expr':y='y_expr':d='duration':s='size'
  • z (Zoom): Sets the zoom factor expression (default is 1).
  • x and y (Pan): Sets the coordinate expressions for the top-left corner of the zoomed viewport.
  • d (Duration): Specifies the duration of the effect in frames (default is 90).
  • s (Size): Sets the output resolution (default is 1280x720).

Configuring the Zoom Factor

The zoom factor is set using the z parameter. It accepts values starting from 1.0 (no zoom). To create a dynamic zoom effect, you must write an expression that changes the zoom level incrementally on every frame.

  • Zoom In Speed: To zoom in slowly, increment the current zoom value (zoom) by a small fraction per frame:

    z='zoom+0.002'

    Increasing the increment (e.g., 0.005) increases the zoom speed.

  • Limiting the Max Zoom: To prevent infinite zooming, use the min or limit functions:

    z='min(zoom+0.001,1.5)'

    This caps the zoom factor at 1.5x.


Configuring Panning Speed and Direction

Panning is controlled by the x (horizontal) and y (vertical) expressions, which calculate the offset of the zoomed crop area for each frame.

By default, the zoom anchor is the top-left corner (0,0). To pan across the frame, you change x and y relative to the current zoom level.

1. Zooming to the Center (No Pan)

To keep the zoom centered, you must dynamically shift the crop coordinates x and y as the zoom factor increases:

x='iw/2-(iw/zoom)/2':y='ih/2-(ih/zoom)/2'
  • iw and ih represent the input width and height.
  • This formula keeps the zoomed frame perfectly centered.

2. Panning Left to Right

To pan the camera to the right while zooming, increase the x coordinate offset manually. The speed of the pan is determined by the value added to x per frame:

x='(zoom-1)*iw/zoom':y='ih/2-(ih/zoom)/2'

To adjust the speed of the pan independently of the zoom, you can add a static multiplier or increment:

x='x+2':y='ih/2-(ih/zoom)/2'

(Note: Ensure that x does not exceed iw - ow to avoid rendering errors).


Complete Command Example

The following command applies a slow, continuous zoom-in to the center of a video, lasting for 150 frames:

ffmpeg -i input.mp4 -vf "zoompan=z='zoom+0.0015':x='iw/2-(iw/zoom)/2':y='ih/2-(ih/zoom)/2':d=150" -c:a copy output.mp4

To speed up the zoom, change 0.0015 to a higher number like 0.003. To slow it down, decrease it to 0.0005.