Crop a video with FFmpeg

Use -vf crop=w:h:x:y. The first two numbers are the size to keep, the last two are where its top-left corner sits. Leave out x:y and FFmpeg takes the middle of the frame. Crop always re-encodes the video, and the audio can be copied as it is.

Region Re-encode

You know the exact rectangle to keep, in pixels.

ffmpeg -i in.mp4 -vf crop=480:270:560:40 -c:a copy out.mp4

480x270, the area that starts 560 px from the left and 40 px from the top. Audio copied.

Size out
480x270
Corner
x 560, y 40
Run time
384 ms
Output
552 KB
Vertical Re-encode

You need a 9:16 clip for phones out of a wide video.

ffmpeg -i in.mp4 -vf crop=ih*9/16:ih:640:0 -c:a copy out.mp4

306x546, full height, starting 640 px in where the girl stands. ih*9/16 is 307.1, and crop rounded it to 306.

Size out
306x546
Kept
24% of the width
Run time
390 ms
Output
749 KB

Proof

Before and after, same frame

Frame 72 of the clip, 3 s in. The first picture marks three crops on the input: yellow is the region, red the centred 9:16, green the 9:16 moved onto the girl. Narrow outputs sit on a dark canvas so they line up.
in.mp4
The input frame with a yellow, a red and a green crop rectangle drawn over it
1280x546 The girl stands right of centre, from about x 680 to x 920.
crop=480:270:560:40 The yellow box
The 480x270 crop: the girl's head and the wall behind her
480x270 Shown at twice its size. Exactly the pixels inside the yellow rectangle.
crop=ih*9/16:ih Cuts her face in half
Centred 9:16 crop that cuts the girl's face in half at the right edge
306x546 No x:y, so FFmpeg centres the crop at x 487. The subject is off centre and falls out of the frame.
crop=ih*9/16:ih:640:0 Subject in frame
9:16 crop starting at x 640 with the girl fully in frame
306x546 The same size, moved 153 px right. Pick x from a frame of your own footage.
letterbox.mp4
The clip inside a 16:9 frame with black bars above and below
1280x720 86 rows of black on top, 88 at the bottom. The picture fills rows 86 to 631.
crop=1280:546:0:86 Bars gone, no picture lost
The same frame with the black bars cropped away
1280x546 The value cropdetect=round=2 settled on. The first and last rows are picture, not black.

Footage: Sintel, © Blender Foundation, CC BY 3.0.

Run it

Run it on Rendobar

Send the same command from your code. The job fetches each input from its URL and returns the output file's URL when it finishes.

job.ts
import { createClient } from "@rendobar/sdk";
const rb = createClient({ apiKey: process.env.RENDOBAR_API_KEY });
const job = await rb.jobs.run({
type: "ffmpeg",
inputs: {
"in.mp4": "https://cdn.rendobar.com/examples/crop-video/in.mp4",
},
params: {
command: "ffmpeg -i in.mp4 -vf crop=ih*9/16:ih:640:0 -c:a copy out.mp4",
},
});
console.log(job.output.file.url);

Install with npm i @rendobar/sdk. jobs.run() submits and waits, so it returns the finished job in one call.

terminal
curl https://api.rendobar.com/jobs \
-H "Authorization: Bearer $RENDOBAR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "ffmpeg",
"inputs": {
"in.mp4": "https://cdn.rendobar.com/examples/crop-video/in.mp4"
},
"params": {
"command": "ffmpeg -i in.mp4 -vf crop=ih*9/16:ih:640:0 -c:a copy out.mp4"
}
}'

Returns immediately with a job id. Poll GET /jobs/{id} or register a webhook rather than blocking on the request.

Every variant

Pick the command for your case

