终于完工! ffmpeg 视频滤镜:添加文本-drawtext

滤镜描述

drawtext

官网链接 =》 FFmpeg Filters Documentation 

这个滤镜可以给视频添加上文本,可以给文本加边框、颜色、阴影。注意不是字幕功能,因为这个滤镜不能精准的控制开始和结束的时间。

滤镜使用

参数
   fontfile          <string>     ..FV....... set font file
   text              <string>     ..FV.....T. set text
   textfile          <string>     ..FV....... set text file
   fontcolor         <color>      ..FV.....T. set foreground color (default "black")
   fontcolor_expr    <string>     ..FV....... set foreground color expression (default "")
   boxcolor          <color>      ..FV.....T. set box color (default "white")
   bordercolor       <color>      ..FV.....T. set border color (default "black")
   shadowcolor       <color>      ..FV.....T. set shadow color (default "black")
   box               <boolean>    ..FV.....T. set box (default false)
   boxborderw        <string>     ..FV.....T. set box borders width (default "0")
   line_spacing      <int>        ..FV.....T. set line spacing in pixels (from INT_MIN to INT_MAX) (default 0)
   fontsize          <string>     ..FV.....T. set font size
   text_align        <flags>      ..FV.....T. set text alignment (default 0)
     left                         ..FV.....T.
     L                            ..FV.....T.
     right                        ..FV.....T.
     R                            ..FV.....T.
     center                       ..FV.....T.
     C                            ..FV.....T.
     top                          ..FV.....T.
     T                            ..FV.....T.
     bottom                       ..FV.....T.
     B                            ..FV.....T.
     middle                       ..FV.....T.
     M                            ..FV.....T.
   x                 <string>     ..FV.....T. set x expression (default "0")
   y                 <string>     ..FV.....T. set y expression (default "0")
   boxw              <int>        ..FV.....T. set box width (from 0 to INT_MAX) (default 0)
   boxh              <int>        ..FV.....T. set box height (from 0 to INT_MAX) (default 0)
   shadowx           <int>        ..FV.....T. set shadow x offset (from INT_MIN to INT_MAX) (default 0)
   shadowy           <int>        ..FV.....T. set shadow y offset (from INT_MIN to INT_MAX) (default 0)
   borderw           <int>        ..FV.....T. set border width (from INT_MIN to INT_MAX) (default 0)
   tabsize           <int>        ..FV.....T. set tab size (from 0 to INT_MAX) (default 4)
   basetime          <int64>      ..FV....... set base time (from I64_MIN to I64_MAX) (default I64_MIN)
   font              <string>     ..FV....... Font name (default "Sans")
   expansion         <int>        ..FV....... set the expansion mode (from 0 to 2) (default normal)
     none            0            ..FV....... set no expansion
     normal          1            ..FV....... set normal expansion
     strftime        2            ..FV....... set strftime expansion (deprecated)
   y_align           <int>        ..FV.....T. set the y alignment (from 0 to 2) (default text)
     text            0            ..FV....... y is referred to the top of the first text line
     baseline        1            ..FV....... y is referred to the baseline of the first line
     font            2            ..FV....... y is referred to the font defined line metrics
   timecode          <string>     ..FV....... set initial timecode
   tc24hmax          <boolean>    ..FV....... set 24 hours max (timecode only) (default false)
   timecode_rate     <rational>   ..FV....... set rate (timecode only) (from 0 to INT_MAX) (default 0/1)
   r                 <rational>   ..FV....... set rate (timecode only) (from 0 to INT_MAX) (default 0/1)
   rate              <rational>   ..FV....... set rate (timecode only) (from 0 to INT_MAX) (default 0/1)
   reload            <int>        ..FV....... reload text file at specified frame interval (from 0 to INT_MAX) (default 0)
   alpha             <string>     ..FV.....T. apply alpha while rendering (default "1")
   fix_bounds        <boolean>    ..FV....... check and fix text coords to avoid clipping (default false)
   start_number      <int>        ..FV....... start frame number for n/frame_num variable (from 0 to INT_MAX) (default 0)
   text_source       <string>     ..FV....... the source of text
   text_shaping      <boolean>    ..FV....... attempt to shape text before drawing (default true)
   ft_load_flags     <flags>      ..FV....... set font loading flags for libfreetype (default 0)
     default                      ..FV.......
     no_scale                     ..FV.......
     no_hinting                   ..FV.......
     render                       ..FV.......
     no_bitmap                    ..FV.......
     vertical_layout              ..FV.......
     force_autohint               ..FV.......
     crop_bitmap                  ..FV.......
     pedantic                     ..FV.......
     ignore_global_advance_width              ..FV.......
     no_recurse                   ..FV.......
     ignore_transform              ..FV.......
     monochrome                   ..FV.......
     linear_design                ..FV.......
     no_autohint                  ..FV.......
