Sub topic 10
Animating zoom in and out
A static zoom, like the one built with crop and scale earlier in this tutorial, jumps from the full frame to the magnified view in a single frame. Viewers read that jump as a cut, and sometimes as a glitch. This chapter replaces the jump with a short, smooth transition in and out.
It assumes you can already do a static zoom (crop a region, scale it back to full size) and that you understand the timeline and playhead. The ffmpeg examples assume a 1920x1080 source at 30 frames per second.
What a zoom animation is made of
An animated zoom has four parts:
- Start state: the full frame, at a zoom of 1.0.
- Ramp in: the zoom level rises to the target over a short time.
- Hold: the zoom stays constant while the viewer reads the detail.
- Ramp out: the zoom returns to 1.0.
Editors expose this as keyframes. A keyframe is a value you set at a specific moment, such as "scale = 100% at 0:20.0" and "scale = 150% at 0:21.0". The editor calculates every frame in between. This is a different idea from the codec keyframes in the earlier chapter on re-encoding. A codec keyframe is a fully stored picture. An animation keyframe is a value you choose. Both meanings appear in editor documentation, so check which one a given manual means.
Choosing durations
Timing decides whether the zoom feels purposeful or distracting.
- Ramp length: 0.5 to 1 second works for most lectures. Shorter feels like a snap. Longer than about 1.5 seconds makes the viewer wait for the detail.
- Hold length: at least 2 seconds, and long enough to cover the whole explanation of the detail.
- Zoom amount: 1.3x to 1.6x is typical. Beyond 2x on a 1080p source, text starts to look soft, because you are enlarging pixels that were never recorded at that size.
- Trigger: begin the ramp slightly before the narrator mentions the detail, about 0.3 seconds early. The eye then arrives at the same time as the word.
Easing: how the value changes between keyframes
Easing is the shape of the change between two keyframes. With linear easing the zoom rises at a constant rate. The motion starts and stops abruptly, which looks mechanical. Ease in-out starts slowly, speeds up, and settles slowly. It looks much more natural for zooms.
| Easing | Behavior | Typical use |
|---|---|---|
| Linear | Constant speed, abrupt start and stop | Slow, continuous drift over a long hold |
| Ease in | Starts slowly, ends fast | Rarely used for zooms |
| Ease out | Starts fast, settles gently | Zooming in on a detail |
| Ease in-out | Slow start, fast middle, slow end | Default choice for in and out |
A common ease in-out curve is smoothstep: for progress p between 0 and 1, the eased value is p * p * (3 - 2 * p). The curve is flat at both ends, so there is no sudden start or stop.
This small Python script prints the zoom level every half second for a 10-second clip with a 1-second ramp and a 1.5x peak. You can use the numbers as manual keyframes in an editor that lacks easing controls.
def smoothstep(p):
return p * p * (3 - 2 * p)
fps = 30
frames = 300 # 10 s at 30 fps
ramp = 30 # 1 s ramp
peak = 1.5
for on in range(0, frames, 15):
p = min(1.0, on / ramp, (frames - on) / ramp)
zoom = 1 + (peak - 1) * smoothstep(p)
print(f"t={on / fps:4.1f}s zoom={zoom:.3f}")The output rises from 1.000 to 1.500 over the first second, stays at 1.500, and falls back near 1.000 at the end.
Animating a zoom with ffmpeg
The static example used crop followed by scale. That approach cannot be animated. The crop filter evaluates its width and height once, when the filter starts, so the crop size cannot change from frame to frame. Only its x and y position can change per frame. Use the zoompan filter instead. It evaluates a z (zoom) expression for every frame and uses x and y expressions to place the visible window.
ffmpeg -ss 20 -t 10 -i lecture.mp4 \
-vf "scale=3840:2160,zoompan=z='st(0,min(1,min(on/30,(300-on)/30)));1+0.5*ld(0)*ld(0)*(3-2*ld(0))':x='max(0,min(0.68*iw-iw/zoom/2,iw-iw/zoom))':y='max(0,min(0.37*ih-ih/zoom/2,ih-ih/zoom))':d=1:s=1920x1080:fps=30,format=yuv420p" \
-c:v libx264 -crf 18 -c:a aac zoom_anim.mp4How this works:
-ss 20 -t 10takes a 10-second segment starting at 20 s. At 30 fps that is 300 frames.onis the output frame number.on/30is therefore the time in seconds since the segment started.min(1, min(on/30, (300-on)/30))is the progressp. It rises from 0 to 1 over the first second, stays at 1, and falls back to 0 over the last second.st(0, ...)stores it in slot 0 andld(0)reads it back, so the long expression is not repeated.1+0.5*p*p*(3-2*p)applies smoothstep and scales the result to a zoom between 1.0 and 1.5.xandykeep the visible window centered on a focus point at 68% across and 37% down the frame. Themax(0, min(...))wrapper stops the window from leaving the picture.d=1makeszoompanproduce one output frame per input frame. Without it, each input frame is repeated for many output frames.scale=3840:2160runs first.zoompanrounds its window position to whole pixels, which makes slow movement jitter. Working on an upscaled copy makes each rounding step half as large on screen.
Because this re-renders every frame, the video is always re-encoded. Re-encoding is why the command specifies -c:v libx264, and -crf 18 keeps quality high. You cannot use stream copy for a clip that contains an animated zoom.
Common problems
Jitter or shaking
Jitter comes from the integer rounding of the window position. Upscale before zoompan, as in the example, and avoid moving the focus point while the zoom is changing. Panning and zooming at the same time doubles the chance of visible stepping.
The focus drifts
If the window is centered on the frame center while the detail is off to one side, the detail slides out of view as the zoom grows. Anchor the window on the detail itself, using the focus fractions in the x and y expressions, so it stays in the same relative place during the ramp.
Soft text at the hold
Zooming a 1080p recording to 1.5x displays about 1280x720 real pixels. If the lecture is a screen recording, record at a higher resolution or use a lower zoom. Text is the first thing to look soft.
Audio and sync
The zoom changes only the picture. As long as you do not trim the audio separately, it stays in sync. If the zoomed segment is part of a longer edit, apply the cuts first, then the zooms, so that the zoom times refer to the final timeline. Removing 8 seconds earlier in the video shifts every later zoom by 8 seconds.
Interaction with captions
Burned-in captions drawn before the zoom are enlarged with the picture and can leave the frame. Apply the zoom first and burn the captions in last. Separate caption tracks are unaffected, because players draw them on top of the final picture.
Applying this in a graphical editor
Most editors follow the same steps:
- Place the playhead where the ramp starts and add a scale keyframe at 100%.
- Move the playhead 0.5 to 1 second later and set the scale to your target, for example 150%, plus position values that center the detail.
- Add a keyframe at the end of the hold with the same values.
- Add a final keyframe 0.5 to 1 second later, back at 100%.
- Select the keyframes at the ramps and apply ease in-out, often called Bezier or smooth.
- Scrub through the ramps and the hold to check for jitter and cropped content.
Exercise
You need a zoom on a 12-second clip at 30 fps. The zoom should reach 1.8x, with 2-second ramps in and out. Which numbers in the ffmpeg zoom expression change, and what is the new expression for z?
Show answer
The clip has 12 x 30 = 360 frames, and a 2-second ramp is 60 frames. Replace 300 with 360 and 30 with 60 in the progress term. The peak increment becomes 0.8. The expression is z='st(0,min(1,min(on/60,(360-on)/60)));1+0.8ld(0)ld(0)(3-2ld(0))'.
Exercise
Why can't you animate a zoom by putting an expression such as crop=w='iw/(1+t/10)' in the crop filter?
Show answer
The crop filter evaluates its output width and height once, at initialization, so they cannot vary per frame. Only the x and y position are evaluated per frame. A filter that evaluates the zoom per frame, such as zoompan, is needed.