|
1 | 1 | from __future__ import unicode_literals
|
2 | 2 |
|
3 |
| -from .nodes import ( |
4 |
| - FilterNode, |
5 |
| - filter_operator, |
6 |
| -) |
| 3 | +from .nodes import FilterNode, filter_operator |
| 4 | +from ._utils import escape_chars |
7 | 5 |
|
8 | 6 |
|
9 | 7 | @filter_operator()
|
@@ -179,6 +177,148 @@ def drawbox(stream, x, y, width, height, color, thickness=None, **kwargs):
|
179 | 177 | return FilterNode(stream, drawbox.__name__, args=[x, y, width, height, color], kwargs=kwargs).stream()
|
180 | 178 |
|
181 | 179 |
|
| 180 | +@filter_operator() |
| 181 | +def drawtext(stream, text=None, x=0, y=0, escape_text=True, **kwargs): |
| 182 | + """Draw a text string or text from a specified file on top of a video, using the libfreetype library. |
| 183 | +
|
| 184 | + To enable compilation of this filter, you need to configure FFmpeg with ``--enable-libfreetype``. To enable default |
| 185 | + font fallback and the font option you need to configure FFmpeg with ``--enable-libfontconfig``. To enable the |
| 186 | + text_shaping option, you need to configure FFmpeg with ``--enable-libfribidi``. |
| 187 | +
|
| 188 | + Args: |
| 189 | + box: Used to draw a box around text using the background color. The value must be either 1 (enable) or 0 |
| 190 | + (disable). The default value of box is 0. |
| 191 | + boxborderw: Set the width of the border to be drawn around the box using boxcolor. The default value of |
| 192 | + boxborderw is 0. |
| 193 | + boxcolor: The color to be used for drawing box around text. For the syntax of this option, check the "Color" |
| 194 | + section in the ffmpeg-utils manual. The default value of boxcolor is "white". |
| 195 | + line_spacing: Set the line spacing in pixels of the border to be drawn around the box using box. The default |
| 196 | + value of line_spacing is 0. |
| 197 | + borderw: Set the width of the border to be drawn around the text using bordercolor. The default value of |
| 198 | + borderw is 0. |
| 199 | + bordercolor: Set the color to be used for drawing border around text. For the syntax of this option, check the |
| 200 | + "Color" section in the ffmpeg-utils manual. The default value of bordercolor is "black". |
| 201 | + expansion: Select how the text is expanded. Can be either none, strftime (deprecated) or normal (default). See |
| 202 | + the Text expansion section below for details. |
| 203 | + basetime: Set a start time for the count. Value is in microseconds. Only applied in the deprecated strftime |
| 204 | + expansion mode. To emulate in normal expansion mode use the pts function, supplying the start time (in |
| 205 | + seconds) as the second argument. |
| 206 | + fix_bounds: If true, check and fix text coords to avoid clipping. |
| 207 | + fontcolor: The color to be used for drawing fonts. For the syntax of this option, check the "Color" section in |
| 208 | + the ffmpeg-utils manual. The default value of fontcolor is "black". |
| 209 | + fontcolor_expr: String which is expanded the same way as text to obtain dynamic fontcolor value. By default |
| 210 | + this option has empty value and is not processed. When this option is set, it overrides fontcolor option. |
| 211 | + font: The font family to be used for drawing text. By default Sans. |
| 212 | + fontfile: The font file to be used for drawing text. The path must be included. This parameter is mandatory if |
| 213 | + the fontconfig support is disabled. |
| 214 | + alpha: Draw the text applying alpha blending. The value can be a number between 0.0 and 1.0. The expression |
| 215 | + accepts the same variables x, y as well. The default value is 1. Please see fontcolor_expr. |
| 216 | + fontsize: The font size to be used for drawing text. The default value of fontsize is 16. |
| 217 | + text_shaping: If set to 1, attempt to shape the text (for example, reverse the order of right-to-left text and |
| 218 | + join Arabic characters) before drawing it. Otherwise, just draw the text exactly as given. By default 1 (if |
| 219 | + supported). |
| 220 | + ft_load_flags: The flags to be used for loading the fonts. The flags map the corresponding flags supported by |
| 221 | + libfreetype, and are a combination of the following values: |
| 222 | +
|
| 223 | + * ``default`` |
| 224 | + * ``no_scale`` |
| 225 | + * ``no_hinting`` |
| 226 | + * ``render`` |
| 227 | + * ``no_bitmap`` |
| 228 | + * ``vertical_layout`` |
| 229 | + * ``force_autohint`` |
| 230 | + * ``crop_bitmap`` |
| 231 | + * ``pedantic`` |
| 232 | + * ``ignore_global_advance_width`` |
| 233 | + * ``no_recurse`` |
| 234 | + * ``ignore_transform`` |
| 235 | + * ``monochrome`` |
| 236 | + * ``linear_design`` |
| 237 | + * ``no_autohint`` |
| 238 | +
|
| 239 | + Default value is "default". For more information consult the documentation for the FT_LOAD_* libfreetype |
| 240 | + flags. |
| 241 | + shadowcolor: The color to be used for drawing a shadow behind the drawn text. For the syntax of this option, |
| 242 | + check the "Color" section in the ffmpeg-utils manual. The default value of shadowcolor is "black". |
| 243 | + shadowx: The x offset for the text shadow position with respect to the position of the text. It can be either |
| 244 | + positive or negative values. The default value is "0". |
| 245 | + shadowy: The y offset for the text shadow position with respect to the position of the text. It can be either |
| 246 | + positive or negative values. The default value is "0". |
| 247 | + start_number: The starting frame number for the n/frame_num variable. The default value is "0". |
| 248 | + tabsize: The size in number of spaces to use for rendering the tab. Default value is 4. |
| 249 | + timecode: Set the initial timecode representation in "hh:mm:ss[:;.]ff" format. It can be used with or without |
| 250 | + text parameter. timecode_rate option must be specified. |
| 251 | + rate: Set the timecode frame rate (timecode only). |
| 252 | + timecode_rate: Alias for ``rate``. |
| 253 | + r: Alias for ``rate``. |
| 254 | + tc24hmax: If set to 1, the output of the timecode option will wrap around at 24 hours. Default is 0 (disabled). |
| 255 | + text: The text string to be drawn. The text must be a sequence of UTF-8 encoded characters. This parameter is |
| 256 | + mandatory if no file is specified with the parameter textfile. |
| 257 | + textfile: A text file containing text to be drawn. The text must be a sequence of UTF-8 encoded characters. |
| 258 | + This parameter is mandatory if no text string is specified with the parameter text. If both text and |
| 259 | + textfile are specified, an error
F438
is thrown. |
| 260 | + reload: If set to 1, the textfile will be reloaded before each frame. Be sure to update it atomically, or it |
| 261 | + may be read partially, or even fail. |
| 262 | + x: The expression which specifies the offset where text will be drawn within the video frame. It is relative to |
| 263 | + the left border of the output image. The default value is "0". |
| 264 | + y: The expression which specifies the offset where text will be drawn within the video frame. It is relative to |
| 265 | + the top border of the output image. The default value is "0". See below for the list of accepted constants |
| 266 | + and functions. |
| 267 | +
|
| 268 | + Expression constants: |
| 269 | + The parameters for x and y are expressions containing the following constants and functions: |
| 270 | + dar: input display aspect ratio, it is the same as ``(w / h) * sar`` |
| 271 | + hsub: horizontal chroma subsample values. For example for the pixel format "yuv422p" hsub is 2 and vsub |
| 272 | + is 1. |
| 273 | + vsub: vertical chroma subsample values. For example for the pixel format "yuv422p" hsub is 2 and vsub |
| 274 | + is 1. |
| 275 | + line_h: the height of each text line |
| 276 | + lh: Alias for ``line_h``. |
| 277 | + main_h: the input height |
| 278 | + h: Alias for ``main_h``. |
| 279 | + H: Alias for ``main_h``. |
| 280 | + main_w: the input width |
| 281 | + w: Alias for ``main_w``. |
| 282 | + W: Alias for ``main_w``. |
| 283 | + ascent: the maximum distance from the baseline to the highest/upper grid coordinate used to place a |
| 284 | + glyph outline point, for all the rendered glyphs. It is a positive value, due to the grid's |
| 285 | + orientation with the Y axis upwards. |
| 286 | + max_glyph_a: Alias for ``ascent``. |
| 287 | + descent: the maximum distance from the baseline to the lowest grid coordinate used to place a glyph |
| 288 | + outline point, for all the rendered glyphs. This is a negative value, due to the grid's |
| 289 | + orientation, with the Y axis upwards. |
| 290 | + max_glyph_d: Alias for ``descent``. |
| 291 | + max_glyph_h: maximum glyph height, that is the maximum height for all the glyphs contained in the |
| 292 | + rendered text, it is equivalent to ascent - descent. |
| 293 | + max_glyph_w: maximum glyph width, that is the maximum width for all the glyphs contained in the |
| 294 | + rendered text |
| 295 | + n: the number of input frame, starting from 0 |
| 296 | + rand(min, max): return a random number included between min and max |
| 297 | + sar: The input sample aspect ratio. |
| 298 | + t: timestamp expressed in seconds, NAN if the input timestamp is unknown |
| 299 | + text_h: the height of the rendered text |
| 300 | + th: Alias for ``text_h``. |
| 301 | + text_w: the width of the rendered text |
| 302 | + tw: Alias for ``text_w``. |
| 303 | + x: the x offset coordinates where the text is drawn. |
| 304 | + y: the y offset coordinates where the text is drawn. |
| 305 | +
|
| 306 | + These parameters allow the x and y expressions to refer each other, so you can for example specify |
| 307 | + ``y=x/dar``. |
| 308 | +
|
| 309 | + Official documentation: `drawtext <https://ffmpeg.org/ffmpeg-filters.html#drawtext>`__ |
| 310 | + """ |
| 311 | + if text is not None: |
| 312 | + if escape_text: |
| 313 | + text = escape_chars(text, '\\\'%') |
| 314 | + kwargs['text'] = text |
| 315 | + if x != 0: |
| 316 | + kwargs['x'] = x |
| 317 | + if y != 0: |
| 318 | + kwargs['y'] = y |
| 319 | + return filter_(stream, drawtext.__name__, **kwargs) |
| 320 | + |
| 321 | + |
182 | 322 | @filter_operator()
|
183 | 323 | def concat(*streams, **kwargs):
|
184 | 324 | """Concatenate audio and video streams, joining them together one after the other.
|
|
0 commit comments