How to Resize Video with FFmpeg
Resize video with FFmpeg: scale to a width or height and keep the aspect ratio, fit into a box with padding, crop to vertical, and do it with one API call.
To resize a video with FFmpeg and keep its aspect ratio, set one side and use -2 for the other: ffmpeg -i input.mp4 -vf scale=-2:720 -c:v libx264 -crf 23 -c:a copy output.mp4. To get an exact size such as 1080×1920 without stretching, scale and then pad or crop.
Below are the commands with each part explained, the same job as one Xora API call, and the common errors. For other common commands, see the FFmpeg cheat sheet.
The local command
ffmpeg -i input.mp4 -vf scale=-2:720 -c:v libx264 -crf 23 -preset medium -c:a copy -movflags +faststart output_720p.mp4| Part | What it does |
|---|---|
scale=-2:720 | Height 720, width from the aspect ratio, rounded to an even number. |
-c:v libx264 -crf 23 | A resize always re-encodes the video. CRF 23 is a good default. |
-c:a copy | Keep the audio as it is. |
To set the width in place of the height, use scale=1280:-2.
Exact sizes without stretching
Fit inside the size, with bars (letterbox)
ffmpeg -i input.mp4 -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1" -c:v libx264 -crf 23 -c:a copy output.mp4Fill the size and crop the edges (landscape to vertical)
ffmpeg -i input.mp4 -vf "scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920,setsar=1" -c:v libx264 -crf 23 -c:a copy vertical.mp4| Method | Result |
|---|---|
scale=W:H | Exact size. Stretches the video when the aspect ratio is different. |
decrease + pad | The whole picture, with black bars. |
increase + crop | No bars, but the edges are cut off. |
The same job with the Xora API
The resize recipe takes a width, a height or both. With one of them, the other side keeps the aspect ratio:
{ "mode": "recipe", "input": { "url": "https://example.com/talk_4k.mp4" }, "output": { "format": "mp4" }, "recipe": { "name": "resize", "height": 720 }}With both a width and a height, the recipe scales to that exact size, so a different aspect ratio is stretched. To pad or crop, send the filters in mode: "ffmpeg":
{ "mode": "ffmpeg", "input_files": { "in_1": "https://example.com/talk_4k.mp4" }, "output_files": { "out_1": "talk_vertical.mp4" }, "ffmpeg": { "args": [ "-i", "{{in_1}}", "-vf", "scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920,setsar=1", "-c:v", "libx264", "-crf", "23", "-c:a", "copy", "{{out_1}}" ] }}Send the body to https://api.xora.sh/v1/jobs with Authorization: Bearer YOUR_API_KEY, then poll GET /v1/jobs/{id} or pass webhookUrl. See the resize recipe.
Variants
- Make it smaller to send or upload: scale to 720p and raise the CRF a little. See how to compress video with FFmpeg.
- Portrait sources: check the orientation first. A phone video can be 1080×1920, and
scale=-2:720then makes it 405×720. Usescale=720:-2for portrait, or readwidthandheightwith a probe job. - Do not scale up: add
min:scale='min(1280,iw)':-2keeps small videos at their own size. - Edit proxies: for a low-resolution copy to edit with, use the proxy recipe.
Common errors
| Error | Cause | Fix |
|---|---|---|
width not divisible by 2 or height not divisible by 2 | -1 gave an odd size. | Use -2. |
| The video looks stretched | scale=W:H with a different aspect ratio. | Use force_original_aspect_ratio with pad or crop. |
| The video looks squeezed in some players | The sample aspect ratio is not 1:1 after a pad or crop. | Add setsar=1. |
| The Xora output is not the size you expected | The resize recipe needs a width, a height or both. | Send at least one of them. |
VALIDATION_ERROR (Xora) | The output name has a hyphen or a space. | Use letters, numbers and underscores, for example talk_vertical.mp4. |
Failed and rejected Xora jobs cost nothing. See error handling.
Related
- Resize recipe reference.
- FFmpeg cheat sheet: the common commands, each as an API call.
- How to compress video with FFmpeg.
Frequently asked questions
How do I resize a video with FFmpeg and keep the aspect ratio?
Set one side and use -2 for the other: ffmpeg -i input.mp4 -vf scale=-2:720 -c:v libx264 -crf 23 -c:a copy output.mp4. FFmpeg calculates the width from the aspect ratio and rounds it to an even number, which H.264 needs.
What is the difference between -1 and -2 in the FFmpeg scale filter?
Both keep the aspect ratio. -1 can give an odd number of pixels, which H.264 and many other encoders reject. -2 rounds to an even number, so it is the safe choice.
How do I resize a video to an exact size without stretching it?
Fit it inside the size and add bars: scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2. Or fill the size and cut the edges: scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920.
Can I resize a video without installing FFmpeg?
Yes. Send the video URL to the Xora API with the resize recipe and a width, a height or both. For padding or cropping, send your own scale, pad or crop filters in mode ffmpeg.