Sideway
output.to from Sideway
Draft for Information Only

Content

FFmpeg Filter
 drawtext
  Configuration
  Syntax
  Parameter
  Text Expansion
  Command
  Filter Example
 Source and Reference

FFmpeg Filter

drawtext

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

Configuration

Configuration of FFmpeg : --enable-libfreetype: to enable compilation of the libfreetype library. --enable-libfrontconfig: to enable default font fallback and the font option needed to be configure --enable-libfribidi: to enable the text_shaping option

Syntax

Parameter

The parameter of filter arelast updated 28Dec2019 boxto draw a box around text with the background color. 1:enable, 0:disable, default=0 boxborderwto set the border width of border around the box with boxcolor. default=0 boxcolorto set the color of box around text. default="white" line_spacingto set the border line spacing of the box. default=0 borderwto set the border width of border around the text with bordercolor. default=0 bordercolorto set the border color of border around the text. default="black" expansiont set how the text is expanded. value=none, strftime (deprecated), or normal (default. basetimeto set a start time in microseconds for the count. Only applied in the deprecated strftime expansion mode. To emulate in normal expansion mode use the pts function with the start time in seconds as the second argument. fix_boundsto check and fix text coords to avoid clipping if fix_bounds=true fontcolorto set the text color. default="black" fontcolor_exprstring which is expanded the same way as text to obtain dynamic fontcolor. default=empty and is not processed. if option is set, fontcolor is overrided. fontto set the font family of text. default="San". fontfileto set the font file to be used for drawing text. Path must be include. This parameter is mandatory if the fontconfig support is disabled. alphato apply alpha blending for drawing text. value=0.0 to 1.0. The expression accepts the same variables x, y as well. default=1 fontsizeto set the font size for drawing text. default=16 text_shapingattempt to shape the text before drawing if set to 1, otherwise, just draw the text exactly as given. default=1 if supported. ft_load_flagsto set flag for loading the fonts. The flags map the corresponding flags supported by libfreetype, and are a combination of following values:default, no_scale, no_hinting, render, no_bitmap, vertical_layout, force-autohint, crop_bitmap, pedantic, ignor_global_advance_width, no_recurse, ignore_transform, monochrome, linear_design, no_autohint. default="default" shadowcolorto set the color for drawing a shadow behind the drawn text. default="black" shadowx, shadowyto set the x and y offsets for the text shadow position with respect to the position of the text. Both positive or negative values are accepted. default=0 for both x and y start_numberto set the starting frame number for the n/frame_num variable. default="0" tabsizeto set the tab size in number of spaces. default=4 timecodeto set the initial timecode in format "hh:mm:ss[:;.]ff". can be used with or without text parameter and timecode_rateoption must be specified. timecode_rate, rate, rto set the timecode frame rate for timecode only. Value will be rounded to nearest integer. Minimum value is "1". Drop-frame timecode is supported for frame rates 30 and 60. tc24hmaxthe output of the timecode option will wrap around at 24 hours if set to 1. default=0 (disabled) textto set 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. textfileto specify the 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. reloadthe textfile will be reloaded before each frame if set to 1. Be sure to update it atomically, or it may be read partially, or even fail. x, yto specify the offsets where text will be drawn within the video frame with reference relative to the top/left border of the output image. default=0 for both x and y.
darinput display aspect ratio. equal to (w/h)*sar. hsub, vsubhorizontal and vertical chroma subsample values. e.g. format "yuv422p", hsub is 2 and vsub is 1. line_h, lhthe height of each text line main_h, h, Hthe input height main_w, w, Wthe input width max_glyph_a, ascentthe maximum distacne from the baseline to the highest/upper grid coordinate used to place a glyph outline point, for all the rendered glyphs. It is a positive value due to the grid's orientation, with the Y-axis upwards. max_glyph_hmaximum glyph height, that is the maximum height for all the glyphs contained in the rendered text, it is equivalent to ascent-descent. max_glyph_wmaximum glyph width, that is the maximum width for all the glyphs contained in the rendered text. nthe number of input frame, starting from 0 sarthe input sample aspect ratio ttimestamp expressed in seconds, NAN if the input timestamp is unknown. text_h, ththe height of the rendered text text_w, twthe width of the rendered text x, ythe x and y offset coordinates where the text is drawn. These parameters allow the x and y expressions to refer to each other, e.g. y=x/dar. pict_typeA one character description of the current frame's picture type. pkt_posthe current packet's position in the input file or stream (in btyes, from the start of the input). A value of -1 indicates this info is not available. pkt_durationthe current packet's duration, in seconds. pkt_sizethe current packet's size (in bytes).

Text Expansion

If expansion is set to strftime, the filter recognizes strftime() sequences in the provided text and expands them accordingly. This feature is deprecated. If expansion is set to none, the text is printed verbatim. If expansion is set to normal, which is the default, the following expansion mechanism is used.
The backslash character '\", followed by any character, always expands to the second character. Sequences of the form %{⋯} are expanded. The text between the braces is a function name, possibly followed by arguments separated by ':'. If the arguments contain special characters or delimiters (':' or '}'), they should be escaped. Note that they probably must also be escaped as the value for the text option in the filter argument string and as the filter argument in the filtergraph description, and possibly also for the shell, that makes up to four levels of the escaping; using a test file avoidsthese problems. expr, eThe expression evaluation result. It must take one argument specifying the expression to be evaluated, which accepts the same constants and functions as the x and y values. Note that not all constants shuld be used, for example the text size is not known when evaluating the expression, so the constants text_w and text_h will have an undefined value. expr_int_format, eifEvaluate the expression;s value and output as formatted integer. The first argument is the expression to be evaluated, just as for teh expr function. The second argument specifies the output format. Allowed values are '‘x’, ‘X’, ‘d’ and ‘u’. They are treated exactly as in the printf function. The third parameter is optional and sets the number of positions taken by the output. It can be used to add padding with zeros from the left. gmtimeThe time at which the filter is running, expressed in UTC. It can accept an argument a strftime() format string. localtimeThe time at which the filter is running, expressed in the local time zone. It can accept an argument a strftime() format string. metadataFrame metadata. Takes one or two arguments. The first argument is mandatory and specifies the metadata key. The second argument is optional and specifies a default value, used when the metadat key is not found or empty. Available metadata can be identified by inspecting entries starting with TAG included within each frame section printed by running ffprobe -show_frames. String metadata generated in filters leading to the drawtext filter are also available. n, frame_numThe frame number, starting from 0. pict_typeA one character description of the current picture type. ptsThe timestamp of the current frame. It can take up to three arguments. The first argument is the format of the timestamp. It defaults to flt for seconds as a decimal number with microsecond accuracy; hms stands for a formatted [-]HH:MM:SS.mmm timestamp with millisecond accuracy. gmtime stands for the timestamp of the frame formatted as UTC time. localtime stands for the timestamp of the frame formatted as local time zone time. The second argument is an offset added to the timestamp. If the format isset to hms, a third argument 24HH may be supplied to present the hour part of the formatted timestamp in 24h format (00-23). If the format is set to localtime or gmtime, a third argument may be supplied a strftime() format string. By default, YYYY-MM-DD HH:MM:SS format will be used.