box 是不是给文字添加背景 1 (enable) 0(disable).
boxborderw 设置上下边框的宽度,举个例子 boxborderw=10|20|30|40,分别是上边框|右边框|底边框|左边框
boxcolor 背景的颜色,可以输入颜色名字(ffmpeg -colors),也可以输入颜色值(比如#afeeee)
y_align 想象一下画面就是一个二维坐标系,当把文字放到坐标系的时候,文字的那一部分与坐标点对齐。

text-最高处与坐标对齐

baseline-文字的中间与坐标对齐

borderw 文字的描边的宽度
bordercolor 文字描边的颜色
expansion 文本扩展功能,现在就是normal和none两个值了,默认是normal,意思是你可以在text中使用函数 normal支持的函数参考toc-Text-expansion
fix_bounds 当你的文本要超出视频的左边的时候,这个值打开的时候,会修复超出边界问题,一直保持着不超出视频的左边
fontcolor 字体的颜色
fontcolor_expr 颜色的表达式,可以实现动态颜色
font 字体名字
fontfile 字体文件的路径
fontsize 字体大小
shadowcolor 阴影的颜色
shadowx、shadowy 阴影的位置
start_number 设置变量的n的开始数字
text 要写的文字
textfile 文字可以保存在文件中,这里写文件的路径
reload 经过多少帧重新加载一次textfile

我测试了一下boxh boxw line_spacing text_align,感觉他们没有起作用,不知道啥原因(有清楚的大神请给留个言)

案例

原图

ffmpeg  -y -i ffm_media/wonder_girls.jpg  -filter_complex "drawtext=textfile='ffm_media/drawtext.txt':fontsize=80:x=(w-text_w)/2:y=h-text_h+5:fontcolor=white" result/drawtext/drawtext_test_002.jpg

设置了字体,文本保存在了文件中了

ffmpeg  -y -i ffm_media/wonder_girls.jpg  -filter_complex "drawtext=text='hello wonder girls':fontsize=80:x=(w-text_w)/2:y=h-text_h:fontcolor=white:borderw=5:bordercolor=pink" result/drawtext/drawtext_test_005.jpg

设置了字体的描边

 

ffmpeg  -y -i ffm_media/wonder_girls.jpg  -filter_complex "drawtext=text='hello wonder girls':fontsize=80:x=(w-text_w)/2:y=h-text_h:fontcolor=white:borderw=5:shadowcolor=green:shadowx=5:shadowy=7" result/drawtext/drawtext_test_007.jpg

设置了字体的阴影

ffmpeg  -y -i ffm_media/wonder_girls.jpg  -filter_complex "drawtext=text='hello wonder  girls':fontsize=80:x=(w-text_w)/2:y=h-text_h-50:fontcolor=white:box=1:boxborderw=10|20|30|40:boxcolor=blue" result/drawtext/drawtext_test_009.jpg

设置了背景

ffmpeg  -y -i ffm_media/short_dancer.mp4 -ss 3 -filter_complex "drawtext=text='%{localtime} frame %{n}':fontsize=40:x=(w-text_w)/2:y=h-text_h-n:fontcolor=white:box=1:boxborderw=10|20|30|40:boxcolor=blue:y_align=baseline" result/drawtext/drawtext_dancer_005.mp4

设置动态移动的文字,这里主要是纵向移动,是因为在y变量中应用了 n(当前的帧数) 这个变量

ffmpeg  -y -i ffm_media/short_dancer.mp4  -filter_complex "drawtext=text='%{localtime} frame %{n}':fontsize=40:x=(w-text_w)/2:y=h-text_h-n:fontcolor_expr='%{eif\\:if(between(t\, 0\, 3)\, 0xFFFFFF\, 0xFFB6C1) \\: x}':box=1:boxborderw=10|20|30|40:boxcolor=blue" result/drawtext/drawtext_dancer_006.mp4

 动态改变颜色的,这个是根据时间判断的,3秒前是一个颜色,3秒以后是另一个颜色。

## 祝你好运
 
hope("大神多多指点")
topic("有问题可以交流呀")
concat("球球QUN", "61480", "9646")

猜你喜欢

转载自blog.csdn.net/u014686399/article/details/143407049