83

Is it possible to resize an image using FFmpeg?

I have this so far:

ffmpeg. -i 1.jpg -vf scale=360:240 > 2.jpg

I get the error message:

At least one output file must be specified

Is it possible?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179

5 Answers5

164

You can try this:

ffmpeg -i input.jpg -vf scale=320:240 output_320x240.png

I got this from source

Note: The scale filter can also automatically calculate a dimension while preserving the aspect ratio: scale=320:-1, or scale=-1:240

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Andri Kurnia
  • 2,148
  • 1
  • 13
  • 13
  • Hi, beat you to it. It was what I just found. But, as you got the right answer and you gave me your time I give you a tick :) – Andrew Simpson Mar 02 '15 at 11:03
  • 27
    The scale filter can also automatically calculate a dimension while preserving the aspect ratio: `scale=320:-1`, or `scale=-1:240`. – llogan Mar 02 '15 at 18:13
  • This results in a corrupt image for me. Just a bunch of lines and colors. jpg to jpg – Tyguy7 Nov 09 '15 at 18:26
  • 2
    Interesting, png output works fine though. JPG is borked. – Tyguy7 Nov 09 '15 at 18:27
  • What if I want to keep the output file name unchanged? Can I have a shorter command? Thank you. – Alston Mar 01 '19 at 03:22
  • I have created a 65,535x155-pixel waveform transparent png from an audio file with another ffmpeg command. And as I want some other resolutions such as 20,000x155, 10,000x155, and 1920x155, I tried using this code because I did not want to create another resolutions with the first time command to save time. It works but as my png has the transparent areas, I see grayed pixels around the waveform. Is there any solution to resize a transparent png image with ffmpeg maintaining the areas which are partly transparent? – M Ahmadzadeh Sep 13 '22 at 06:46
  • The same command works for videos, by the way, if someone - like me - was wondering... – BjoernL. Feb 27 '23 at 10:54
31

If you want to retain aspect ratio you can do -

ffmpeg -i 1.jpg -vf scale="360:-1" 2.jpg

or if you want to resize based on input width and height, let's say half of input width and height you can do -

ffmpeg -i 1.jpg -vf scale="iw/1:ih/2" 2.jpg

where

iw: input width
ih: input height
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • 1
    I found this very useful. It should be noted that the correct usage for cutting both height and width in half is -vf scale="iw/2:ih/2". – charles young Jan 02 '23 at 13:53
11

It is also possible to resize an image to fit inside some dimensions and letterbox the rest.

Example command:

ffmpeg -i IN.png -vf "scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2" OUT.jpg

See this answer for more details.

Boris Yakubchik
  • 3,861
  • 3
  • 34
  • 41
  • 1
    After a long online search and not knowing that this was called "letterboxing", this answer just saved me from a lot of hassle! Thanks so much! – kafman Jun 13 '21 at 16:22
  • 1
    Thank you, also the only answer I found online that correctly aspect fits multiple images – jjxtra Feb 18 '23 at 04:40
7

Thanks to @andri-kurnia 's answer.

This example also shows how to resize multiple images (in windows):

for %j in (*.jpg) do ffmpeg -i "%j" -vf scale=480:-1 "Small-%~nj.jpg"

This command will resize all .jpg images in the folder, sets the width 480 while keeping ratio, and add "Small-" at the start of the resized image name. And I think for some types, it may be necessary to use -2 instead of -1. For specifying the height, we can use something like -1:480.

Misagh
  • 3,403
  • 1
  • 20
  • 17
5

To reduce image scale to the bounding box of width:320px and height:240px.

ffmpeg -i src_image_path -vf 'scale=if(gte(a\,320/240)\,min(320\,iw)\,-2):if(gte(a\,320/240)\,-2\,min(240\,ih))' dst_image_path

a: aspect ratio
iw: in width
ih: in height

If the src image size is in the bounding box do no resize on it. If image has a big aspect ration than 320/240 and width is bigger then 320, resize width to 320 and keep the aspect ration. If image has a small aspect ration than 320/240 and height is bigger then 240, resize height to 240 and keep the aspect ration.

LF00
  • 27,015
  • 29
  • 156
  • 295