Command

This filter supports altering parameters via commands: reinitAlter existing filter parameters. Syntax for the argument is the same as for filter invocation. If the entire argument cannot be parsed or applied as valid values then the filter will continue with its existing parameters. For example argument, fontsize=56:fontcolor=green:text='Hello World' Full filter invocation with sendcmd is sendcmd=c='56.0 drawtext reinit fontsize=56\:fontcolor=green\:text=Hello\\ World'

Filter Example

Example references from https://ffmpeg.org/ffmpeg-filters.html
  • Draw "Test Text" with font FreeSerif, using the default values for the optional parameters. drawtext="fontfile=/usr/share/fonts/truetype/freefont/FreeSerif.ttf: text='Test Text'"
  • Draw ’Test Text’ with font FreeSerif of size 24 at position x=100 and y=50 (counting from the top-left corner of the screen), text is yellow with a red box around it. Both the text and the box have an opacity of 20%. drawtext="fontfile=/usr/share/fonts/truetype/freefont/FreeSerif.ttf: text='Test Text':\
    x=100: y=50: fontsize=24: fontcolor=yellow@0.2: box=1: boxcolor=red@0.2"
    Note that the double quotes are not necessary if spaces are not used within the parameter list.
  • Show the text at the center of the video frame: drawtext="fontsize=30:fontfile=FreeSerif.ttf:text='hello world':x=(w-text_w)/2:y=(h-text_h)/2"
  • Show the text at a random position, switching to a new position every 30 seconds: drawtext="fontsize=30:fontfile=FreeSerif.ttf:text='hello world':x=if(eq(mod(t\,30)\,0)\,rand(0\,(w-text_w))\,x):y=if(eq(mod(t\,30)\,0)\,rand(0\,(h-text_h))\,y)"
  • Show a text line sliding from right to left in the last row of the video frame. The file LONG_LINE is assumed to contain a single line with no newlines. drawtext="fontsize=15:fontfile=FreeSerif.ttf:text=LONG_LINE:y=h-line_h:x=-50*t"
  • Show the content of file CREDITS off the bottom of the frame and scroll up. drawtext="fontsize=20:fontfile=FreeSerif.ttf:textfile=CREDITS:y=h-20*t"
  • Draw a single green letter "g", at the center of the input video. The glyph baseline is placed at half screen height. drawtext="fontsize=60:fontfile=FreeSerif.ttf:fontcolor=green:text=g:x=(w-max_glyph_w)/2:y=h/2-ascent"
  • Show text for 1 second every 3 seconds:drawtext="fontfile=FreeSerif.ttf:fontcolor=white:x=100:y=x/dar:enable=lt(mod(t\,3)\,1):text='blink'"
  • Use fontconfig to set the font. Note that the colons need to be escaped. drawtext='fontfile=Linux Libertine O-40\:style=Semibold:text=FFmpeg'
  • Print the date of a real-time encoding (see strftime(3)):drawtext='fontfile=FreeSans.ttf:text=%{localtime\:%a %b %d %Y}'
  • Show text fading in and out (appearing/disappearing):
    #!/bin/sh
    DS=1.0 # display start
    DE=10.0 # display end
    FID=1.5 # fade in duration
    FOD=5 # fade out duration
    ffplay -f lavfi "color,drawtext=text=TEST:fontsize=50:fontfile=FreeSerif.ttf:fontcolor_expr=ff0000%{eif\\\\: clip(255*(1*between(t\\, $DS + $FID\\, $DE - $FOD) + ((t - $DS)/$FID)*between(t\\, $DS\\, $DS + $FID) + (-(t - $DE)/$FOD)*between(t\\, $DE - $FOD\\, $DE) )\\, 0\\, 255) \\\\: x\\\\: 2 }"
  • Horizontally align multiple separate texts. Note that max_glyph_a and the fontsize value are included in the y offset. drawtext=fontfile=FreeSans.ttf:text=DOG:fontsize=24:x=10:y=20+24-max_glyph_a, drawtext=fontfile=FreeSans.ttf:text=cow:fontsize=24:x=80:y=20+24-max_glyph_a

Source and Reference

https://ffmpeg.org/ffmpeg-filters.htmllast updated 28Dec2019
libfreetype: http://www.freetype.org/
fontconfig: http://freedesktop.org/software/fontconfig/fontconfig-user.html
libfribidi: http://fribidi.org/

©sideway

ID: 191202802 Last Updated: 12/28/2019 Revision: 0


Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019