How to Adjust Line Spacing in FFmpeg drawtext
This guide explains how to control the vertical line spacing of
multi-line text rendered with FFmpeg's drawtext video
filter. By applying the built-in line_spacing parameter,
you can increase or decrease the pixel gap between lines to improve
readability and match your video's layout requirements.
The line_spacing
Option
FFmpeg provides a dedicated option within the drawtext
filter called line_spacing. It defines the additional
vertical space in pixels added between consecutive lines of text.
- Positive values (e.g.,
line_spacing=20) push lines further apart. - Negative values (e.g.,
line_spacing=-5) pull lines closer together, which is useful for fonts with excessively tall default line heights. - The default value is
0, which relies entirely on the font engine's standard line metrics.
Basic Command Syntax
To adjust line spacing on multi-line text using an inline string
containing newline characters (\n), use the following
syntax:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='First Line\nSecond Line\nThird Line':fontsize=36:fontcolor=white:line_spacing=25:x=50:y=50" -codec:a copy output.mp4In this command:
line_spacing=25adds 25 pixels of vertical space between each line.\nseparates each line of text.-codec:a copypreserves the original audio stream without re-encoding.
Using an External Text File
When dealing with paragraphs or longer blocks of text, using a text file is cleaner and prevents shell escaping issues:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:textfile=subtitles.txt:fontsize=32:fontcolor=yellow:line_spacing=15:x=(w-text_w)/2:y=(h-text_h)/2" -codec:a copy output.mp4Here, subtitles.txt contains the raw multi-line text.
The drawtext filter automatically respects standard line
breaks within the file and applies the 15 pixel spacing
between every line.
Decreasing Default Line Spacing
Some TrueType and OpenType fonts include large vertical bounding boxes that result in unwanted gaps between lines. You can tighten the spacing by passing a negative integer:
ffmpeg -i input.mp4 -vf "drawtext=fontfile=/path/to/font.ttf:text='Title\nSubtitle':fontsize=48:fontcolor=white:line_spacing=-10:x=100:y=100" -codec:a copy output.mp4Test different values in increments of 5 to 10 pixels to achieve the desired balance for your chosen font and font size.