1

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?

jb_alvarado
  • 169
  • 3
  • 11

1 Answers1

6

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),...'
Gyan
  • 85,394
  • 9
  • 169
  • 201
  • Thank you Gyan! I was reading the doc, but not carefully enough. Now with your keywords I see it! Also the semicolon for separating I missed. – jb_alvarado Nov 11 '19 at 13:36
  • Hi @Gyan, I am new to FFmpeg and still catching up with conditional expressions. Can you please explain how does this work or give me some link to study more about this? My use case is to add a text that fades in while moving into the frame, and moves out while fading out. I got the moving part done but still struggling with giving it a time-varying opacity. TIA! – Jaideep Khare Apr 21 '21 at 15:15