I would like ffmpeg to grab a 1280x720 MP4 video file, crop to square size ratio, resize it to 640x640

The following two commands work for me with great GIF quality bu it's just missing the correct resizing. It does give me a GIF output but the size is 1138x640 instead of 640x640.

Generating a palette:

ffmpeg -y -ss 30 -t 3 -i input.mp4 \
-vf fps=10,scale=1138:-1:flags=lanczos,palettegen palette.png

Outputting GIF using the palette:

ffmpeg -ss 30 -t 3 -i input.mp4 -i palette.png -filter_complex \
"fps=10,scale=1138:-1:flags=lanczos[x];[x][1:v]paletteuse" output.gif

This gives me a 1138x640.gif but how can I get it to be 640x640 without relying on another separate command?

1 Answer 1


Here's a simple method that will work for 1280x720 inputs.

Generate palette:

ffmpeg -i input.mp4 -filter_complex "fps=10,scale=-1:640,crop=ih:ih,setsar=1,palettegen" palette.png

Create GIF:

ffmpeg -i input.mp4 -i palette.png -filter_complex "[0]fps=10,scale=-1:640,crop=ih:ih,setsar=1[x];[x][1:v]paletteuse" output.gif

You can make this more complex if desired by adding the force_aspect_ratio scale option to fit arbitrary input sizes into 640x640, and by adding split and fifo to avoid making the temporary palette file.

  • 2
    Thank you very much for your answer. I accepted your answer but for more precise cropping, how would I go about changing where the cropping takes place? Let's say if I wanted to crop the video 20px from left and 10px from top.
    – Joel Z.
    Feb 4, 2018 at 6:35
  • 1
    – Gyan
    Feb 4, 2018 at 12:20
  • 1
    I used GIMP on one of the frames to get the exact width and offset... then plugged them in like this: ..\ffmpeg-4.3.1-full_build\ffmpeg-4.3.1-full_build\bin\ffmpeg -i C:\Users\myuser\Downloads\myvideo.mkv -vf "fps=30,scale=1920:-1:flags=lanczos,crop=1734:940:90:38,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 outputcrop.gif I'm guessing the scale=1920 is unneeded, but it worked to produce the exact dimensions specified in the crop section anyway!
    – nmz787
    Sep 22, 2020 at 0:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .