eladg / ffmpeg-by-example

Creative Commons Attribution 4.0 International
8 stars 5 forks source link

Fade In and Out text using the 'drawtext' filter - FFmpeg By Example #74

Open eladg opened 2 years ago

eladg commented 2 years ago

https://www.ffmpegbyexample.com/examples/50gowmkq/fade_in_and_out_text_using_the_drawtext_filter/

This FFmpeg command uses the color filter source to generate an empty black frame. For more info about this filter, see the Generate a solid color example.

On top of this blank input, we use the drawtext filter to generate the overlayed text layer. To review the complete list of parameters, please refer to the official documentation. On this example we use the alpha,fontcolor, fontsize,text, x, and y filter parameters.

By far, the most complex value is for the alpha parameter, which we will break down last.

fontcolor=ffffff for white color text, see ffmpeg Color for list of supported values). fontsize=64 sets the size of the font to 64px. text=\"drawtext\" text with fade in & out is the text string to be generated. Lastly. the x/y is the position on the screen that the text layer should be overlayed. Notice that in this context, w and h is the size of the input frame and text_w and text_h is the size of the text overlay.

Finaly, the alpha param to the drawtext filter is where the fade in/out magic happens. The string is a set inlin 'larger than' (lt) and if conditions. For more info about these Expression Evaluation functions, please refer to the official documentation

  lt(t\,0.3)\,          <---- X1  returns 0 if t < 0.3
  0\,                   <---- Y1
  if (                  <---- Z1
    lt(t\,1.3)\,          <---- X2  returns a value between 0-1 (ascending)
    (t-0.3)/1\,           <---- Y2  for 0.3 > t > 1.3
    if (                  <---- Z2
      lt(t\,3)\,            <---- X3  returns 1 if t<3
      1\,                   <---- Y3
      if (                  <---- Z3
        lt(t\,4)\,             <---- X4  returns a value between 0-1 (descending)
        (1-(t-3))/1\,          <---- Y4  for 3 > t > 4
        0                      <---- Z4 
      )
    )
  )
)

based on the drawtext examples section.