ffmpeg # 为视频添加文字drawtext

Draw a text string or text from a specified file on top of a video, using the libfreetype library.

To enable compilation of this filter, you need to configure FFmpeg with –enable-libfreetype.
To enable default font fallback and the font option you need to configure FFmpeg with –enable-libfontconfig.
To enable the text_shaping option, you need to configure FFmpeg with –enable-libfribidi.

ffmpeg中用drawtext filter为视频添加文字。

如果想要使用drawtext,编译时需要加上–enable-libfreetype。
如果要选择多种字体,需要加上–enable-libfontconfig。
如果还需要字体变形,需要加上–enable-libfribidi。

drawtext 参数简介

-vf drawtext=fontcolor=white:fontsize=40:fontfile=msyh.ttf:text='Hello World':x=0:y=100

The font size to be used for drawing text. The default value of fontsize is 16.

默认16

  • fontfile 字体库文件

The font file to be used for drawing text.
The path must be included.
This parameter is mandatory(强制的) if the fontconfig support is disabled.

  • text 要加的文字

The text string to be drawn. The text must be a sequence of UTF-8 encoded characters. This parameter is mandatory if no file is specified with the parameter textfile.

  • textfile

A text file containing text to be drawn. The text must be a sequence of UTF-8 encoded characters.
This parameter is mandatory if no text string is specified with the parameter text.
If both text and textfile are specified, an error is thrown.

通过文件指定要添加的字体。
text and textfile不能同时存在。

  • x=0:y=100 在什么坐标加文字

The expressions which specify the offsets where text will be drawn within the video frame. They are relative to the top/left border of the output image.
The default value of x and y is “0”.

相对于左上角的坐标。
默认x=0, y=0

通过text添加文字

ffmpeg -i in.mp4 -vf drawtext=fontcolor=white:fontsize=40:fontfile=msyh.ttf:text='Hello World':x=0:y=100 -vframes 20 -y out.mp4

image.png

通过textfile添加文字

ffmpeg -i in.mp4 -vf drawtext=fontcolor=white:fontsize=40:fontfile=msyh.ttf:textfile='test.txt':x=0:y=20 -vframes 20 -y out.mp4

其中test.txt中的内容为:

Good Moring
Hello World
中华人民共和国

image.png

image.png

References:

https://ffmpeg.org/ffmpeg-all.html#drawtext-1

作者:FlyingPenguin
链接:https://www.jianshu.com/p/9d24d81ca199
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

发表评论