FFmpeg Adding Text on Video

FFmpeg, a powerful multimedia processing tool, provides developers with the capability to enhance videos by adding text overlays. Whether you want to include subtitles, watermarks, or any other textual information, FFmpeg offers versatile options for text addition. In this comprehensive guide, we will explore the steps to add text to videos using FFmpeg, providing detailed instructions and customization options.

  1. Installing FFmpeg:
    Before you begin, ensure FFmpeg is installed on your system. Download the latest version from the official website (https://ffmpeg.org/download.html) or use package managers like Homebrew on macOS or APT on Linux.
  2. Navigate to the Video File Directory:
    Open a terminal or command prompt and navigate to the directory containing the video file to which you want to add text. Use the cd command to change the directory:
   cd /path/to/video/file/directory
  1. Execute FFmpeg Command – Basic Text Overlay:
    To add a basic text overlay to a video, use the following FFmpeg command:
   ffmpeg -i input_video.mp4 -vf "drawtext=text='Hello, World!':fontsize=24:fontcolor=white:x=10:y=10" -c:a copy output_video_with_text.mp4

Breakdown of the command:

  • -i input_video.mp4: Specifies the input video file.
  • -vf "drawtext=text='Hello, World!':fontsize=24:fontcolor=white:x=10:y=10": Specifies the video filter to draw text on the video.
    • text='Hello, World!': Sets the text to be added.
    • fontsize=24: Sets the font size.
    • fontcolor=white: Sets the font color to white.
    • x=10:y=10: Sets the position of the text at coordinates (10, 10).
  • -c:a copy: Copies the audio stream without re-encoding.
  • output_video_with_text.mp4: Defines the output filename.
  1. Adjusting Parameters:
  • Text Content (text=): Modify the text content as needed.
  • Font Size (fontsize=): Adjust the font size to suit your preferences.
  • Font Color (fontcolor=): Specify the desired font color.
  • Text Position (x= and y=): Set the X and Y coordinates for the text position.
  • Output Filename Format: Customize the output filename based on your preferences.
  1. Executing the Command:
    Press Enter to execute the FFmpeg command. FFmpeg will process the video, adding the specified text overlay. The progress will be displayed in the terminal.
  2. Execute FFmpeg Command – Dynamic Text with Timestamp:
    To add dynamic text with a timestamp to a video, use the following FFmpeg command:
   ffmpeg -i input_video.mp4 -vf "drawtext=text='Timestamp\: %{pts\:hms}':fontsize=18:fontcolor=yellow:x=10:y=10" -c:a copy output_video_with_timestamp.mp4

Breakdown of the command:

  • -i input_video.mp4: Specifies the input video file.
  • -vf "drawtext=text='Timestamp\: %{pts\:hms}':fontsize=18:fontcolor=yellow:x=10:y=10": Specifies the video filter to draw dynamic text with a timestamp.
    • text='Timestamp\: %{pts\:hms}': Sets the text to include the timestamp.
    • fontsize=18: Sets the font size.
    • fontcolor=yellow: Sets the font color to yellow.
    • x=10:y=10: Sets the position of the text at coordinates (10, 10).
  • -c:a copy: Copies the audio stream without re-encoding.
  • output_video_with_timestamp.mp4: Defines the output filename.
  1. Viewing the Output:
    Once the process is complete, navigate to the output directory where the video with added text is saved. Open the generated file using a media player to inspect the changes.
  2. Customization and Additional Options:
    Explore FFmpeg’s extensive documentation for advanced options, such as using different fonts, adjusting text opacity, or animating text. Customizing the command allows you to add text to videos with unique characteristics.

Two common methods how to include some text to the video output is to use subtitles or the overlay technique from the previous chapter. The most advanced option with many possibilities is to use a drawtext filter

FFmpeg Video filter: drawtext

For example, to draw a Welcome message (located in the top-left corner by default) with an Arial font in a black color on a white background, we can use the command (characters are typed on 1 line)

ffmpeg -i input -vf drawtext=fontfile=arial.ttf:text=Welcome output

FFmpeg Dynamic text

The t (time) variable representing the time in seconds enables to change x and y values according to the current time

Horizontal text movement

To move the text horizontally across the video frame, we include the t variable to the expression for the x parameter, for instance to move provided text every second by n pixels in a right-to-left direction, we use an expression x=w-tn. To change the movement to the left-to-right direction, x=w+tn expression is used. For example, to show “Dynamic RTL text” string moving at the top, we use the command

ffmpeg -f lavfi -i color=c=#abcdef -vf drawtext=^ “fontfile=arial.ttf:text=’Dynamic RTL text’:x=w-t*50:^ fontcolor=darkorange:fontsize=30” output

More common use is to scroll a line of text near the bottom, where the text is located in a text file that contains only one very long line. The next example uses the textfile parameter with a value info.txt

ffmpeg -f lavfi -i color=c=orange -vf drawtext=”fontfile=arial.ttf:^ textfile=info.txt:x=w-t*50:y=h-th:fontcolor=blue:fontsize=30″ output

Conclusion:
Adding text to videos using FFmpeg provides a flexible and efficient solution for various scenarios. By following this comprehensive guide, you can use FFmpeg to enhance videos with static or dynamic text overlays, tailoring the output to your specific requirements.

Leave a Comment