Search code examples
ffmpegdrawtext

ffmpeg: drawtext fade in/out with zmq


There are several ways to fade in and out text in ffmpeg. But I only found solutions where the actual time is known.

But what can I do, when I don't know the current running time and I would like to fade in and out a text?

Let's say I have an endless stream and I want to fade in a text with zmqsend. And the fade should start immediately. For that my understanding is, that I need to store some time information in a variable and calculate with that. But storing variables is not possible in ffmpeg expressions - right?

For testing purposes here are a playing instance:

ffplay -dumpgraph 1 -f lavfi "color=s=512x288:c=black,zmq,drawtext=text=''"

For adding some text with zmq I can run now:

echo Parsed_drawtext_2 reinit text="Hello\ World,\ what’s\ up?" | zmqsend

Or if I know the running time and after 10 seconds I want the text fade in:

"text='Hello\ World':fontsize=:fontcolor=ffffff:alpha='if(lt(t,10),0,if(lt(t,11),(t-10)/1,if(lt(t,16),1,if(lt(t,17),(1-(t-16))/1,0))))'"

My goal is now to have an expression what I can send, so that ffmpeg starts fading in the text and out after a certain time.

Something like:

now=t,if(lt(t,now+10),0,if(lt(t,now+11),(t-(now+10))/1,if(lt(t,now+16),1,if(lt(t,now+17),(1-(t-(now+16)))/1,0))))

Is there a way to store variables in expression, or is there any other way to realize this?


Solution

  • Expressions can store variables in 10 'registers' numbered 0 to 9. Functions are st(n,value) to store and ld(n) to load value from register n. Registers aren't shared across expressions, so a register within the alpha expression isn't available in the fontcolor expr..etc

    So, you would start the expr like this

    'ifnot(ld(1),st(1,t));if(lt(t,ld(1)+10),0,if(lt(t,ld(1)+11),...'