Reduce Generated GIF Size Using FFMPEG
Solution 1
The standard way to use ffmpeg for GIFs is
Generate a palette from the video
ffmpeg -y -i file.mp4 -vf palettegen palette.png
Then,
ffmpeg -y -i file.mp4 -i palette.png -filter_complex paletteuse -r 10 -s 320x480 file.gif
More options documented here.
Solution 2
I tried all these techniques and I ended up using https://ezgif.com
I have no idea what magic they're using under the hood, but their compression level knob, palette optimization and resolution reduction resulted in the highest visual quality and smallest file size.
I tried -vf palettegen
and -filter_complex paletteuse
but that removed the ability to -vf "scale=iw*.2:ih*.2"
in the same run so I had to do that separately. And ffmpeg created larger files for 0.3
scale than 0.5
which is odd. After an hour of fiddling, I tried ezgif and it worked nicely.
Solution 3
Below command helped me optimising my gif's. The parameters can change based on your specific requirements.
ffmpeg -y -i input.mp4 -filter_complex "fps=5,scale=480:-1:flags=lanczos,split[s0][s1];[s0]palettegen=max_colors=32[p];[s1][p]paletteuse=dither=bayer" output.gif
Note:
-
here scale and max_colors drastically impacts the output gif's filesize.
-
If you set a low values for scale and max_colors, setting up dither to bayer makes the output gif look a little better. Default value for dither is sierra2_4a
Solution 4
vid=
start_time=00:00:01
duration=5
height=ih/2 # input height halved , can replace with pixils .
width=-2 # keeps aspect ratio . can replace with pixils .
fps=25 # frames per a second .
filters="fps=$fps,scale=$width:$height:flags=lanczos"
ffmpeg -ss $start_time \
-t $duration \
-i "$vid" \
-vf "$filters,palettegen" \
-y palette.png &&
ffmpeg -ss $start_time \
-t $duration \
-i "$vid" \
-i palette.png \
-lavfi "$filters [x]; [x][1:v] paletteuse" \
-y "$vid".gif &&
rm palette.png
Solution 5
What worked for me was specifying a lower frame rate than the 10 you're using (-r 10) for the output gif. Probably not what you want if you're after good quality. If you're after a better quality gif then it will be bigger in file size.
Related videos on Youtube
![arsena](https://i.stack.imgur.com/ZtYNB.jpg?s=256&g=1)
arsena
Updated on September 18, 2022Comments
-
arsena almost 2 years
I'm developing android application that converts
mp4
files intogifs
usingffmpeg
. Problem is that generated gifs are huge in size. And another problem is that I can't use anything else thanffmpeg
(e.g.imagemagick
forconvert
, or evenpalletes
for now) to reduce generated gif size.this is the command I'm using:
ffmpeg -y -i file.mp4 -pix_fmt rgb24 -r 10 -s 320x480 file.gif
So is there any other way to optimize conversion?
-
Foad about 4 yearstry
magick out.gif -fuzz 30% -layers Optimize result.gif
-
Ricardo Bohner about 3 yearsWhy not use webp instead of gif. Webp can use more colores than gif and produces an animaed imagesequence with much lower size.
-
-
Jason C over 7 yearsWeirdly, generating a palette tripled the size of the GIF, although it greatly increased the quality over the default palette. I found the best way is to generate the GIF with ffmpeg as per usual (possibly with a better palette as in this answer) then just run it through an optimization tool (there's ones online too) that can make unchanging parts of the frame transparent or apply other optimizations.
-
confetti almost 6 yearsWhere is
$palette
being set in your script? -
abc almost 6 yearswhere the command is run
-
abc almost 6 yearssorry . there was a mistake with palette file . fixed now .
-
Roy Shilkrot over 3 yearsyou can just
-filter_complex "paletteuse,scale=iw*.2:ih*.2"
instead of-vf
-
Jason C over 2 yearsTFW 5 years later, you've got a question, you find a useful answer, you read a helpful comment and think "ah yeah; I noticed that, too", and then you realize that comment was yours. 😅
-
z0r about 2 yearsGenerating a palette worked really well for me for a screen capture: reduced the size by 25%. Then running through
gifsicle
reduced it again, with an overall reduction of 70%.