⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawtext.c

📁 ffmpeg的完整源代码和作者自己写的文档。不但有在Linux的工程哦
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * drawtext.c: print text over the screen
 ******************************************************************************
 * Options:
 * -f <filename>    font filename (MANDATORY!!!)
 * -s <pixel_size>  font size in pixels [default 16]
 * -b               print background
 * -o               outline glyphs (use the bg color)
 * -x <pos>         x position ( >= 0) [default 0]
 * -y <pos>         y position ( >= 0) [default 0]
 * -t <text>        text to print (will be passed to strftime())
 *                  MANDATORY: will be used even when -T is used.
 *                  in this case, -t will be used if some error
 *                  occurs
 * -T <filename>    file with the text (re-read every frame)
 * -c <#RRGGBB>     foreground color ('internet' way) [default #ffffff]
 * -C <#RRGGBB>     background color ('internet' way) [default #000000]
 *
 ******************************************************************************
 * Features:
 * - True Type, Type1 and others via FreeType2 library
 * - Font kerning (better output)
 * - Line Wrap (if the text doesn't fit, the next char go to the next line)
 * - Background box
 * - Outline
 ******************************************************************************
 * Author: Gustavo Sverzut Barbieri <gsbarbieri@yahoo.com.br>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#define MAXSIZE_TEXT 1024

#include "framehook.h"

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#undef time
#include <sys/time.h>
#include <time.h>

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H

#define SCALEBITS 10
#define ONE_HALF  (1 << (SCALEBITS - 1))
#define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))

#define RGB_TO_YUV(rgb_color, yuv_color) do { \
  yuv_color[0] = (FIX(0.29900)    * rgb_color[0] + FIX(0.58700) * rgb_color[1] + FIX(0.11400) * rgb_color[2] + ONE_HALF) >> SCALEBITS; \
  yuv_color[2] = ((FIX(0.50000)   * rgb_color[0] - FIX(0.41869) * rgb_color[1] - FIX(0.08131) * rgb_color[2] + ONE_HALF - 1) >> SCALEBITS) + 128; \
  yuv_color[1] = ((- FIX(0.16874) * rgb_color[0] - FIX(0.33126) * rgb_color[1] + FIX(0.50000) * rgb_color[2] + ONE_HALF - 1) >> SCALEBITS) + 128; \
} while (0)

#define COPY_3(dst,src) { \
    dst[0]=src[0]; \
    dst[1]=src[1]; \
    dst[2]=src[2]; \
}



#define SET_PIXEL(picture, yuv_color, x, y) { \
    picture->data[0][ (x) + (y)*picture->linesize[0] ] = yuv_color[0]; \
    picture->data[1][ ((x/2) + (y/2)*picture->linesize[1]) ] = yuv_color[1]; \
    picture->data[2][ ((x/2) + (y/2)*picture->linesize[2]) ] = yuv_color[2]; \
}

#define GET_PIXEL(picture, yuv_color, x, y) { \
    yuv_color[0] = picture->data[0][ (x) + (y)*picture->linesize[0] ]; \
    yuv_color[1] = picture->data[1][ (x/2) + (y/2)*picture->linesize[1] ]; \
    yuv_color[2] = picture->data[2][ (x/2) + (y/2)*picture->linesize[2] ]; \
}


typedef struct {
  unsigned char *text;
  char *file;
  unsigned int x;
  unsigned int y;
  int bg;
  int outline;
  unsigned char bgcolor[3]; /* YUV */
  unsigned char fgcolor[3]; /* YUV */
  FT_Library library;
  FT_Face    face;
  FT_Glyph   glyphs[ 255 ];
  FT_Bitmap  bitmaps[ 255 ];
  int        advance[ 255 ];
  int        bitmap_left[ 255 ];
  int        bitmap_top[ 255 ];
  unsigned int glyphs_index[ 255 ];
  int        text_height;
  int        baseline;
  int use_kerning;
} ContextInfo;


void Release(void *ctx)
{
    if (ctx)
        av_free(ctx);
}


