[FFmpeg-devel] [Patch v2] drawtext: ignore last newline

Jonathan Baecker jonbae77 at gmail.com
Tue Jun 11 15:42:55 EEST 2019


This is a new version from my last patch.

As Gyan suggested it is now optional to skip/trim the last line break.

-------------- next part --------------
From 8d31aab97ceaaec4947e1628e2ff1391dd77d4b2 Mon Sep 17 00:00:00 2001
From: Jonathan Baecker <jonbae77 at gmail.com>
Date: Tue, 11 Jun 2019 14:33:50 +0200
Subject: [PATCH] trim last empty line

---
 libavfilter/vf_drawtext.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
index 01cd7fa12..22ec870ed 100644
--- a/libavfilter/vf_drawtext.c
+++ b/libavfilter/vf_drawtext.c
@@ -139,7 +139,7 @@ typedef struct DrawTextContext {
     int exp_mode;                   ///< expansion mode to use for the text
     int reinit;                     ///< tells if the filter is being reinited
 #if CONFIG_LIBFONTCONFIG
-    uint8_t *font;              ///< font to be used
+    uint8_t *font;                  ///< font to be used
 #endif
     uint8_t *fontfile;              ///< font to be used
     uint8_t *text;                  ///< text to be drawn
@@ -162,6 +162,7 @@ typedef struct DrawTextContext {
     unsigned int default_fontsize;  ///< default font size to use

     int line_spacing;               ///< lines spacing in pixels
+    int trim_end;                   ///< skip last empty line - true/false
     short int draw_box;             ///< draw box around text - true or false
     int boxborderw;                 ///< box border width
     int use_kerning;                ///< font kerning is used - true/false
@@ -211,6 +212,7 @@ static const AVOption drawtext_options[]= {
     {"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},
+    {"trim_end",    "trim last empty line", OFFSET(trim_end),           AV_OPT_TYPE_BOOL,   {.i64=0},     0,        1       , FLAGS},
     {"box",         "set box",              OFFSET(draw_box),           AV_OPT_TYPE_BOOL,   {.i64=0},     0,        1       , FLAGS},
     {"boxborderw",  "set box border width", OFFSET(boxborderw),         AV_OPT_TYPE_INT,    {.i64=0},     INT_MIN,  INT_MAX , FLAGS},
     {"line_spacing",  "set line spacing in pixels", OFFSET(line_spacing),   AV_OPT_TYPE_INT,    {.i64=0},     INT_MIN,  INT_MAX,FLAGS},
@@ -1377,6 +1379,18 @@ static int draw_text(AVFilterContext *ctx, AVFrame *frame,

     /* compute and save position for each glyph */
     glyph = NULL;
+
+    if (s->trim_end) {
+        /* skip last newline character */
+        char * last = text + len - 1;
+        char * second_last = text + len - 2;
+
+        if (*second_last == '\r' && *last == '\n')
+            *second_last = 0;
+        else if (*last == '\r' || *last == '\n')
+            *last = 0;
+    }
+
     for (i = 0, p = text; *p; i++) {
         GET_UTF8(code, *p++, continue;);

--
2.21.0



More information about the ffmpeg-devel mailing list