日本語

How to Create a Slow Zoom Video from a Photo with FFmpeg zoompan

​​FFmpeg​command line,images,video

The zoompan filter in FFmpeg can turn a single photo into a video that slowly zooms in. It is useful for adding motion to still images, such as photos in a slideshow or stills inside a larger video.

In my test environment, I made a video that zooms a 1920×1080 image to 1.5x over 5 seconds, and I measured the zoom factor and the position of the center of the frame for each frame. Depending on the options and the input settings, the zoom reached only 1.19x in 5 seconds, the video became 625 seconds long, or the frame wobbled slightly. I describe how to fix each of these, along with the measured numbers.

Note

The test environment is Ubuntu 24.04.5 with FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5). Instead of a real photo, I made a synthetic image of a sky and hills with Python (Pillow), and placed four black dots to measure positions. To measure the zoom factor, I found the centroid of the four dots in each output frame and compared the spacing between the dots with the original spacing. I did not test a real camera photo, panning (moving toward an edge of the frame), or a slideshow that joins several photos.

Command for a 1.5x zoom toward the center over 5 seconds

First, here is the finished command. It makes a 1280×720, 25 fps, 5-second video (125 frames), where the zoom factor rises at a constant rate from 1.0 to 1.5.

bash
ffmpeg -i photo.png -vf "zoompan=z='1+0.5*on/124':d=125:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':s=1280x720:fps=25" -c:v libx264 -pix_fmt yuv420p lin.mp4

The options and variables, as described in the official zoompan documentation, are as follows.

  • z: The zoom expression. Range 1 to 10, default is 1.
  • x, y: Expressions for the top-left position of the area to crop.
  • d: The number of frames to produce from one input image (default is 90).
  • s: The output size (default is hd720).
  • fps: The output frame rate (default is 25).
  • on: The number of the output frame (counting from 0).
  • iw, ih: The width and height of the input. zoom is the current zoom factor.

z='1+0.5*on/124' gives 1.0 at the first frame (on=0) and 1.5 at the last frame (on=124). x and y crop an area of width iw/zoom and height ih/zoom from the center of the image. The cropped area is then scaled up to size s to make one frame.

In the test environment, the command finished in 0.98 seconds. The resulting video was 1280×720, 125 frames, and 5.000000 seconds long. The zoom factor measured from the dot spacing was 1.0000 at frame 1, 1.2500 at frame 63, and 1.5001 at frame 125.

An image of a sky and hills that gradually grows larger. A band at the top shows the frame number and the zoom factor
The zoom from 1.00x to 1.50x over 125 frames (this is the video made by first enlarging 4x, which is introduced in a later section, thinned out to 12.5 fps. The zoom factors are values calculated from the expression)

To zoom out instead, reverse the expression to z='1.5-0.5*on/124'. The measured zoom factors also fell at a constant rate: 1.5000, 1.2500, and 1.0000.

With the official example expression, the zoom reaches only 1.19x in 5 seconds

The official documentation has an example that zooms toward the center using z='min(zoom+0.0015,1.5)'. This expression adds 0.0015 to the previous frame's zoom factor each time and stops at 1.5.

With this expression and d=125, frame 1 was 1.0016 and frame 125 was 1.1881. By frame 1, one addition has already been made. To reach 1.5x, it would take about 334 frames (0.5 ÷ 0.0015), or about 13 seconds at 25 fps. (This is calculated from the expression, not measured.)

A line graph with two lines. The expression using on reaches 1.5 at frame 125, while the expression that keeps adding to zoom ends at 1.19
Zoom factor per frame. The light blue line is the expression using on, and the gray line is the expression from the official example

If you want to decide the video length and the final zoom factor, it is better to write it as 1+(final zoom factor−1)×on/(d−1) using on. This way, the final zoom factor stays correct even when you change the length.

Adding -loop 1 to the input makes the video 625 seconds long

Adding -loop 1 -t 5 to the input changes the result a lot.

bash
ffmpeg -loop 1 -t 5 -i photo.png -vf "zoompan=z='min(zoom+0.0015,1.5)':d=125:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':s=1280x720:fps=25" -c:v libx264 -preset ultrafast -pix_fmt yuv420p loop.mp4

The resulting video had 15625 frames and was 625.000000 seconds long. In the test environment, it took 35.77 seconds to create.

