How do I resize a video to make it smaller with FFmpeg? (e.g. from 1024x576 to 720x480.)
3 Answers
The most basic example is this:
ffmpeg -i input.avi -s 720x480 -c:a copy output.mkv
Using the scale
filter will provide more flexibility:
ffmpeg -i input.avi -filter:v scale=720:-1 -c:a copy output.mkv
The -1
will tell ffmpeg to automatically choose the correct height in relation to the provided width to preserve the aspect ratio. -1
can also be used for width if you provide a given height.
One downside of scale when using libx264
is that this encoder requires even values and scale may automatically choose an odd value resulting in an error: width or height not divisible by 2
. You can tell scale to choose an even value for a given height (720 in this example):
scale="trunc(oh*a/2)*2:720"
...or a given width (1280 in this example):
scale="1280:trunc(ow/a/2)*2"
Note that your ffmpeg build could complain about not recognizing -c
or -filter
options. It also may not support scale
. In that case you should use a newer ffmpeg, which you can download as a static build, or compile yourself.
-
11After digging through the man page for ffmpeg several times I found that instead of
-filter:v
, the option appears to have been changed to-vf
. Commented Apr 26, 2013 at 15:51 -
6@doug65536
-filter:v
and-vf
are both fine. The Ubuntu version you were using is broken and outdated—it's not really FFmpeg'sffmpeg
, but the binary supplied by Libav (the FFmpeg fork) under the same name.– slhckCommented Jul 26, 2013 at 6:41 -
5'-c' is short for -codec, ':a' specifies audio stream(s), 'copy' specifies the copy codec, which copies the stream(s) (in this case audio) without reencoding. Basically the audio streams will pass through. This is common when you are only manipulating the video stream.– jimharkCommented Feb 5, 2016 at 23:27
-
25You can tell scale to provide even value by using
scale=720:-2
Commented May 30, 2018 at 17:38 -
2The syntax of
scale
with a negative size (which asks a divisibility and keep ratio approximately) may seem practical, but i prefer the scale optionsforce_original_aspect_ratio=decrease
andforce_divisible_by=2
along with one or two dimensions, because it is much more obvious than relying on the negative arbitrary convention. I have no idea of which option belongs to which ffmpeg version so i do not know how relevant it is. Commented Sep 16, 2021 at 23:23
I use following commands to do rescaling for videos and images. For fixed width and height -
ffmpeg -i input.avi -vf scale="720:480" output.avi
and if you want to retain aspect ratio just give height as -1 and it will automatically resize based on the width -
ffmpeg -i input.avi -vf scale="720:-1" output.avi
If you want to scale based on input size e.g. lets say reduce the width/height to half you can do -
ffmpeg -i input.avi -vf scale="iw/2:ih/2" output.avi
NOTE :
iw : input width
ih : input height
Static build can be downloaded from - https://johnvansickle.com/ffmpeg/
Documentation : https://ffmpeg.org/ffmpeg.html#filter_005foption
-
-
I got a terrible video quality with this command. Much better if I create output.mp4, not avi. So extension matters.– sekrettCommented Oct 4, 2019 at 20:12
-
-
@AlexeySh. Try
-2
instead of-1
andfloor(iw/4)*2:floor(ih/4)*2
instead ofiw/2:ih/2
. Commented Feb 20 at 21:32
Using your GPU instead of CPU:
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i "input_file.mp4"
-c:a copy -vf "scale_cuda=-2:480" -c:v h264_nvenc "output_file.mp4"
You'll need an NVIDIA graphics card that supports CUDA hardware acceleration.
This worked for me (in Feb. 2024) using FFmpeg v6.0-essentials (from gyan.dev) on Windows 10 Pro with a NVIDIA GeForce GTX 960.
Details:
- From the NVIDIA FFmpeg Transcoding Guide:
-hwaccel cuda
chooses appropriate hw accelerator-hwaccel_output_format cuda
keeps the decoded frames in GPU memory-c:v h264_nvenc
selects the NVIDIA hardware accelerated H.264 encoderWithout the
-hwaccel cuda -hwaccel_output_format cuda
option, the decoded raw frames would be copied back to system memory via the PCIe bus, shown in figure 3. Later, the same image would be copied back to GPU memory via PCIe to encode on the GPU. These two additional transfers create latency due to the transfer time and will increase PCIe bandwidth occupancy.
See also:
- Using FFmpeg with NVIDIA GPU Hardware Acceleration
- FFmpeg documentation - filters
- FFmpeg documentation -
scale_cuda
-vf "scale_cuda=-2:480"
scales to a 480px height and a width calculated to keep the aspect ratio. The2
in-2
makes sure the width is divisible by 2 - see FFmpeg's scale options:
width, w
height, h
Set the output video dimension expression. Default value is the input dimension.
If the width or w value is 0, the input width is used for the output. If the height or h value is 0, the input height is used for the output.
If one and only one of the values is -n with n >= 1, the scale filter will use a value that maintains the aspect ratio of the input image, calculated from the other specified dimension. After that it will, however, make sure that the calculated dimension is divisible by n and adjust the value if necessary.
If both values are -n with n >= 1, the behavior will be identical to both values being set to 0 as previously detailed.
- ⚠️ Some formats or filters are not supported by the GPU, so you may need to use both GPU + CPU. See "Mixing CPU and GPU processing" in the NVIDIA FFmpeg Transcoding Guide:
Sometimes it might be necessary to mix CPU and GPU processing. For example you may need to decode on the CPU, because the format is unsupported on the GPU decoder, or because a filter is not available on the GPU. In those cases, you can’t use the
-hwaccel cuvid
or-hwaccel cuda
flag. Instead, you need to manage uploading the data from system to GPU memory using thehwupload_cuda
filter.In the example below, an H.264 stream is decoded on the GPU and downloaded to system memory since
-hwaccel cuvid
is not set. The fade filter is applied in system memory and the processed image uploaded to GPU memory using thehwupload_cuda
filter. Finally, the image is scaled usingscale_npp
and encoded on the GPU.ffmpeg -vsync 0 -c:v h264_cuvid -i input.264 -vf "fade,hwupload_cuda,scale_npp=1280:720" -c:v h264_nvenc output.264
Read this to learn more about scale_npp
vs. scale_cuda
.
Credits: this post, this one, and this one pointed me in the right direction to figure out the proper syntax for hardware acceleration with CUDA.