Use the drawtext filter for simple text on video. If you need more complex timing, formatting, or dynamic text see the subtitles filter. This answer focuses on the drawtext filter.
Example
Print Stack Overflow
in white text onto center of video, with black background box of 50% opacity:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:[email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2" -codec:a copy output.mp4
- The audio is stream copied in this example (like a copy and paste).
@0.5
controls background box opacity. 0.5 is 50%. Remove @0.5
if you do not want any transparency.
- See the drawtext filter documentation for a complete list and explanations of options.
Preview
You can use ffplay
to preview your text without having to wait for a file to encode:
ffplay -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:[email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2" input.mp4
Alternatively you can use mpv
but the syntax is slightly different:
mpv --vf="lavfi=[drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:[email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2]" input.mp4
Multiple texts
You can chain multiple drawtext filters:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Stack Overflow':fontcolor=white:fontsize=24:box=1:[email protected]:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2,drawtext=fontfile=/path/to/font.ttf:text='Bottom right text':fontcolor=black:fontsize=14:x=w-tw-10:y=h-th-10" -codec:a copy output.mp4
Position
x
and y
determine text position:
Position |
x :y |
With 10 px padding |
Top left |
x=0:y=0 |
x=10:y=10 |
Top center |
x=(w-text_w)/2:y=0 |
x=(w-text_w)/2:y=10 |
Top right |
x=w-tw:y=0 |
x=w-tw-10:y=10 |
Centered |
x=(w-text_w)/2:y=(h-text_h)/2 |
|
Bottom left |
x=0:y=h-th |
x=10:y=h-th-10 |
Bottom center |
x=(w-text_w)/2:y=h-th |
x=(w-text_w)/2:y=h-th-10 |
Bottom right |
x=w-tw:y=h-th |
x=w-tw-10:y=h-th-10 |
Random |
See this answer |
|
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…