ffmpeg添加不同大小的半透明水印(png)

时间:2016-09-20 10:24:11

标签: filter ffmpeg transparency blend

我正在尝试在半透明的h264视频上添加一个png水印(带有alpha通道)。通过使用叠加过滤器,我设法为视频添加了水印。

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

但叠加过滤器不提供透明选项。所以我尝试使用混合滤镜。但是,当我使用原始分辨率时,会出现错误消息。

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

输出:

  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

结果看起来像参数的某些分辨率问题。因此我尝试在混合之前缩放水印。

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使用这些参数。但是输出并不是我预期的,因为水印已被拉长。 有没有想过将水印与不同分辨率混合而不延伸到具有透明度的视频?

以下是测试文件。 (ffmpeg 3.1.2版) https://drive.google.com/open?id=0B2X3VLS01TogdHVJZ2I1ZC1GUUU https://drive.google.com/open?id=0B2X3VLS01TogbjhuZTlBOFFpN1k

2 个答案:

答案 0 :(得分:5)

在叠加

之前使用lut过滤器
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

答案 1 :(得分:1)

Logo相对于image大小v1(居中)进行缩放 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相对于image大小v2缩放(右下边有边距) 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