static int ParseColor(char *text, unsigned char yuv_color[3])
{
  char tmp[3];
  unsigned char rgb_color[3];
  int i;

  tmp[2] = '\0';

  if ((!text) || (strlen(text) != 7) || (text[0] != '#') )
    return -1;

  for (i=0; i < 3; i++)
    {
      tmp[0] = text[i*2+1];
      tmp[1] = text[i*2+2];

      rgb_color[i] = strtol(tmp, NULL, 16);
    }

  RGB_TO_YUV(rgb_color, yuv_color);

  return 0;
}

int Configure(void **ctxp, int argc, char *argv[])
{
    int c;
    int error;
    ContextInfo *ci=NULL;
    char *font=NULL;
    unsigned int size=16;
    FT_BBox bbox;
    int yMax, yMin;
    *ctxp = av_mallocz(sizeof(ContextInfo));
    ci = (ContextInfo *) *ctxp;

    /* configure Context Info */
    ci->text = NULL;
    ci->file = NULL;
    ci->x = ci->y = 0;
    ci->fgcolor[0]=255;
    ci->fgcolor[1]=128;
    ci->fgcolor[2]=128;
    ci->bgcolor[0]=0;
    ci->fgcolor[1]=128;
    ci->fgcolor[2]=128;
    ci->bg = 0;
    ci->outline = 0;
    ci->text_height = 0;

    optind = 0;
    while ((c = getopt(argc, argv, "f:t:T:x:y:s:c:C:bo")) > 0) {
      switch (c) {
      case 'f':
        font = optarg;
        break;
      case 't':
        ci->text = av_strdup(optarg);
        break;
      case 'T':
        ci->file = av_strdup(optarg);
        break;
      case 'x':
        ci->x = (unsigned int) atoi(optarg);
        break;
      case 'y':
        ci->y = (unsigned int) atoi(optarg);
        break;
      case 's':
        size = (unsigned int) atoi(optarg);
        break;
      case 'c':
        if (ParseColor(optarg, ci->fgcolor) == -1)
          {
            av_log(NULL, AV_LOG_ERROR, "Invalid foreground color: '%s'. You must specify the color in the internet way(packaged hex): #RRGGBB, ie: -c #ffffff (for white foreground)\n", optarg);
            return -1;
          }
        break;
      case 'C':
        if (ParseColor(optarg, ci->bgcolor) == -1)
          {
            av_log(NULL, AV_LOG_ERROR, "Invalid foreground color: '%s'. You must specify the color in the internet way(packaged hex): #RRGGBB, ie: -c #ffffff (for white foreground)\n", optarg);
            return -1;
          }
        break;
      case 'b':
        ci->bg=1;
        break;
      case 'o':
        ci->outline=1;
        break;
      case '?':
        av_log(NULL, AV_LOG_ERROR, "Unrecognized argument '%s'\n", argv[optind]);
        return -1;
      }
    }

    if (!ci->text)
      {
        av_log(NULL, AV_LOG_ERROR, "No text provided (-t text)\n");
        return -1;
      }

    if (ci->file)
      {
        FILE *fp;
        if ((fp=fopen(ci->file, "r")) == NULL)
          {
            av_log(NULL, AV_LOG_INFO, "WARNING: The file could not be opened. Using text provided with -t switch: %s", strerror(errno));
          }
        else
          {
            fclose(fp);
          }
      }

    if (!font)
      {
        av_log(NULL, AV_LOG_ERROR, "No font file provided! (-f filename)\n");
        return -1;
      }

    if ((error = FT_Init_FreeType(&(ci->library))) != 0)
      {
        av_log(NULL, AV_LOG_ERROR, "Could not load FreeType (error# %d).\n", error);
        return -1;
      }

    if ((error = FT_New_Face( ci->library, font, 0, &(ci->face) )) != 0)
      {
        av_log(NULL, AV_LOG_ERROR, "Could not load face: %s  (error# %d).\n", font, error);
        return -1;
      }

    if ((error = FT_Set_Pixel_Sizes( ci->face, 0, size)) != 0)
      {
        av_log(NULL, AV_LOG_ERROR, "Could not set font size to %d pixels (error# %d).\n", size, error);
        return -1;
      }

    ci->use_kerning = FT_HAS_KERNING(ci->face);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -