How to Scroll Text Horizontally in FFmpeg

This article provides a quick, step-by-step guide on how to use FFmpeg's drawtext video filter to create a horizontal scrolling text effect, commonly known as a marquee or ticker. You will learn the specific mathematical formulas and FFmpeg commands required to scroll text from right to left across your video, adjust the scrolling speed, and make the text loop infinitely.

To scroll text horizontally in FFmpeg, you must dynamically change the horizontal coordinate (x) of the drawtext filter over time. This is achieved by using the t variable (current time in seconds) alongside the video width (w) and the text width (tw).

The Basic Command for Right-to-Left Scrolling

The most common scrolling effect is a marquee that moves from the right edge of the screen to the left. The following command demonstrates how to apply this effect:

ffmpeg -i input.mp4 -vf "drawtext=text='This is your scrolling marquee text':x='w-t*150':y='h-th-20':fontsize=24:fontcolor=white:box=1:boxcolor=black@0.5" -codec:a copy output.mp4

Explaining the Variables and Logic

  • x='w-t*150': This controls the horizontal position.
    • w represents the width of the input video.
    • t is the current time in seconds.
    • 150 is the speed of the scroll in pixels per second.
    • As time (t) increases, the text moves further to the left (negative direction).
  • y='h-th-20': This controls the vertical position.
    • h is the height of the video.
    • th is the height of the text.
    • h-th-20 places the text 20 pixels above the bottom edge of the video.
  • box=1:boxcolor=black@0.5: This enables a semi-transparent black background behind the text to ensure it remains readable against any video background.

How to Loop the Scrolling Text Infinitely

If your video is long, the text in the command above will eventually scroll off the screen and never return. To make the text loop continuously back to the right side of the screen once it has fully exited on the left, use the modulo (mod) function:

ffmpeg -i input.mp4 -vf "drawtext=text='This text loops infinitely':x='w-mod(t*150\,w+tw)':y='h-th-20':fontsize=24:fontcolor=white:box=1:boxcolor=black@0.5" -codec:a copy output.mp4

Breakdown of the Loop Formula:

  • w+tw: This is the total distance the text needs to travel to completely appear and disappear (video width plus text width).
  • mod(t*150, w+tw): This resets the horizontal position back to the beginning (0) once the distance traveled (t * 150) exceeds the total distance (w + tw).
  • w-mod(...): Subtracting this value from the width (w) ensures the scroll starts from the right edge of the video. (Note: The comma inside the filter must be escaped with a backslash \, so FFmpeg does not confuse it with a filter parameter separator).

By adjusting the multiplier (e.g., changing 150 to 200 for faster speed, or 100 for slower speed), you can easily customize the pace of your scrolling text.