5

I am new to ffmpeg and I want to create a square thumbnail of size 500x500 by cropping the center of the video, irrespective of width and height. How can I achieve this? Thanks in advance.

WebDiva
  • 133
  • 3
  • 23
  • What do you want it to do if input width or height is smaller than 500? – llogan Sep 11 '20 at 17:46
  • It's just a preference. I think that the most minimum width of inputs would be 320 or more as I am targetting only mobile phones. The only thing that should be considered is that it should be a square and should be cropped from the center of the video. Thank You. – WebDiva Sep 11 '20 at 17:55

1 Answers1

9

First crop, then scale.

ffmpeg -i in -vf "crop=w='min(min(iw\,ih)\,500)':h='min(min(iw\,ih)\,500)',scale=500:500,setsar=1" -vframes 1 thumbnail.jpg

x and y for crop aren't set as they default to center crop.


ffmpeg -i in -vf "crop=w='min(iw\,ih)':h='min(iw\,ih)',scale=500:500,setsar=1" -vframes 1 thumbnail.jpg

This will select the largest square possible and scalethat to 500x500.

Gyan
  • 85,394
  • 9
  • 169
  • 201
  • 1
    Thank you it's working fine but ffmpeg always shows the warning 'deprecated pixel format used, make sure you did set range correctly' and 'Warning: data is not aligned! This can lead to a speed loss'. Is this something that I should be considered about? – WebDiva Sep 12 '20 at 07:44
  • 1
    Sorry but no change. – WebDiva Sep 12 '20 at 07:52
  • ```Stream mapping: Stream #0:0 -> #0:0 (h264 (native) -> mjpeg (native)) Press [q] to stop, [?] for help [swscaler @ 000001c4b31707c0] deprecated pixel format used, make sure you did set range correctly [swscaler @ 000001c4b31707c0] Warning: data is not aligned! This can lead to a speed loss``` – WebDiva Sep 12 '20 at 07:53
  • 1
    Also I just have a request that, can you set the min width, height and scale according to input? The issue just came into my mind when @llogan mentioned it in the comments. Thank you. – WebDiva Sep 12 '20 at 07:57
  • It's fine to ignore it now, after you've added the format filter. @llogan's concern is accounted for, in my crop exprs. – Gyan Sep 12 '20 at 09:32
  • 1
    But the thumbnail does not show the full part of the video's center (width). How can i solve this? – WebDiva Sep 12 '20 at 14:47
  • The full width won't be visible if width is greater than height, since you want to cut a square window. – Gyan Sep 12 '20 at 15:02
  • 1
    No it's a 9:16 video. – WebDiva Sep 12 '20 at 15:28
  • What's the resolution? – Gyan Sep 12 '20 at 15:47
  • 1
    The resolution is 776 x 1440 – WebDiva Sep 12 '20 at 16:18
  • In this case, the middle 500x500 region will be cropped. – Gyan Sep 12 '20 at 19:37
  • 1
    Is there any way to crop it to full width and scale the thumbnail to 500x500 – WebDiva Sep 13 '20 at 05:35