0

i have the following command to overlay gif above png

String command = '-y -i $path.png -i $path.gif -filter_complex '
             '[1:v]scale=200:200[i1];'
             '[0:v][i1]overlay=100:100'
             ' -q:v 1 $output.gif';

it work fine , but the problem is that my first input ($path.png) is no longer transparently as what it was before execute .

in other word : before i execute the command my path.png has rounded corners and transparently background , when i overlay the gif above it so it totally effect path.png like it remove the rounded corners and background became black .

when i overlay png instead of gif so it does not effect the path.png and everything is fine .

So how to to overlay gif with preserving the transparently of path.png and it's rounded corner too . thanks

Edit

this is the first input (png)

enter image description here

this is second input (gif)

enter image description here

Now i am trying to overlay the second input (gif) above the first input (png)

'-y -i png -i gif -filter_complex '
             '[1:v]scale=$w:$h[i1];'
             '[0:v][i1]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)-10'
             ' -q:v 1 outputGif';

OUTPUT

enter image description here

as you can see , it lost the black transparently and rounded corners

and the following is @Rotem command

 '-y -i png  -i gif -filter_complex '
             '"[1:v]scale=$w:$h[i1];'
             '[0:v][i1]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)-10:'
             'format=auto,split[s0][s1];[s0]palettegen=reserve_transparent=1[p];[s1][p]paletteuse" -q:v 1 outputGif'; 

output

enter image description here

it became totally transparent . but i am trying to keep that black transparent background like original and not fully transparent

1 Answers1

0

For keeping transparently for both GIF and PNG, we may use the following syntax:

ffmpeg -y -i in.png -i in.gif -filter_complex "[1:v]scale=200:200[i1];[0:v][i1]overlay=100:100:format=auto,split[s0][s1];[s0]palettegen=reserve_transparent=1[p];[s1][p]paletteuse" -q:v 1 out.gif


  • overlay=100:100:format=auto,format=rgba - forces the overlay output pixel format to be rgba (keeping transparency).
    Note: It's working with FFmpeg 5.1.2 (I didn't test older versions).
  • [s0]palettegen=reserve_transparent=1[p];[s1][p]paletteuse - generates a new palette for the output GIF as in the following answer.
    The new palette matches the colors of the new overlaid image.
    reserve_transparent=1 generates the palette with transparency.

Testing:

out.gif:
enter image description here

in.png:
enter image description here

in.gif:
enter image description here

Rotem
  • 1,607
  • 3
  • 10
  • 11
  • deeply thanks @Rotem , this is work perfectly but i think I'm expressing it wrong with (transparently word) , i mean it has black background say 0.5 degree (not totally transparently ) , after testing your command it totally make the background transparently where i want to preserve the original one which has black transparently background (but totally transparent) – Mohammed Hamdan Jul 29 '23 at 02:10
  • command = '-y -i $png -i $gif -filter_complex ' '"[1:v]scale=$w:$h[i1];' '[0:v][i1]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)-$fromBottom:' 'format=auto,split[s0][s1];[s0]palettegen=reserve_transparent=1[p];[s1][p]paletteuse" -q:v 1 $gif'; – Mohammed Hamdan Jul 29 '23 at 02:11
  • Can you please edit your question, and add sample input images? (you don't have to post the real images, just images that reproduce the issue). Please use file names instead of `$png`, `$gif`... and replace `command =` with full FFmpeg command. Can you please make sure that animated GIF format supports semi-transparent background? I think it is not supported. See [this post](https://ezgif.com/help/gif-transparency) for example. – Rotem Jul 29 '23 at 08:08
  • thanks for taking one more try help , i am scary if i did not understand you but i did my best in EDIT , please take a look – Mohammed Hamdan Jul 29 '23 at 09:44
  • When I edit the PNG image into [this image](https://i.stack.imgur.com/jfrjv.png), my solution seems to work. The issue is that most of the area of your PNG input image is semi-transparent (the gray part is 60% transparent and 40% opaque). Animated GIF format doesn't support semi-transparent pixels, only 100% transparent and 100% opaque. We may use FFmpeg to pass some threshold over the PNG transparency, making most area 100% opaque and make the corners 100% transparent but I think it's out of the scope of the question... – Rotem Jul 29 '23 at 20:52
  • thanks for telling me these info, i wasn't know that, so in final do you think there are no way to do my desired using ffmpeg ? – Mohammed Hamdan Jul 29 '23 at 23:21
  • 1
    In case what you want is similar to the following Animated PNG: `ffmpeg -y -i in.png -i in.gif -filter_complex "[0:v]format=rgba[v0];[1:v]scale=200:50[i1];[v0][i1]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)-10:format=auto" -plays 0 out.apng`, It is not possible using Animated GIF. The gray background of the Animated GIF can't be semi-transparent. – Rotem Jul 30 '23 at 16:16
  • thanks you , so i understand from you that i must choice another gif that support semi-transparent in order to do my desired output in my question ? in other word i mean does it possible to find gif that supporting semi-transparent ? if yes so which command of yours is for this ? your last command it working perfectly as expected but my gif in no longer animated anymore :( .. deeply thanks – Mohammed Hamdan Aug 03 '23 at 01:36
  • Animated GIF format supports only two type of transparency: "transparent" and "not transparent". There is no support for "half transparent" pixels. The above example uses **Animated PNG** instead of Animated GIF. Animated PNG supports "half transparent" pixels. Animated PNG is support is less common, so you probably can't use Animated PNG format. – Rotem Aug 03 '23 at 11:53
  • thank you , after reading your comment carefully, i tested above command with Animated PNG instead gif https://lukeroberts.tv/blog/wp-content/uploads/2019/05/apng1.png , there are no animated in output but it preserve the half-transparent '-y -i $png -i https://lukeroberts.tv/blog/wp-content/uploads/2019/05/apng1.png' ' -filter_complex "[0:v]format=rgba[v0];[1:v]scale=200:50[i1];[v0][i1]' 'overlay=(main_w-overlay_w)/2:(main_h-overlay_h)-10:format=auto" -plays 0 $.apng'; – Mohammed Hamdan Aug 05 '23 at 12:33