FFmpeg使用png与透明度和排除混合模式

时间:2016-08-24 23:12:22

标签: video ffmpeg png

我正在尝试使用排除混合模式将视频和png混合在一起并具有透明度。我看了很多,但我找不到适合我的解决方案。这是ffmpeg命令:

ffmpeg -i movie.mkv -i text_white_bg_trans.png -filter_complex\
  "[1:v]setsar=sar=1,format=rgba[txt]; [0:v]format=rgba[mo]; [mo][txt]blend=all_mode='exclusion'"  out_exclusion.mkv
编辑:和控制台输出:

ffmpeg version 3.0.2 Copyright (c) 2000-2016 the FFmpeg developers
  built with Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
  configuration: --prefix=/opt/local --enable-swscale --enable-avfilter --enable-avresample --enable-libmp3lame --enable-libvorbis --enable-libopus --enable-libtheora --enable-libschroedinger --enable-libopenjpeg --enable-libmodplug --enable-libvpx --enable-libsoxr --enable-libspeex --enable-libass --enable-libbluray --enable-lzma --enable-gnutls --enable-fontconfig --enable-libfreetype --enable-libfribidi --disable-indev=jack --disable-outdev=xv --mandir=/opt/local/share/man --enable-shared --enable-pthreads --cc=/usr/bin/clang --enable-vda --enable-videotoolbox --arch=x86_64 --enable-yasm --enable-gpl --enable-postproc --enable-libx264 --enable-libxvid
  libavutil      55. 17.103 / 55. 17.103
  libavcodec     57. 24.102 / 57. 24.102
  libavformat    57. 25.100 / 57. 25.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 31.100 /  6. 31.100
  libavresample   3.  0.  0 /  3.  0.  0
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
Input #0, matroska,webm, from 'movie.mkv':
  Metadata:
    ENCODER         : Lavf57.25.100
  Duration: 00:00:11.88, start: 0.000000, bitrate: 5143 kb/s
    Stream #0:0: Video: h264 (High), yuv420p, 1640x1232, SAR 1:1 DAR 205:154, 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
    Metadata:
      ENCODER         : Lavc57.24.102 libx264
      DURATION        : 00:00:11.880000000
Input #1, png_pipe, from 'text_white_bg_trans.png':
  Duration: N/A, bitrate: N/A
    Stream #1:0: Video: png, rgba(pc), 1640x1232, 25 tbr, 25 tbn, 25 tbc
File 'out_exclusion.mkv' already exists. Overwrite ? [y/N] y
No pixel format specified, yuv444p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 @ 0x7fb10a01e200] using SAR=1/1
[libx264 @ 0x7fb10a01e200] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x7fb10a01e200] profile High 4:4:4 Predictive, level 4.0, 4:4:4 8-bit
[libx264 @ 0x7fb10a01e200] 264 - core 148 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=4 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, matroska, to 'out_exclusion.mkv':
  Metadata:
    encoder         : Lavf57.25.100
    Stream #0:0: Video: h264 (libx264) (H264 / 0x34363248), yuv444p, 1640x1232 [SAR 1:1 DAR 205:154], q=-1--1, 25 fps, 1k tbn, 25 tbc (default)
    Metadata:
      encoder         : Lavc57.24.102 libx264
    Side data:
      unknown side data type 10 (24 bytes)
Stream mapping:
  Stream #0:0 (h264) -> format
  Stream #1:0 (png) -> setsar
  blend -> Stream #0:0 (libx264)
