Slide Text Using Drawtext FFmpeg: Tech Support Guide
In this comprehensive guide, we will discuss the process of sliding text across a video screen at a desired x-point using the FFmpeg drawtext filter. This technique is particularly useful for adding subtitles, captions, or annotations to a video with precision and control.
Understanding the FFmpeg Drawtext Filter
FFmpeg is a powerful, open-source media processing tool that supports a wide range of audio and video formats. The drawtext filter enables users to draw text on a video, offering extensive customization like font, color, size, box, border, and shadow properties. Moreover, this filter accepts basic geometric transformations, allowing us to control the position, movement, and scaling of the text.
Sliding Text Across the Screen: The Basics
To create a sliding text effect, we need to modify the text's position over time using expressions based on the filter's enable and move options. By carefully defining these properties, we can achieve a smooth and accurate x-point movement for our desired duration.
Required Parameters
x: initial x-coordinate where the text starts to appear.y: initial y-coordinate where the text starts to appear.w: video width required to calculate the duration of the movement.movetype: determines how the text moves. Use'slide'for sliding text.
Expressions for Sliding Text
To achieve a smooth slide, you can use the following expressions for the enable and move parameters:
enable='eq(n,0)+gte(on,n)+le(n,d+(on-1))',
move='if(gte(on,n), min(1,(t-n)/(d-1)), 0) * (iw-tw)'
Where:
n: frame number.on: frame where the text should start appearing.d: duration (number of frames) for text visibility.t: time in seconds from the beginning.iw: video width.tw: text width.
Code Example for Sliding Text Across the Screen
Here's a complete FFmpeg command that takes a video input called input.mp4, adds sliding text, and outputs the result to output.mp4:
ffmpeg -i input.mp4 -vf "drawtext=enable='eq(n,0)+gte(on,n)+le(n,d+(on-1))': \
move='if(gte(on,n), min(1,(t-n)/(d-1)), 0) * (iw-tw)': \
x='if(gte(on,n), min((iw-tw)/(d-1)*(n-on), (iw-tw)), 0)': \
y='0': \
fontfile=/path/to/your/font.ttf: \
fontsize=24: \
fontcolor=white: \
box=1: \
boxcolor=black: \
boxborderw=5: \
shadowx=2: \
shadowy=2: \
shadowcolor=black: \
text='Desired Text': \
start_number=0: \
on=50: \
d=150" \
output.mp4
Explanation
enable: text visible from frame 50 (on) to frame 200 (50 + 150 - 1).move: text slides from left to right within 200 frames at a consistent speed.x: initial x-position of the text, adjusted based on the video's width.y: constrains the text to the top of the screen.start_number: sets the frame where subtitle numbering should start (default is 1).
- Reference: FFmpeg documentation: Drawtext Filter
- Book: "FFmpeg: The Essential Guide to Modern Video and Audio Processing" by Alessandro Thei (Link)
- Online resource: FFmpeg Tutorial