1

Using the regular method, input > drawtext seems to remove transparency for GIFs and messes with the coloring

ffmpeg -i test.gif drawtext=text='Test':fontcolor=red:fontsize=24 output.gif

I've always tried using filter complex's palettegen and paletteuse, but those require arguments that drawtext doesn't provide:

ffmpeg -i banner.gif -filter_complex "[0:v] drawtext=text='Test':fontcolor=red:fontsize=24 [a]; [a] palettegen=reserve_transparent=1 [b]; paletteuse [a][b]" output.gif

What's the best way to efficiently draw text while preserving transparency?

1 Answers1

2

According to this post, the syntax is:

ffmpeg -y -i test.gif -filter_complex "drawtext=text='Test':fontcolor=red:fontsize=24,split[s0][s1];[s0]palettegen=reserve_transparent=1[p];[s1][p]paletteuse" output.gif


My guess is that FFmpeg decodes the GIF into raw RGBA format and draws the red text on the RGBA image (in the RAM).

Applying split, palettegen and paletteuse, generates a new palette (ignoring the original palette), and convert the raw RGBA image to indexed image using the generated palette.
(In theory, there is more efficient way doing it when the red color is part of the original palette, but I don't thing FFmpeg supports that kind of optimization).


Testing:

  • Download the PNG image from my former post (image in RGBA format).
  • Convert the PNG image to GIF image (with transparency):
    ffmpeg -y -i 6qepl.png -filter_complex "split[s0][s1];[s0]palettegen=reserve_transparent=1[p];[s1][p]paletteuse" test.gif
  • Draw red text while preserving transparency:
    ffmpeg -y -i test.gif -filter_complex "drawtext=text='Test':fontcolor=red:fontsize=120,split[s0][s1];[s0]palettegen=reserve_transparent=1[p];[s1][p]paletteuse" output.gif

Output (resized):
enter image description here

(The red dots around the transparent part are the result of the newly generated palette).

Rotem
  • 3,346