d is the number of frames per input image. -loop 1 -t 5 makes the input 25 fps × 5 seconds = 125 images, and since 125 frames were made for each image, the result was 125 × 125 = 15625 frames. Looking at the frames, frames 123 to 125 zoomed to 1.1852, 1.1867, and 1.1881, and frame 126 went back to 1.0016. The video repeats a 5-second zoom 125 times.

When you need -loop 1, set d=1 so that one frame is made for each input image. However, with d=1 alone, the zoom factor stayed at 1.0016 and did not change. The official documentation describes zoom as "the zoom factor calculated last for the current input frame." In the source code of FFmpeg 6.1.1, zoom is reset to 1 each time the input image changes, so the amount added on the previous image is not carried over. The zoom factor from the previous input is stored in pzoom, so you can add to max(zoom,pzoom) as in the official example.

bash
ffmpeg -loop 1 -t 5 -i photo.png -vf "zoompan=z='min(max(zoom,pzoom)+0.0015,1.5)':d=1:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':s=1280x720:fps=25" -c:v libx264 -pix_fmt yuv420p loop_pz.mp4

This makes 125 frames and 5 seconds. The measured zoom factors were the same at every frame as those of the video made with d=125 and without -loop 1.

If the aspect ratio of the photo differs from s, the image stretches sideways

zoompan scales the cropped area directly to size s. When I used s=1280x720 (16:9) with a 4:3 photo (1600×1200), the image was stretched horizontally. A round dot 21 pixels in diameter in the original image became a wide oval measuring 17 pixels wide and 12 pixels high in frame 1.

To keep the original aspect ratio, use the crop filter before zoompan to cut the photo to 16:9, the same as s. If you do not specify a position, crop takes the center.

bash
ffmpeg -i photo43.png -vf "crop=1600:900,zoompan=z='1+0.5*on/124':d=125:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':s=1280x720:fps=25" -c:v libx264 -pix_fmt yuv420p a43crop.mp4

In this case, the dot stayed round at 17 pixels wide and 17 pixels high, and the ratio of horizontal to vertical spacing between the dots was 1.333, the same as in the original image. Since 150 pixels are cut from the top and the bottom, adjust the crop position for photos where the important content is near the edges. If you can keep the video at 4:3, setting s to the same ratio as the photo, for example s=960x720, also kept the ratio at 1.333.

A comparison of two images stacked vertically. The top shows the sun and black dots stretched into wide ovals, and the bottom keeps them round
Frame 1 of the video made from a 4:3 photo. The top is as-is, and the bottom is cropped to 16:9 first

If the frame wobbles finely, enlarge the image first before passing it on

In the videos made with the commands so far, the center of the frame wobbled left-right and up-down by up to about 2 pixels while slowly zooming in. In the zoompan source code of FFmpeg 6.1.1, the position and size of the cropped area are determined in whole pixel units. The zoom factor changes smoothly, but the crop position moves only in 1-pixel steps. This rounding appears as wobble in the output, because it is enlarged.

One way to fix this is to enlarge the image before passing it to zoompan. Since the input pixels become finer, the 1-pixel rounding becomes smaller in the output. Here I enlarged 1920×1080 by 4x to 7680×4320.

bash
ffmpeg -i photo.jpg -vf "scale=7680:-1,zoompan=z='1+0.5*on/124':d=125:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':s=1280x720:fps=25" -c:v libx264 -pix_fmt yuv420p smooth.mp4

I measured how far the center of the four dots (which should stay at the center of the frame) moved over the 125 frames. The input was the same image saved as JPEG (yuvj420p).

Method Horizontal wobble range Vertical wobble range Maximum movement per frame Time taken
zoompan as-is 2.13 px 2.04 px 1.72 px horizontal, 1.55 px vertical 0.89 s
zoompan after 4x enlargement 0.64 px 0.63 px 0.40 px horizontal, 0.49 px vertical 2.49 s

The wobble range dropped to about one third. With a PNG image, 2.11 px became 0.65 px, which was nearly the same result.

A line graph of the horizontal wobble of the center of the frame, frame by frame. The gray line swings widely between 0.3 and 2.5, and the light blue line stays between 0.3 and 1.0
Horizontal wobble of the frame center per frame. Light blue is with the 4x enlargement first, and gray is as-is

The trade-off is that the time taken increased from 0.89 seconds to 2.49 seconds, about 2.8 times longer.

​Posted by 86