Sub topic 9
Adding a zoom with crop and scale
This chapter explains how a zoom works at the pixel level and how to build one with ffmpeg. It assumes you can run a command in a terminal and know what a video frame, resolution, and an MP4 file are. It also assumes you have read the earlier chapter on why a zoom helps the viewer, so here we only cover the mechanics.
The working model: a zoom is a crop plus a scale
A video editor's "zoom" button hides two operations:
- Crop keeps a rectangle from the frame and discards everything outside it.
- Scale resizes that rectangle back to the full output size, for example 1920x1080.
The cropped region is smaller than the output, so scaling stretches it. The magnification is the ratio of the output size to the crop size. A 1280x720 crop scaled to 1920x1080 gives a 1.5x zoom.
Order matters. If you scale first and crop afterward, you lose the benefit of a sharper result and waste processing time on pixels you throw away. Always crop first.
Choosing the crop rectangle
A crop is defined by four numbers: width, height, and the x and y position of the top-left corner. In ffmpeg the filter is crop=w:h:x:y, and you can use the variables iw and ih for the input width and height.
Keep the crop at the same aspect ratio as the output, which is 16:9 for most lectures. Otherwise the scale step stretches the picture and circles turn into ovals. Cropping iw/factor by ih/factor keeps the ratio automatically.
| Zoom factor | Crop size on a 1920x1080 frame | Output pixels per source pixel |
|---|---|---|
| 1.25x | 1536x864 | 1.25 |
| 1.5x | 1280x720 | 1.5 |
| 2x | 960x540 | 2 |
| 3x | 640x360 | 3 |
The crop must stay inside the frame. Check that x + w <= iw and y + h <= ih. If the rectangle extends past the edge, ffmpeg stops with an error about invalid parameters.
To center the crop, use (iw-ow)/2 for x and (ih-oh)/2 for y. Here ow and oh are the crop's own width and height, which the crop filter lets you reference. To aim at a specific area, such as a code block in the lower right of a slide, compute x and y from where that area starts.
Example: zoom a 10-second segment
This command extracts a segment and zooms into it:
ffmpeg -ss 20 -t 10 -i lecture.mp4 \
-vf "crop=iw/1.5:ih/1.5:iw/4:ih/4,scale=1920:1080" \
-c:v libx264 -c:a aac zoomed_segment.mp4Here is what each part does:
-ss 20 -t 10placed before-istarts reading at 20 seconds and keeps 10 seconds.crop=iw/1.5:ih/1.5:iw/4:ih/4keeps a 1280x720 rectangle whose top-left corner is at x=480, y=270.scale=1920:1080enlarges that rectangle to the full frame.-c:v libx264 -c:a aacre-encodes the video and audio.
This example is not centered. A centered 1280x720 crop on a 1920x1080 frame starts at x=320, y=180. The offsets iw/4 and ih/4 shift the region toward the lower right. That is useful when the content you want to emphasize is there, but it is easy to mistake for a centered crop.
Zooming only part of a longer video
The command above produces only the 10-second segment. A lecture usually needs the zoom applied to a stretch in the middle while the rest plays unchanged. The crop filter has no time-based switch, so a common approach is to create a zoomed copy of the whole video stream and overlay it only during the chosen interval. The overlay filter does support time-based control through its enable option.
ffmpeg -i lecture.mp4 \
-filter_complex "[0:v]split[base][z];[z]crop=iw/1.5:ih/1.5:iw/4:ih/4,scale=1920:1080[zoomed];[base][zoomed]overlay=enable='between(t,20,30)'[v]" \
-map "[v]" -map 0:a \
-c:v libx264 -crf 18 -pix_fmt yuv420p -c:a copy \
lecture_zoom.mp4How it works:
splitduplicates the video stream intobaseandz.- The
zcopy is cropped and scaled, and the result is labeledzoomed. overlaydrawszoomedon top ofbase. Theenableexpression turns the overlay on only when the timetis between 20 and 30 seconds.-map "[v]" -map 0:aselects the new video and the original audio.
Because both streams keep the original timestamps, the audio stays in sync. The audio is copied without re-encoding, which saves time and avoids quality loss. The video must be re-encoded because its pixels changed.
The zoomed overlay is hard-coded at 1920x1080 in this example. If your source has a different resolution, change the scale values to match it, or the overlay will not cover the base frame.
Common errors and how to avoid them
Blurry result. Scaling up cannot create detail. A 2x zoom on a 1080p recording shows each source pixel as a 2x2 block of interpolated pixels, so small text turns soft. Record at 4K when you plan to zoom, or limit zooms to 1.5x on 1080p. Adding :flags=lanczos to the scale filter, as in scale=1920:1080:flags=lanczos, gives a sharper result than the default bicubic setting.
Distorted shapes. This happens when the crop aspect ratio differs from the output ratio. Compute both crop dimensions from the same factor.
Crop outside the frame. Check x + w <= iw and y + h <= ih before running. Using iw and ih expressions instead of fixed numbers makes the command work on any resolution.
Cutting off what you wanted to show. Crop rectangles are easy to place too tightly. Leave a margin around the target area, and play the result to confirm that the text is complete.
Player compatibility. Some players refuse H.264 video in a non-standard pixel format. Adding -pix_fmt yuv420p avoids this. H.264 in this format also needs even width and height, so choose crop sizes that are even numbers.
Audio drift after trimming. If you use -ss before -i with copy mode, ffmpeg can only start at a keyframe and the cut may be slightly off. Re-encoding the video, as these examples do, makes the start frame-accurate. The earlier chapter on keyframes explains why.
Checking the result
Open the output and check each of these:
- The zoomed region fills the frame with no black bars.
- Text in the zoomed area is readable at the size a learner will see it.
- The zoom starts and ends at the intended times, and the speaker's words still match what is on screen.
- Nothing important sits near the edge of the crop.
Exercise
Your source is 1920x1080. Write the filter chain that zooms 2x into the lower-right quadrant and outputs a full 1920x1080 frame. Then say why the crop is valid.
Show answer
crop=iw/2:ih/2:iw/2:ih/2,scale=1920:1080. The crop is 960x540 with its top-left corner at x=960, y=540. Since 960 + 960 = 1920 and 540 + 540 = 1080, the rectangle ends exactly at the frame edge, so it stays inside the frame. The 16:9 ratio is kept, so the scale step does not distort the picture.
Exercise
Why does the single-pass crop and scale command in this chapter not work for zooming only seconds 20 to 30 of a full lecture, and what filter lets you limit the zoom to that interval?
Show answer
The crop filter applies to every frame it receives and has no time-based switch, so the whole video would be zoomed (or only the extracted segment would be kept). Splitting the stream, zooming one copy, and combining them with the overlay filter works because overlay accepts enable='between(t,20,30)', which shows the zoomed copy only in that interval.