Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg add semi transparent watermark(png) with different size

I'm trying to add a png watermark (with alpha channel) over h264 video with semi transparent. By using overlay filter I managed to add watermark to the video.

ffmpeg -y -i input.mp4 -i watermark.png -filter_complex "[0][1] overlay=0:0" -c:v libx264 -an output.mp4

But overlay filter does not provide transparent option. So I tried to use blend filter. However, when I use origin resolution, error message comes out.

ffmpeg -y -i input.mp4 -i watermark.png -filter_complex "[0][1]blend=all_mode=overlay:all_opacity=0.3" -c:v libx264 -an output.mp4

Output:

  libavutil      55. 28.100 / 55. 28.100
  libavcodec     57. 48.101 / 57. 48.101
  libavformat    57. 41.100 / 57. 41.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 47.100 /  6. 47.100
  libavresample   3.  0.  0 /  3.  0.  0
  libswscale      4.  1.100 /  4.  1.100
  libswresample   2.  1.100 /  2.  1.100
  libpostproc    54.  0.100 / 54.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.41.100
  Duration: 00:00:45.08, start: 0.000000, bitrate: 1872 kb/s
    Stream #0:0(und): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1920x1080, 1869 kb/s, 29.72 fps, 30 tbr, 16k tbn, 32k tbc (default)
    Metadata:
      handler_name    : VideoHandler
Input #1, png_pipe, from 'watermark.png':
  Duration: N/A, bitrate: N/A
    Stream #1:0: Video: png, rgba(pc), 64x64, 25 tbr, 25 tbn, 25 tbc
[Parsed_blend_0 @ 00750600] First input link top parameters (size 1920x1080, SAR 0:1) do not match the corresponding second input link bottom parameters (64x64, SAR 0:1)
[Parsed_blend_0 @ 00750600] Failed to configure output pad on Parsed_blend_0
Error configuring complex filters.
Invalid argument

The result looks like some resolution issue with the parameters. So that I tried to scale the watermark before blending.

ffmpeg -y -i input.mp4 -i watermark.png -filter_complex "[0:0]scale=1920x1080[a]; [1:0]scale=1920x1080[b]; [a][b]blend=all_mode=overlay:all_opacity=0.3" -c:v libx264 -an output.mp4

FFMPEG works with these parameters. But the output wasn't I expected, because watermark had been stretched. Any idea to blend watermark with different resolution without stretching to video with transparency?

Here are the testing file. (ffmpeg version 3.1.2) https://drive.google.com/open?id=0B2X3VLS01TogdHVJZ2I1ZC1GUUU https://drive.google.com/open?id=0B2X3VLS01TogbjhuZTlBOFFpN1k

like image 708
Archer Avatar asked Sep 20 '16 10:09

Archer


People also ask

How do I add a watermark to FFmpeg?

The easiest way to install ffmpeg is to download a pre-built binary for your specific platform. Then you don’t have to worry about including and installing all the right dependencies and codecs you will be using. Once you have ffmpeg installed, adding a watermark is as easy as passing your existing source through an overlay filter like so:

How to add branding or a watermark to a clip?

If we wanted to add branding or a watermark to the clip but not cover the existing video, we can use the pad filter to add some padding to our clip, and then position our watermark over the padding like so:

How do I install FFmpeg?

The easiest way to install ffmpeg is to download a pre-built binary for your specific platform. Then you don’t have to worry about including and installing all the right dependencies and codecs you will be using.


2 Answers

Logo scaled relative to image size v1 (centered) Result

ffmpeg -y \
-i "bird.jpg" \
-i "logo.png" \
-filter_complex "\
[1][0]scale2ref=h=ow/mdar:w=iw/4[#A logo][bird];\
[#A logo]format=argb,colorchannelmixer=aa=0.5[#B logo transparent];\
[bird][#B logo transparent]overlay\
=(main_w-w)/2:(main_h-h)/2" \
image_with_logo.jpg

#A preserve aspect ratio of logo, scale width of logo to 1/4 of image width
#B add alpha channel, reduce opacity to 50%
# overlay image an logo
#position is horizontally and vertically centered

Logo scaled relative to image size v2 (bottom right with margin) Result

ffmpeg -y \
-i "bird.jpg" \
-i "logo.png" \
-filter_complex "\
[1][0]scale2ref=h=ow/mdar:w=iw/4[#A logo][bird];\
[#A logo]format=argb,colorchannelmixer=aa=0.5[#B logo transparent];\
[bird][#B logo transparent]overlay\
=(main_w-w)-(main_w*0.1):(main_h-h)-(main_h*0.1)" \
image_with_logo.jpg

#A preserve aspect ratio of logo, scale width of logo to 1/4 of image width
#B add alpha channel, reduce opacity to 50%
# overlay image an logo
# position: bottom right, with a margin of 10% of the edges
like image 143
drake7 Avatar answered Oct 05 '22 03:10

drake7


Use the lut filter before overlay

ffmpeg -y -i input.mp4 -i watermark.png -filter_complex 
          "[1]lut=a=val*0.3[a];[0][a]overlay=0:0"
       -c:v libx264 -an output.mp4
like image 30
Gyan Avatar answered Oct 05 '22 03:10

Gyan