How to overlay text on video using FFMPEG

September 26, 2018 in Development

...

FFMPEG is a handy tool that can be used to edit video from the command line. Using ffmpeg you can perform many different edits to videos. One effect you can accomplish is overlaying text on video. To follow along you can download the sample file which is a free stock file provided by Pexel Video.

The Script

ffmpeg -i D:\Videos\city.mp4 -filter_complex "[0:v]drawtext=fontfile='C\:\\Windows\\Fonts\\arial.ttf':text='A Celebration of Life':fontsize=64:fontcolor=white" D:\Videos\text.mp4

The command above creates the following video.

Dissecting the command

FFMPEG

The first parameter we pass into the command line is 'ffmpeg'. This is telling the command prompt that we want to execute the command using the ffmpeg library. In order to use this command you will need to set ffmpeg as an environment variable. You can find out how to do that at this Wiki Tutorial

-i

Next in the command we notice the "-i". The -i” is what is known as a “switch”. The role of switches is to describe the next piece of information. For example, saying “-i” tells ffmpeg that the next piece of information is an input file name.

D:\Videos\city.mp4

This is the input file that we downloaded from Pexels. Notice how we are referencing the full path. If you downloaded your video in a different path you will need to change this to match the appropriate file location.

-filter_complex

The filter_complex command tells ffmpeg that we want to use one of their complex filters. There are many different filters available through the ffmpeg complex_filters, one of those filters is the drawtext command. Which is the command we will be using to generate the text. After we tell ffmpeg that we want to use a complex filter we need to wrap that filter in double quotes. *Be careful that you use the " character and not any other type of quotes.

[0:v]

The [0:v] tells the filter which channel we want to apply the filter to. The 0 stands for the zeroth video(if you are familiar with computer science we like to start counting at 0 as opposed to starting at 1). The stands for the video channel, as opposed to the [0:a] which would apply the filter to the audio channel.

drawtext

The drawtext is a filter that allows us to overlay text on a video. The drawtext filter takes many parameters but for this example we are only going to be using the bare minimum parameters which is: fontfile, text, color, and size. You can learn more about the drawtext filter at the official ffmpeg documentation

fontfile='C\:\\Windows\\Fonts\\arial.ttf'

The fontfile is where we specify which font we want to use for our text effect. You can use any font you wish. On a Windows Machine you can find the fonts located in the C:\Windows\fonts folder. You can also download a Google font and reference that font file. *One thing to be careful of when referencing a font is that the : character is a special character in the drawtext command and has to be escaped by using the \ character. If you get the error "Cannot find a valid font for the family Sans Error initializing filter 'drawtext' with args..." this is because you are not referencing the font correctly or you have a colon in your font path.  

text='This text was generated via ffmpeg'

This is the command that tells ffmpeg what text we want to overlay on the video. Inside the quotes you can type whatever you want, and ffmpeg will overlay the video with your text.

fontsize=64

This is what tells ffmpeg the size of the font we want. You can experiment with different sizes to find the right fit for your video.

fontcolor=black

The fontcolor is pretty self explanatory. Simply type in the color you wish, and your text will be that color.

D:\Videos\text.mp4

The above argument is the path where you want ffmpeg to put your output video. If you don't specify the full path the video will be placed where ever your ffmpeg executable is located.

Are you trying to generate dynamic videos for your customers?

Schedule a call with us to see how we can help you.

Sign Up for Our Mailing List