FFMPEG - Fading text with background
Asked Answered
V

2

6

I'm trying to fade a text in and out (the text has a background), at the moment, what I have is this command:

1. Blend command

ffmpeg -y -i input.mp4 -filter_complex "drawtext=fontfile=HelveticaNeue.ttf:text='Testing': fontcolor=white:fontsize=40: box=1: [email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2[subtitles];[subtitles][0:v]blend=all_expr='A*(if(between(T,1,2),(T-1),0))+B*(1-(if(between(T,1,2),(T-1),0)))'[out]"  -map '[out]' -map 0:a output.mp4

The command above successfully fades in the drawtext (aka subtitles in this filter), but I haven't managed to make it fade them out for some reason, because changing the numeric values of it don't quite have the result I expect.

I've also tried a command that is less complex but doesn't work too for other reasons:

2. Fade command

ffmpeg -y -i input.mp4 -filter_complex "drawtext=fontfile=HelveticaNeue.ttf:text='Testing': fontcolor=white:fontsize=40: box=1: [email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2[subtitles]; [subtitles]fade=t=in:st=2:d=1,fade=t=out:st=3:d=1[out]"  -map '[out]' -map 0:a output.mp4

This second command fades in and out, but applies to the entire video and not the subtitles part alone.

Any way someone can give me a hand with this?

Virago answered 26/9, 2017 at 10:16 Comment(1)
I found this: ffmpeg.shanewhite.coPreengage
F
4

The quick and dirty method to do this is to split the base video into two, draw the text on one copy, add an alpha channel, apply fades to the alpha, overlay the result onto the other copy.

e.g.

ffmpeg -y -i input.mp4 -filter_complex "[0]split[base][text];[text]drawtext=fontfile=HelveticaNeue.ttf:text='Testing': fontcolor=white:\
fontsize=40: box=1: [email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2,format=yuva444p,fade=t=in:st=2:d=1:alpha=1,fade=t=out:st=3:d=1:alpha=1[subtitles]; \
[base][subtitles]overlay" output.mp4
Frig answered 26/9, 2017 at 19:30 Comment(2)
Very interesting! I thought it would be much slower then a solution where we fade just the subtitles channel but it was quite the contrary, your solution takes less then half then my "1. Blend command", i will probably go with this unless some better option arises, thank you very much for the input!Virago
blend addresses each pixel individually so more mem I/O ops.Frig
F
14

ffmpeg -y -i input.mp4 -filter_complex "[0:v]drawtext=fontfile=Lato-Light.ttf:text='Sample Text':fontsize=40:fontcolor=985a5a:alpha='if(lt(t,2),0,if(lt(t,3),(t-2)/1,if(lt(t,6),1,if(lt(t,7),(1-(t-6))/1,0))))':x=(w-text_w)/2:y=(h-text_h)/2" output.mp4

Follow this link to generate your own ffmpeg command for text fade in fade out: http://ffmpeg.shanewhite.co/

Ferino answered 1/7, 2019 at 13:58 Comment(2)
Had no idea about that tool, thanks for sharing mate!Virago
This tool is awesomeKerriekerrigan
F
4

The quick and dirty method to do this is to split the base video into two, draw the text on one copy, add an alpha channel, apply fades to the alpha, overlay the result onto the other copy.

e.g.

ffmpeg -y -i input.mp4 -filter_complex "[0]split[base][text];[text]drawtext=fontfile=HelveticaNeue.ttf:text='Testing': fontcolor=white:\
fontsize=40: box=1: [email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2,format=yuva444p,fade=t=in:st=2:d=1:alpha=1,fade=t=out:st=3:d=1:alpha=1[subtitles]; \
[base][subtitles]overlay" output.mp4
Frig answered 26/9, 2017 at 19:30 Comment(2)
Very interesting! I thought it would be much slower then a solution where we fade just the subtitles channel but it was quite the contrary, your solution takes less then half then my "1. Blend command", i will probably go with this unless some better option arises, thank you very much for the input!Virago
blend addresses each pixel individually so more mem I/O ops.Frig

© 2022 - 2024 — McMap. All rights reserved.