ffmpeg: overlay a png image on a video with custom transparency?

16,180

Solution 1

Another option besides geq is colorchannelmixer.

[1:v]format=argb,colorchannelmixer=aa=0.5[zork]

Solution 2

I think I got it:

ffmpeg 
 -i foo.mkv 
 -i bar.png 
 -filter_complex "[1:v]format=argb,geq=r='r(X,Y)':a='0.5*alpha(X,Y)'[zork]; 
   [0:v][zork]overlay" 
 -vcodec libx264 
 myresult.mkv

Where 0.5 is the opacity factor. I'm including format=argb so it also works with overlay images that don't have an alpha channel of themselves.

Share:
16,180
RocketNuts
Author by

RocketNuts

Updated on June 03, 2022

Comments

  • RocketNuts
    RocketNuts about 2 years

    Suppose I have a video foo.mkv and an image bar.png (which happens to contains an alpha channel). I can blend this image over the video like this:

    ffmpeg 
     -i foo.mkv 
     -i bar.png 
     -filter_complex "[0:v][1:v]overlay" 
     -vcodec libx264 
     myresult.mkv
    

    (using multiple lines here for readability, normally this is one command line).

    Now, besides the png image having an alpha channel of its own, I would also apply a custom overall transparency when blending this image over the video.

    In the above example, the image would be visible 100% on top of the video — or at least the parts where its alpha channel is fully opaque.

    Is there a way to add a custom overall opacity or transparency blend factor, something like opacity=0.5 or whatever, which would make the image only 50% visible?

  • RocketNuts
    RocketNuts almost 8 years
    Thanks! What I like about that is it doesn't force me to explicitly set one of the color channels as well (as is the case with geq, hence my dummy r='r(X,Y)'). Do you know of a way to force the format to contain alpha, without explicitly forcing the color model as well? (as I am doing with format=argb) i.e. is there something like format=a<currentcolormodel> ?
  • Gyan
    Gyan almost 8 years
    Not in a way that won't overwrite the alpha channel if it does exist.
  • RocketNuts
    RocketNuts almost 8 years
    Out of curiosity, what would be a way that would overwrite an existing alpha channel?
  • Gyan
    Gyan almost 8 years
    color=gray,format=gray[c];[c][0]scale2ref[a][v];[v][a]alpham‌​erge[ovrly] Color gray would ultimately lead to a 50% alpha channel. More tedious, but avoids knowing the source format.
  • Gyan
    Gyan almost 8 years
    Another way to get your result is to use the blend filter in overlay mode with opacity of 0.5 applied to top layer.
  • RocketNuts
    RocketNuts almost 8 years
    Right, I didn't know that one either. Very helpful, I learned a lot, thx!
  • Zain Ali
    Zain Ali almost 6 years
    @Gyan can you please answer the similar question?
  • dvdhns
    dvdhns over 3 years
    More complete demonstrative answer
  • Jeremy Caney
    Jeremy Caney over 2 years
    Why do you prefer this over the significantly simpler top-voted answer? Are there capabilities offered by this approach not available using the command-line options?