FFmpeg fontcolor_expr 随时间动态改变字体颜色

FFmpeg fontcolor_expr to dynamically change fontcolor over time

我正在使用带有 Node.JS 的 Fluent-FFmpeg 来创建上面覆盖有文本的视频,我想随着视频的进行更改文本的颜色。

我的过滤器遵循以下模式:

drawtext=enable='between(t,18.93,20.28)':fontfile=fonts/cousine-bold.ttf:fontsize=144:fontcolor_expr=%{expr\\: if(between(t\, 0\, 20)\, 888888\, 111111)}:x=82:y=288:text='PROGRAMMING'

它打印出这些东西:

ffmpeg version 3.3.2 Copyright (c) 2000-2017 the FFmpeg developers
  built with Apple LLVM version 8.1.0 (clang-802.0.42)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libfreetype --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-vda
  libavutil      55. 58.100 / 55. 58.100
  libavcodec     57. 89.100 / 57. 89.100
  libavformat    57. 71.100 / 57. 71.100
  libavdevice    57.  6.100 / 57.  6.100
  libavfilter     6. 82.100 /  6. 82.100
  libavresample   3.  5.  0 /  3.  5.  0
  libswscale      4.  6.100 /  4.  6.100
  libswresample   2.  7.100 /  2.  7.100
  libpostproc    54.  5.100 / 54.  5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'uploads/0c719e39820366bc62c8fd66a6327828':
  Metadata:
    major_brand     : qt  
    minor_version   : 0
    compatible_brands: qt  
    creation_time   : 2017-06-23T00:19:46.000000Z
    com.apple.quicktime.make: Apple
    com.apple.quicktime.model: MacBookPro9,2
    com.apple.quicktime.software: Mac OS X 10.12.5 (16F73)
    com.apple.quicktime.creationdate: 2017-06-22T17:18:42-0700
  Duration: 00:00:22.00, start: 0.000000, bitrate: 3408 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], 3091 kb/s, 15 fps, 15 tbr, 30k tbn, 50 tbc (default)
    Metadata:
      creation_time   : 2017-06-23T00:19:46.000000Z
      handler_name    : Core Media Data Handler
      encoder         : H.264
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 316 kb/s (default)
    Metadata:
      creation_time   : 2017-06-23T00:19:46.000000Z
      handler_name    : Core Media Data Handler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x7ff72e810a00] using SAR=1/1
[libx264 @ 0x7ff72e810a00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x7ff72e810a00] profile High, level 3.1
[libx264 @ 0x7ff72e810a00] 264 - core 148 r2748 97eaef2 - H.264/MPEG-4 AVC codec - Copyleft 2003-2016 - 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=-2 threads=6 lookahead_threads=1 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=15 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, mp4, to './outputFile.mp4':
  Metadata:
    major_brand     : qt  
    minor_version   : 0
    compatible_brands: qt  
    com.apple.quicktime.creationdate: 2017-06-22T17:18:42-0700
    com.apple.quicktime.make: Apple
    com.apple.quicktime.model: MacBookPro9,2
    com.apple.quicktime.software: Mac OS X 10.12.5 (16F73)
    encoder         : Lavf57.71.100
    Stream #0:0(und): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 15 fps, 15360 tbn, 15 tbc (default)
    Metadata:
      creation_time   : 2017-06-23T00:19:46.000000Z
      handler_name    : Core Media Data Handler
      encoder         : Lavc57.89.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1(und): Audio: aac (LC) ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      creation_time   : 2017-06-23T00:19:46.000000Z
      handler_name    : Core Media Data Handler
      encoder         : Lavc57.89.100 aac
frame=   52 fps=0.0 q=27.0 size=      37kB time=00:00:04.66 bitrate=  65.7kbits/s speed=8.74x    
frame=   72 fps= 70 q=27.0 size=     187kB time=00:00:06.03 bitrate= 253.4kbits/s speed=5.83x    
frame=   95 fps= 61 q=27.0 size=     358kB time=00:00:07.52 bitrate= 390.4kbits/s speed=4.82x    
frame=  117 fps= 57 q=27.0 size=     569kB time=00:00:08.98 bitrate= 518.9kbits/s speed=4.35x    
frame=  137 fps= 53 q=27.0 size=     732kB time=00:00:10.32 bitrate= 580.2kbits/s speed=4.03x    
frame=  159 fps= 52 q=27.0 size=     942kB time=00:00:11.79 bitrate= 654.7kbits/s speed=3.84x    
frame=  180 fps= 50 q=27.0 size=    1110kB time=00:00:13.18 bitrate= 689.5kbits/s speed=3.67x    
frame=  204 fps= 50 q=27.0 size=    1331kB time=00:00:14.78 bitrate= 737.2kbits/s speed= 3.6x    
frame=  223 fps= 48 q=27.0 size=    1486kB time=00:00:16.06 bitrate= 757.9kbits/s speed=3.49x    
frame=  248 fps= 48 q=27.0 size=    1708kB time=00:00:17.71 bitrate= 789.7kbits/s speed=3.46x    
frame=  266 fps= 47 q=27.0 size=    1859kB time=00:00:18.92 bitrate= 805.0kbits/s speed=3.35x    
frame=  285 fps= 46 q=27.0 size=    2013kB time=00:00:20.19 bitrate= 816.4kbits/s speed=3.28x    
[drawtext @ 0x7ff72d84bc00] Cannot find color '888888.000000'
    Last message repeated 9 times
