Search code examples
videoffmpegdrawtext

ffmpeg - adding text to a video between two times


Hi I have a video which is 20 seconds long, I want to add some text to this video from 00:00:10 seconds to 00:00:15 seconds which just says "Summer Video" and fades in and out using FFMPEG.

I know I have to use the drawtext filter, but has anybody got any examples?

Thank you.


Solution

  • Two methods to do this:

    drawtext

    Use the enable option. Because you want fades you need to add the fade and overlay filters.

    ffmpeg -i input -filter_complex "drawtext=text='Summer Video':enable='between(t,15,20)',fade=t=in:start_time=15:d=0.5:alpha=1,fade=t=out:start_time=19.5:d=0.5:alpha=1[fg];[0][fg]overlay=format=auto,format=yuv420p" -c:a copy output.mp4
    

    subtitles

    Alternatively, make subtitles with the desired time and fades and use the subtitles filter instead:

    ffmpeg -i input -vf subtitles=fade.ass -c:a copy output
    

    The example fade.ass was created in Aegisub. The \fad tag was used to set the fade in/out durations in milliseconds: {\fad(500,500)}Summer Video is 0.5 seconds for both in and out.

    fade.ass example file contents:

    [Script Info]
    ; Script generated by Aegisub 3.2.2
    ; http://www.aegisub.org/
    Title: Default Aegisub file
    ScriptType: v4.00+
    WrapStyle: 0
    ScaledBorderAndShadow: yes
    YCbCr Matrix: None
    
    [Aegisub Project Garbage]
    
    [V4+ Styles]
    Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
    Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1
    
    [Events]
    Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
    Dialogue: 0,0:00:15.00,0:00:20.00,Default,,0,0,0,,{\fad(500,500)}Summer Video
    

    I recommend using the subtitles filter because the subtitles can be easier to use and have more styling options and effects than drawtext.