3

I am trying to add text to a gif, but after I add it there is some image decolorization & pixilation that happens.

The command I am running on a to add text to the gif is as follows:

ffmpeg -i test.gif -vf "drawtext=fontfile=OpenSans.ttf:text='Stack Overflow':fontcolor=black:fontsize=24" copy output.gif

If anyone had any insight on why the distortion / decolorization might be happening. It would be appreciated!

Here is an example gif, then the result of running the command above.

Original Gif:

enter image description here

After: (post ffmpeg):

enter image description here

(don't worry he goes back to get the cat)

Edon
  • 143
  • 5

2 Answers2

1

So I stumbled my way into a working solution, I'm not sure if it's optimal but it works.

I convert to a .mp4 then add the text, then convert back down to a .gif

Convert to mp4 (you will need to set your own font here)

ffmpeg -i test.gif -vf "drawtext=fontfile=OpenSans.ttf:text='Stack Overflow':fontcolor=black:fontsize=24" -codec:a copy output.mp4

Convert back to gif

ffmpeg -i output.mp4 -filter_complex 'fps=10,scale=500:-1:flags=lanczos,split [o1] [o2];[o1] palettegen [p]; [o2] fifo [o3];[o3] [p] paletteuse' final.gif

This results in the image rendering properly with the text (bottom right):

enter image description here

I'm not sure if this is optimal, or what these complex flags are really doing. I'm also arbitrarily setting the scale to 500 because if I don't the final gif is tiny.

So if anyone has a better solution, please do post here!

Edon
  • 143
  • 5
1

Adapt the command as shown in How do I convert a video to GIF using ffmpeg, with reasonable quality:

ffmpeg -i input.gif -vf "drawtext=fontfile=OpenSans.ttf:text='Stack Overflow':fontcolor=black:fontsize=24,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" output.gif
llogan
  • 63,280