frame=  308 fps= 46 q=27.0 size=    2224kB time=00:00:21.75 bitrate= 837.5kbits/s speed=3.27x    
frame=  330 fps= 46 q=27.0 size=    2401kB time=00:00:21.96 bitrate= 895.5kbits/s speed=3.06x    
frame=  330 fps= 41 q=-1.0 Lsize=    2834kB time=00:00:22.00 bitrate=1055.0kbits/s speed=2.73x    
video:2484kB audio:338kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.441003%
[libx264 @ 0x7ff72e810a00] frame I:22    Avg QP:13.64  size: 47604
[libx264 @ 0x7ff72e810a00] frame P:154   Avg QP:16.76  size:  8574
[libx264 @ 0x7ff72e810a00] frame B:154   Avg QP:19.42  size:  1138
[libx264 @ 0x7ff72e810a00] consecutive B-frames:  6.7% 93.3%  0.0%  0.0%
[libx264 @ 0x7ff72e810a00] mb I  I16..4: 85.1% 13.3%  1.6%
[libx264 @ 0x7ff72e810a00] mb P  I16..4:  5.5%  3.1%  0.0%  P16..4: 41.5%  4.0%  5.5%  0.0%  0.0%    skip:40.4%
[libx264 @ 0x7ff72e810a00] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8: 14.7%  0.1%  0.0%  direct: 4.7%  skip:80.4%  L0:28.0% L1:71.4% BI: 0.7%
[libx264 @ 0x7ff72e810a00] 8x8 transform intra:21.7% inter:84.8%
[libx264 @ 0x7ff72e810a00] coded y,uvDC,uvAC intra: 60.5% 98.6% 92.9% inter: 6.0% 24.3% 3.1%
[libx264 @ 0x7ff72e810a00] i16 v,h,dc,p: 35% 19% 43%  3%
[libx264 @ 0x7ff72e810a00] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 10%  7% 80%  1%  1%  1%  1%  0%  0%
[libx264 @ 0x7ff72e810a00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 33% 20% 45%  0%  0%  0%  0%  0%  0%
[libx264 @ 0x7ff72e810a00] i8c dc,h,v,p: 77%  9% 10%  4%
[libx264 @ 0x7ff72e810a00] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x7ff72e810a00] ref P L0: 75.3%  0.3% 17.1%  7.3%
[libx264 @ 0x7ff72e810a00] ref B L0: 80.0% 20.0%
[libx264 @ 0x7ff72e810a00] kb/s:924.66
[aac @ 0x7ff72e812200] Qavg: 2159.960

尤其是这个烦人的地方:

[drawtext @ 0x7ff72d84bc00] Cannot find color '888888.000000'

但是,做这样的事情真的很不错:

fontcolor_expr=%{expr\\: if(between(t\, 0\, 20)\, pink\, white)}

打印出这个错误:

[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
frame=  287 fps= 46 q=27.0 size=    2023kB time=00:00:20.31 bitrate= 815.9kbits/s speed=3.27x    
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid

fontcolor_expr的例子不多,所以我想看看有没有人有这方面的经验。关于在不创建多个过滤器的情况下动态更改字体颜色的任何提示?

请注意 fontcolor_expr=whitefontcolor_expr=888888 是有效的,并且使用固定的字体颜色创建视频。

我已经弄清楚了它的用途:

drawtext=enable='between(t,18.93,20.28)':fontfile=fonts/cousine-bold.ttf:fontsize=144:fontcolor_expr=%{eif\: if(between(t\, 18.93\, 19.02)\, 0xFFFFFF\, 0xFFB6C1) \: x}:x=82:y=288:text='PROGRAMMING'

具体来说:

fontcolor_expr=%{eif\: if(between(t\, 18.93\, 19.02)\, 0xFFFFFF\, 0xFFB6C1) \: x}

据我所知,eif :expression :format :decimal[optional] 允许表达式为 运行 并以特定格式返回。通过 运行 在 eif 中使用 if 语句并在末尾指定 x,它 returns 表达式的结果作为十六进制值。

就是为了以后有人省事,什么都得好好转义。在 Node.JS 中,我使用字符串插值,我的代码如下所示:

`drawtext=enable='between(t,${groupStart},${groupEnd})':fontfile=${fontFile}:fontsize=${fontSize}:fontcolor_expr=%{eif\\: if(between(t\, ${groupStart}\, ${wordStart})\, 0xFFFFFF\, 0xFFB6C1) \\: x}:x=${Math.floor((fontSize / 1.75) * xDistance)}:y=${Math.floor(fontSize * (lineCount + 1))}:text='${elem[0]}'`

expr_int_format, eif

Evaluate the expression's value and output as formatted integer. The first argument is the expression to be evaluated, just as for the expr function. The second argument specifies the output format. Allowed values are x, X, d and u. They are treated exactly as in the "printf" function. The third parameter is optional and sets the number of positions taken by the output. It can be used to add padding with zeros from the left.

发件人:https://www.mankier.com/1/ffmpeg-all