利用FFmpeg API进行字符叠加和加水印

前面两篇文章详细讲解了怎么叠加字幕和Logo,但是这两篇的例子主要是针对Windows平台的,用到大量Windows API,一些非Windows程序员想要移植到其他平台(如Linux、Android)可能还要费一番功夫。要在其他平台进行叠加字幕和Logo有什么比较通用的方案呢?其实FFmpeg已经集成了一个加水印滤镜功能,用跨平台的FFmpeg能够帮助我们轻松实现该功能。

废话少说,先看看加水印滤镜怎么用。

首先要调用avfilter_register_all() 注册所有AVFilter。

接着,定义几个跟加水印滤镜相关的变量:

AVFilterContext * buffersink_ctx = NULL;
AVFilterContext * buffersrc_ctx = NULL;
AVFilterGraph * filter_graph = NULL;
BOOL      g_bInitFilterOK = FALSE;
	
CCritSec     g_FilterLock;

FFmpeg初始化加水印滤镜的例子代码如下:


static int init_filters(const char *filters_descr, AVCodecContext *pCodecCtx)
{
	CAutoLock lock(&g_FilterLock);

	if(filter_graph != NULL)
		return 1;

	if(pCodecCtx->pix_fmt != PIX_FMT_YUV420P) //检查输入图像像素格式
		return 2;

    char args[512];
    int ret;
    AVFilter *buffersrc  = avfilter_get_by_name("buffer");
    AVFilter *buffersink = avfilter_get_by_name("ffbuffersink");
    AVFilterInOut *outputs = avfilter_inout_alloc();
    AVFilterInOut *inputs  = avfilter_inout_alloc();
    enum PixelFormat pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
    AVBufferSinkParams *buffersink_params;

    filter_graph = avfilter_graph_alloc();

    /* buffer video source: the decoded frames from the decoder will be inserted here. */
    _snprintf(args, sizeof(args),
            "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
            pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
            pCodecCtx->time_base.num, pCodecCtx->time_base.den,
            pCodecCtx->sample_aspect_ratio.num, pCodecCtx->sample_aspect_ratio.den);

    ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
                                       args, NULL, filter_graph);
    if (ret < 0) {
        TRACE("Cannot create buffer source\n");
        return ret;
    }

    /* buffer video sink: to terminate the filter chain. */
    buffersink_params = av_buffersink_params_alloc();
    buffersink_params->pixel_fmts = pix_fmts;
    ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
     
  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值