Press [q] to stop, [?] for help
frame=   24 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A speed=  frame=   47 fps= 40 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A speed=  frame=   61 fps= 36 q=28.0 size=      96kB time=00:00:00.12 bitrate=6567.3kbitsframe=   70 fps= 32 q=28.0 size=     264kB time=00:00:00.48 bitrate=4513.9kbitsframe=   83 fps= 30 q=28.0 size=     498kB time=00:00:01.00 bitrate=4079.5kbitsframe=   95 fps= 29 q=28.0 size=     691kB time=00:00:01.48 bitrate=3827.5kbitsframe=  106 fps= 28 q=28.0 size=     896kB time=00:00:01.92 bitrate=3823.7kbitsframe=  118 fps= 27 q=28.0 size=    1132kB time=00:00:02.40 bitrate=3863.7kbitsframe=  130 fps= 26 q=28.0 size=    1338kB time=00:00:02.88 bitrate=3806.1kbitsframe=  142 fps= 26 q=28.0 size=    1588kB time=00:00:03.36 bitrate=3871.7kbitsframe=  154 fps= 25 q=28.0 size=    1808kB time=00:00:03.84 bitrate=3856.9kbitsframe=  166 fps= 25 q=28.0 size=    2062kB time=00:00:04.32 bitrate=3910.9kbitsframe=  178 fps= 24 q=28.0 size=    2328kB time=00:00:04.80 bitrate=3972.8kbitsframe=  190 fps= 24 q=28.0 size=    2620kB time=00:00:05.28 bitrate=4064.7kbitsframe=  203 fps= 24 q=28.0 size=    2886kB time=00:00:05.80 bitrate=4076.0kbitsframe=  215 fps= 24 q=28.0 size=    3132kB time=00:00:06.28 bitrate=4085.9kbitsframe=  226 fps= 24 q=28.0 size=    3357kB time=00:00:06.72 bitrate=4092.5kbitsframe=  238 fps= 24 q=28.0 size=    3589kB time=00:00:07.20 bitrate=4083.9kbitsframe=  250 fps= 23 q=28.0 size=    3794kB time=00:00:07.68 bitrate=4047.3kbitsframe=  260 fps= 23 q=28.0 size=    3978kB time=00:00:08.08 bitrate=4032.7kbitsframe=  272 fps= 23 q=28.0 size=    4212kB time=00:00:08.56 bitrate=4031.0kbitsframe=  284 fps= 23 q=28.0 size=    4453kB time=00:00:09.04 bitrate=4035.3kbitsframe=  295 fps= 23 q=28.0 size=    4632kB time=00:00:09.48 bitrate=4002.9kbitsframe=  297 fps= 20 q=-1.0 Lsize=    5559kB time=00:00:11.80 bitrate=3859.4kbits/s speed=0.808x    
video:5556kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.053747%
[libx264 @ 0x7fb10a01e200] frame I:2     Avg QP:21.70  size: 56001
[libx264 @ 0x7fb10a01e200] frame P:178   Avg QP:23.12  size: 24833
[libx264 @ 0x7fb10a01e200] frame B:117   Avg QP:25.78  size:  9886
[libx264 @ 0x7fb10a01e200] consecutive B-frames: 29.3% 49.8% 14.1%  6.7%
[libx264 @ 0x7fb10a01e200] mb I  I16..4:  9.5% 75.0% 15.5%
[libx264 @ 0x7fb10a01e200] mb P  I16..4:  4.0% 14.8%  1.0%  P16..4: 43.3% 12.0%  4.5%  0.0%  0.0%    skip:20.4%
[libx264 @ 0x7fb10a01e200] mb B  I16..4:  0.6%  1.9%  0.1%  B16..8: 48.5%  4.2%  0.3%  direct: 0.6%  skip:43.8%  L0:42.5% L1:55.6% BI: 1.9%
[libx264 @ 0x7fb10a01e200] 8x8 transform intra:74.8% inter:85.5%
[libx264 @ 0x7fb10a01e200] coded y,u,v intra: 31.3% 0.0% 0.0% inter: 15.0% 0.0% 0.0%
[libx264 @ 0x7fb10a01e200] i16 v,h,dc,p: 30% 32% 13% 25%
[libx264 @ 0x7fb10a01e200] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 22% 32%  3%  4%  3%  6%  3%  3%
[libx264 @ 0x7fb10a01e200] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 25% 33% 16%  3%  4%  3%  9%  4%  2%
[libx264 @ 0x7fb10a01e200] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x7fb10a01e200] ref P L0: 72.2% 17.9%  7.5%  2.5%
[libx264 @ 0x7fb10a01e200] ref B L0: 90.9%  8.6%  0.5%
[libx264 @ 0x7fb10a01e200] ref B L1: 98.6%  1.4%
[libx264 @ 0x7fb10a01e200] kb/s:3830.94
编辑:这是输出的一个框架。 输出视频是B& W而不是原始颜色(不确定原因)。图像中的矩形位于png上具有透明背景的文本块的位置。 png与视频大小相同。

我期待更像这样的事情: enter image description here

@Mulvya的建议用颜色修复了这个问题,但我仍然看到一些透明问题正在处理中。我在下面附上一些不同的图像,每个图像都会产生不同的问题。所有在Photoshop CC中创建(其中可能存在问题)。

带有单独文本图层的透明背景,然后通过Save-> png导出,Photoshop似乎合并这两个图层,但与导出前选择“合并图层”时不同。导出的图像看起来像我上面张贴的B& W图像,但带有颜色。

在第二个我在导出之前合并图层。看起来整个图像都被视为白色。

在第三部分中,我使用不同的灰色在“透明”图层上绘制“hello”,然后导出。透明区域呈现为白色,灰色或半透明区域不透明或具有奇怪的抖动“效果”。

似乎我正在超越StackOverflow领域,但我不确定这篇文章哪个更适合。 1 1 2 2 3 3

1 个答案:

答案 0 :(得分:2)

好的,所以混合滤镜的输出很好,但显然有一个错误 - 无论是混合滤镜如何标记其输出的像素格式,还是在自动插入的格式转换中输出

如果之后插入比例过滤器,结果应该没问题。

ffmpeg -i movie.mkv -i text_white_bg_trans.png -filter_complex\
  "[1:v]setsar=1,format=rgba[txt];[0:v]format=rgba[mo];\
   [mo][txt]blend=all_mode='exclusion',scale=0:0"  out_exclusion.mkv

您可能希望在比例后插入format=yuv420p以产生大多数玩家将要播放的输出。

如果预先计算alpha,则应正确处理半透明效果。

ffmpeg -i movie.mkv -i text_white_bg_trans.png -filter_complex\
  "[1:v]setsar=1,format=rgba,geq=r='r(X,Y)*alpha(X,Y)/255':g='g(X,Y)*alpha(X,Y)/255':b='b(X,Y)*alpha(X,Y)/255'[txt];\
   [0:v]format=rgba[mo];\
   [mo][txt]blend=all_mode='exclusion',scale=0:0"  out_exclusion.mkv