mediacodec.dev

Crop a video

Cropping cuts pixels away; scaling squeezes them. Turning a landscape video into a vertical or square one means cropping, and the trick worth learning is writing the crop in terms of the source’s own height (ih), so one command works on footage of any resolution.

Target shape

crop

crop=ih*9/16:ih. The phone-screen shape: keeps the full height, cuts the sides away.

The expressions use ih, the input height, so the crop scales with the footage. When x and y are not given, the crop filter centres the rectangle for you.

Quality (CRF)

-crf
bettersmaller20

CRF 20: high-quality delivery.

Cropping changes the pixels, so the video re-encodes. x264’s 0–51 scale; 18–23 is the useful range.

Your command

ffmpeg -i input.mp4 \
  -vf "crop=ih*9/16:ih" \
  -c:v libx264 -crf 20 \
  -preset medium -c:a copy \
  output.mp4

Worth knowing

The full form is crop=w:h:x:y; leave x and y off and the filter centres the crop, which is what these expressions rely on.

To aim the crop off-centre (a speaker standing left of frame), add the position: crop=ih*9/16:ih:200:0 starts 200px from the left edge.

Crop dimensions should be even numbers: H.264 with 4:2:0 chroma subsampling cannot encode odd widths or heights.

Run this on your own file

no FFmpeg · runs in this tab

The command above needs FFmpeg on a machine. The settings it encodes do not: your browser can perform the same operation on a real file, right here, through WebCodecs.

Drop a video or audio file here, or

Files are processed in this tab and never uploaded.

The centred vertical 9:16 rectangle is computed from your file's own dimensions, exactly what the ih-based expression does.

WebCodecs has no CRF, so -crf 20 becomes a bitrate target for the output resolution, using the rule that every 6 CRF steps halve or double the size.

Waiting on a file.