aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Utkin <andrey.krieger.utkin@gmail.com>2014-07-04 21:54:53 +0300
committerStefano Sabatini <stefasab@gmail.com>2014-07-17 12:42:56 +0200
commite9ff5df0b53076636082412f06bdd3b6aaa2b607 (patch)
tree752ae7232a63f3e5c980c08df332a6b569f3ef41
parentdec87454deb2f2a314f309cd6c1fa636503a70ae (diff)
downloadffmpeg-e9ff5df0b53076636082412f06bdd3b6aaa2b607.tar.gz
lavfi/drawtext: add fontcolor_expr option
Allow to dynamically evaluate the font color. Signed-off-by: Stefano Sabatini <stefasab@gmail.com>
-rw-r--r--doc/filters.texi16
-rw-r--r--libavfilter/version.h2
-rw-r--r--libavfilter/vf_drawtext.c26
3 files changed, 38 insertions, 6 deletions
diff --git a/doc/filters.texi b/doc/filters.texi
index 6bea105d36..54d6549454 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -3698,6 +3698,11 @@ the "Color" section in the ffmpeg-utils manual.
The default value of @var{fontcolor} is "black".
+@item fontcolor_expr
+String which is expanded the same way as @var{text} to obtain dynamic
+@var{fontcolor} value. By default this option has empty value and is not
+processed. When this option is set, it overrides @var{fontcolor} option.
+
@item font
The font family to be used for drawing text. By default Sans.
@@ -4019,6 +4024,17 @@ Print the date of a real-time encoding (see strftime(3)):
drawtext='fontfile=FreeSans.ttf:text=%@{localtime:%a %b %d %Y@}'
@end example
+@item
+Shwo text fading in and out (appearing/disappearing):
+@example
+#!/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\\\\: max(0\\, min(255\\, 255*(1*between(t\\, $DS + $FID\\, $DE - $FOD) + ((t - $DS)/$FID)*between(t\\, $DS\\, $DS + $FID) + (-(t - $DE)/$FOD)*between(t\\, $DE - $FOD\\, $DE) ) )) \\\\: x\\\\: 2 @}"
+@end example
+
@end itemize
For more information about libfreetype, check:
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 8f47a9683e..9f2c36494c 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -31,7 +31,7 @@
#define LIBAVFILTER_VERSION_MAJOR 4
#define LIBAVFILTER_VERSION_MINOR 11
-#define LIBAVFILTER_VERSION_MICRO 101
+#define LIBAVFILTER_VERSION_MICRO 102
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
LIBAVFILTER_VERSION_MINOR, \
diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
index 3336eb2dc7..c744d93562 100644
--- a/libavfilter/vf_drawtext.c
+++ b/libavfilter/vf_drawtext.c
@@ -143,6 +143,8 @@ typedef struct DrawTextContext {
uint8_t *fontfile; ///< font to be used
uint8_t *text; ///< text to be drawn
AVBPrint expanded_text; ///< used to contain the expanded text
+ uint8_t *fontcolor_expr; ///< fontcolor expression to evaluate
+ AVBPrint expanded_fontcolor; ///< used to contain the expanded fontcolor spec
int ft_load_flags; ///< flags used for loading fonts, see FT_LOAD_*
FT_Vector *positions; ///< positions for each element in the text
size_t nb_positions; ///< number of elements of positions array
@@ -201,6 +203,7 @@ static const AVOption drawtext_options[]= {
{"text", "set text", OFFSET(text), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
{"textfile", "set text file", OFFSET(textfile), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
{"fontcolor", "set foreground color", OFFSET(fontcolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
+ {"fontcolor_expr", "set foreground color expression", OFFSET(fontcolor_expr), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS},
{"boxcolor", "set box color", OFFSET(boxcolor.rgba), AV_OPT_TYPE_COLOR, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS},
{"bordercolor", "set border color", OFFSET(bordercolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
{"shadowcolor", "set shadow color", OFFSET(shadowcolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
@@ -683,6 +686,7 @@ static av_cold int init(AVFilterContext *ctx)
av_log(ctx, AV_LOG_WARNING, "expansion=strftime is deprecated.\n");
av_bprint_init(&s->expanded_text, 0, AV_BPRINT_SIZE_UNLIMITED);
+ av_bprint_init(&s->expanded_fontcolor, 0, AV_BPRINT_SIZE_UNLIMITED);
return 0;
}
@@ -726,6 +730,7 @@ static av_cold void uninit(AVFilterContext *ctx)
FT_Done_FreeType(s->library);
av_bprint_finalize(&s->expanded_text, NULL);
+ av_bprint_finalize(&s->expanded_fontcolor, NULL);
}
static int config_input(AVFilterLink *inlink)
@@ -1054,11 +1059,8 @@ end:
return ret;
}
-static int expand_text(AVFilterContext *ctx)
+static int expand_text(AVFilterContext *ctx, char *text, AVBPrint *bp)
{
- DrawTextContext *s = ctx->priv;
- char *text = s->text;
- AVBPrint *bp = &s->expanded_text;
int ret;
av_bprint_clear(bp);
@@ -1154,7 +1156,7 @@ static int draw_text(AVFilterContext *ctx, AVFrame *frame,
av_bprintf(bp, "%s", s->text);
break;
case EXP_NORMAL:
- if ((ret = expand_text(ctx)) < 0)
+ if ((ret = expand_text(ctx, s->text, &s->expanded_text)) < 0)
return ret;
break;
case EXP_STRFTIME:
@@ -1180,6 +1182,20 @@ static int draw_text(AVFilterContext *ctx, AVFrame *frame,
s->nb_positions = len;
}
+ if (s->fontcolor_expr[0]) {
+ /* If expression is set, evaluate and replace the static value */
+ av_bprint_clear(&s->expanded_fontcolor);
+ if ((ret = expand_text(ctx, s->fontcolor_expr, &s->expanded_fontcolor)) < 0)
+ return ret;
+ if (!av_bprint_is_complete(&s->expanded_fontcolor))
+ return AVERROR(ENOMEM);
+ av_log(s, AV_LOG_DEBUG, "Evaluated fontcolor is '%s'\n", s->expanded_fontcolor.str);
+ ret = av_parse_color(s->fontcolor.rgba, s->expanded_fontcolor.str, -1, s);
+ if (ret)
+ return ret;
+ ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
+ }
+
x = 0;
y = 0;