Each one ran on the same input. The line under it is what came out. Run times are the best of three runs with FFmpeg 8.0 on one desktop machine, so read them as relative.

  1. Crop the centre to 9:16

    Re-encode
    ffmpeg -i in.mp4 -vf crop=ih*9/16:ih -c:a copy out.mp4

    306x546 from the middle of the frame, x 487. Fine when the subject is centred. Here it cut the girl in half.

    385 ms run 740 KB 10.00 s long

  2. Crop to a centred square

    Re-encode
    ffmpeg -i in.mp4 -vf crop=ih:ih -c:a copy out.mp4

    546x546, the middle of the frame. For a wide video the height is the short side.

    542 ms run 1.1 MB 10.00 s long

  3. Crop a wider video to 16:9

    Re-encode
    ffmpeg -i in.mp4 -vf crop=ih*16/9:ih -c:a copy out.mp4

    970x546, 155 px off each side of the 2.35:1 frame.

    759 ms run 1.5 MB 10.00 s long

  4. Trim 50 px off every edge

    Re-encode
    ffmpeg -i in.mp4 -vf crop=iw-100:ih-100 -c:a copy out.mp4

    1180x446. Centred, so 50 px go from each side.

    738 ms run 1.4 MB 10.00 s long

  5. Crop to 9:16 and scale to 1080x1920

    Re-encode
    ffmpeg -i in.mp4 -vf crop=ih*9/16:ih:640:0,scale=1080:1920,setsar=1 -c:a copy out.mp4

    1080x1920 with square pixels. The 306 px crop is blown up 3.5 times, so it is soft. Crop from the highest resolution you have.

    2.6 s run 3.9 MB 10.00 s long

  6. Find the black bars

    Read only
    ffmpeg -i letterbox.mp4 -vf cropdetect=round=2 -f null -

    Prints a crop= line per frame. It settled on crop=1280:546:0:86 once the shot brightened, 5 s in. Read the last line.

    158 ms run

  7. Remove the black bars

    Re-encode
    ffmpeg -i letterbox.mp4 -vf crop=1280:546:0:86 -c:a copy out.mp4

    1280x546. Rows 86 and 631 of the input were the first and last rows of picture, so nothing was lost.

    940 ms run 1.7 MB 10.00 s long

  8. Crop without re-encoding

    Stream copy
    ffmpeg -i in.mp4 -c copy -bsf:v h264_metadata=crop_left=640:crop_right=334 out.mp4

    Reads as 306x546 in ffprobe and in Chrome. The file is as big as the input, because the full frame is still in it.

    82 ms run 3.1 MB 10.00 s long

Gotchas

Where it goes wrong

How do the four crop numbers work?

crop=w:h:x:y. w and h are the size of the area you keep. x and y are the position of its top-left corner, counted from the top-left of the frame. Leave out x and y and the area is centred.

Each value can be an expression. iw and ih are the input width and height, so crop=iw/2:ih:0:0 keeps the left half and crop=iw-100:ih-100 trims 50 px from every edge.

A centred crop is a guess about where the subject is. In the proof above the centred 9:16 cut the girl in half. Grab a frame, find the subject's position, and set x yourself:

ffmpeg -ss 3 -i in.mp4 -frames:v 1 frame.png

Can I crop without re-encoding?

Not with the crop filter. Any filter means decoding and encoding the video again. For H.264 there is a bitstream filter that writes crop values into the stream header instead, and leaves the picture data alone:

ffmpeg -i in.mp4 -c copy -bsf:v h264_metadata=crop_left=640:crop_right=334 out.mp4

ffprobe reported 306x546, and Chrome played it at 306x546 with the girl in frame. The file was 3.1 MB, the same as the input. The re-encoded crop of the same area was 0.75 MB.

The cropped-away pixels are still in the file and still downloaded. Use it when you need the original quality and speed matters more than size. HEVC has the same thing as hevc_metadata.

Why does cropdetect cut off part of my picture?

Two reasons, both seen on the letterboxed test clip.

Dark scenes read as bars. The first 5 s of the clip have a dark cloth on the left, and cropdetect reported crop=848:544:428:88 there, which would throw away a third of the width. Only when the shot brightened did it settle on the full width. Run it over the whole video and use the last value it prints, or skip into a bright scene with -ss.

The default round=16 makes the size a multiple of 16. That gave crop=1280:544:0:88, two rows short, and both of the missing rows were picture. cropdetect=round=2 gave crop=1280:546:0:86, which matches the picture row for row.

Do the crop sizes have to be even?

No, crop rounds them for you. On a 1920x1080 test pattern, crop=607:1080 came out 606 wide and crop=ih*9/16:ih (607.5) came out 608. The encoder never saw an odd width.

The catch is that the output is a pixel off what you typed. If you need the exact size, use even numbers. exact=1 turns the rounding off, and then libx264 stopped with width not divisible by 2 (607x1080).

Related: Resize a video, Rotate a video, Stack videos.

Your command. Our infrastructure.

Bring your own commands. Run them on scalable CPU and GPU infrastructure, and pay only for compute time.