Creating video containing animated text using FFMPEG alone

6

8

I doubt this is possible, but am trying to find some way to

  1. dynamically render text, and then
  2. optionally animate it (simple left to right movement) and then
  3. composite the result on top of another video.

I know FFMPEG is powerful, but is it possible to generate content dynamically like this using FFMPEG alone? The video would be input, but the text would have to be generated entirely using FFMPEG.

Bachalo

Posted 2015-02-06T16:31:01.197

Reputation: 1 515

What exactly do you mean by dynamic? Is the text known when you call ffmpeg? Have you seen the drawtext filter, and did you already try something? Please note that Node.js programming questions are off topic, so I've removed this part from your question. If you have an issue specific to Node.js, you could post about it on [SO]. FFmpeg commandline questions are on topic here, of course. – slhck – 2015-02-06T16:48:02.527

yes the text would be known.... no I haven't seen the drawtext filter...will look into it... any chance you could provide a quick code snippet? – Bachalo – 2015-02-06T16:56:26.893

Answers

8

You can use the drawtext filter.

Dynamic text

The text can be changed during encoding if you use textfile and reload options:

  • textfile A text file containing text to be drawn. The text must be a sequence of UTF-8 encoded characters.

  • reload If set to 1, the textfile will be reloaded before each frame. Be sure to update it atomically, or it may be read partially, or even fail.

Animate

The text can move around using expressions within the x and y drawtext options. See the drawtext documentation for a list of constants and functions.

Timeline editing

Some filters, such as drawtext, support the enable option meaning you can turn the filter off and on. You can see what filters support timeline with:

ffmpeg -filters

Example

If you have an input video that you want to overlay with text:

ffmpeg -i input -vf "drawtext=enable='gte(t,3)':fontfile=Vera.ttf:textfile=text.txt:reload=1:y=h-line_h-10:x=(W/tw)*n" output
  • This will enable the drawtext filter after 3 seconds
  • Every time text.txt is updated the text will change
  • The words will move on the screen from the left to the right (I suck at these expressions but you'll get the idea)

If you have no input video and would just like to generate the text on black background:

ffmpeg -f lavfi -i "color=color=black, drawtext=enable='gte(t,3)':fontfile=Vera.ttf:fontcolor=white:textfile=text.txt:reload=1:y=h-line_h-10:x=(W/tw)*n" -t 5 output

Here, -t 5 specifies the overall length of the output. (If not specified, the encoding would run forever.) The font color is set to white so the text becomes visible.

llogan

Posted 2015-02-06T16:31:01.197

Reputation: 31 929

thanks! will need some testing but a good start – Bachalo – 2015-02-07T00:12:36.327

it shows me input: No such file or directory Actually i just want to make video from text. just like marque work on browser – manish1706 – 2017-06-28T11:27:06.857

here is the command i am using ffmpeg -i input -vf "drawtext=enable='gte(t,3)':fontfile=verdana.ttf:textfile=text.txt:reload=0:y=h-line_h-10:x=(W/tw)*n" output And on same directory i have put one text.txt file with some text – manish1706 – 2017-06-28T12:04:15.820

1@manish1706 As the error says, there is no input file. If you have no video that you want to add text to, you have to generate a black background. See the newly added example above. – slhck – 2017-06-28T12:18:42.170

1Thank you very much @slhck for the help, and i got success for the text with this command ** ffmpeg -f lavfi -i "color=color=yellow, drawtext=enable='gte(t,0)':text=Text1 Text2 Text3 Text4 Text5 Text6 Text7 Text8 Text9 Text10:expansion=normal:fontfile=verdana.ttf: y=h-line_h-120:x=-100*t: fontcolor=white: fontsize=50" -t 20 Output.mp4 ** and video is working well. now i just need to understand how can i pass the html like character for separator between two string, eg: text 1 (dot mark) text2 (dot mark) text3 – manish1706 – 2017-06-29T09:47:19.203

2

@manish1706 Put your text in quotes and you should be able to add any character you want. If you have lots of text, you can also read it from a file using the textfile option instead of text. See http://ffmpeg.org/ffmpeg-all.html#drawtext-1

– slhck – 2017-06-29T11:41:08.203

@slhck for the single quote, i have to render it by php itself. can you please let me know how i can make video time according to text width. eg: banana, mango, grapes, apple, watermelon is text and my crawling text should start with banana and video should ends when watermelon text finish its crawling. Thank you in anticipation @slhck – manish1706 – 2017-07-03T12:06:40.090

@manish1706 I can't give you a simple formula or code for this. You have to calculate how long it takes for one character to scroll across the screen (t), depending on the frame rate and width. Then figure out how many characters fit on a screen. Based on that, calculate how many screens you need for your overall number of characters, and multiply that by t. Give or take. – slhck – 2017-07-03T12:47:06.717