user7032056
user7032056

Reputation:

Adding a watermark to a GIF with reasonable quality using FFMPEG

so I got this code to add a logo onto a folder full of GIFs

@echo off
setlocal
for %%G in ("%~dp0webm\*.gif") do (
    ffmpeg -i %%G -i watermark.png -filter_complex "[0:v]scale=trunc(iw/2)*2:trunc(ih/2)*2[v0];[1:v][v0]scale2ref=trunc(iw/5):trunc(ih/16)[logo][0v];[0v][logo]overlay=W-w-3:H-h-4 [v]" -map "[v]" "C:\Users\Propietario\Desktop\Conversion\gif\%%~nG.gif"
    )
)
endlocal
pause

However, the output quality is rather low, similar to an issue I had with converting from video to GIF which was solved by creating a palette.

ffmpeg -i input.gif -filter_complex palettegen[PAL];[0:v]fifo[v0];[v0][PAL]paletteuse output.gif

Issue is, both solutions use -filter_complex so I don't know exactly how to combine the two in a way in which I'll be able to turn GIFs into watermarked GIFs with reasonable quality.

Upvotes: 0

Views: 896

Answers (2)

bukkojot
bukkojot

Reputation: 1530

You can't add watermark to GIF without quality loss.

GIF have upto 256 color and must be treated as pixel art, not smooth images as video. Tools for video processing expected video as sequence of smooth images. Somewhere here conversion between world of crisp pixels and world of smooth images, which CAN NOT BE done without loss. GIF also uses many tricks, like very adaptive palette, for example, so if you want add white logo, here may be no white color in palette. And here no "alpha blending" any attempt to combine colors will change palette map dramatically, all image will be distorted.

If you really want add overlay, if theory, you can unpack pixels as PALETTE IMAGE (video processing tools in our time can't do it), analyze watermark, adapt watermark for current palette (yes, you must change your watermark, if you don't want change image), add watermark to image and repack image again. This is complex task and I don't know tools for it. As internet user, I don't support practice, when good content labeled by someone with unneeded watermarks, I prefer originals, so I don't like to write such software.

Upvotes: 1

Gyan
Gyan

Reputation: 93271

Use

@echo off
setlocal
for %%G in ("%~dp0webm\*.gif") do (
    ffmpeg -i %%G -i watermark.png -filter_complex "[0:v]scale=trunc(iw/2)*2:trunc(ih/2)*2[v0];[1:v][v0]scale2ref=trunc(iw/5):trunc(ih/16)[logo][0v];[0v][logo]overlay=W-w-3:H-h-4,palettegen [v]" -map "[v]" "C:\Users\Propietario\Desktop\Conversion\gif\%%~nG-palette.png"
    ffmpeg -i %%G -i watermark.png -i "%%~nG-palette.png" -filter_complex "[0:v]scale=trunc(iw/2)*2:trunc(ih/2)*2[v0];[1:v][v0]scale2ref=trunc(iw/5):trunc(ih/16)[logo][0v];[0v][logo]overlay=W-w-3:H-h-4[ov];[ov][2]paletteuse [v]" -map "[v]" "C:\Users\Propietario\Desktop\Conversion\gif\%%~nG.gif"
    )
)
endlocal
pause

Upvotes: 1

Related Questions