Mac OS使用FFmpeg添加视频滤镜

一.概述

一般来说,给视频添加滤镜有两种方式,一种是录制的时候添加滤镜,如基于Open GL的GPUImage。还有一种是编码的时候添加滤镜,如FFmpeg。本文将使用FFmpeg框架中的AVFilter给视频添加滤镜。

本文使用FFmpeg 4.2,Mac OS系统

二.初始化滤镜

1.获取滤镜处理源:获得源及滤镜处理的Sink滤镜,同时申请输入与输出的滤镜结构AVFilterInOut

const AVFilter *buffersrc  = avfilter_get_by_name("buffer");
const AVFilter *buffersink = avfilter_get_by_name("buffersink");
        
AVFilterInOut *outputs = avfilter_inout_alloc();
AVFilterInOut *inputs  = avfilter_inout_alloc();

2.处理AVFilterGraph:存储AVFilter的in和out描述信息。

filter_graph = avfilter_graph_alloc();

3.创建AVFilterContext

       snprintf(args, sizeof(args),
                 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
                 1280,720,AV_PIX_FMT_YUV420P,
                 1, 25,1,1);
        
        ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
                                           args, NULL, filter_graph);
        if (ret < 0) {
            printf("Cannot create buffer source\n");
            return;
        }


        /* 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",
                                           NULL, buffersink_params, filter_graph);
        if (ret < 0) {
            printf("ret == %s\n", av_err2str(ret));
            return;
        }

4.设置其他参数

ret = av_opt_set_int_list(buffersink_ctx, "pix_fmts", pix_fmts,AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
if (ret < 0) {
    av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
    return;
}

5.建立滤镜解析器

    NSString *desc = @"[in]geq=r='X/W*r(X,Y)':g='(1-X/W)*g(X,Y)':b='(H-Y)/H*b(X,Y)',drawtext=fontfile=/Library/Fonts/Arial Unicode.ttf:x=10:y=10:fontcolor=white:fontsize=40:text='版权为爱奇艺官方所有,请勿应用于商业活动。'[text];movie=[path]aqylog.imageset/aqylog.png[wm];[text][wm]overlay=W-w-40:H-h-40 ,scale=1280:720";
const char *filter_descr = [desc cStringUsingEncoding:NSUTF8StringEncoding];
       /* Endpoints for the filter graph. */
        outputs->name       = av_strdup("in");
        outputs->filter_ctx = buffersrc_ctx;
        outputs->pad_idx    = 0;
        outputs->next       = NULL;
        
        inputs->name       = av_strdup("out");
        inputs->filter_ctx = buffersink_ctx;
        inputs->pad_idx    = 0;
        inputs->next       = NULL;
        
        if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_descr,
                                            &inputs, &outputs, NULL)) < 0){
            printf("Error avfilter_graph_parse_ptr \n");
            return;
        }
            
        
        if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0){
            printf("Error avfilter_graph_config \n");
            return;
        }

6.初始化一个用于添加滤镜后输出的AVFrame

        frame_out = av_frame_alloc();
        frame_out->width = pCodecContext->width;
        frame_out->height = pCodecContext->height;
        frame_out->format = pCodecContext->pix_fmt;
        ret = av_frame_get_buffer(frame_out, 0);
        if (ret < 0) {
            printf("ret == %s\n", av_err2str(ret));
        }
        
        avfilter_inout_free(&inputs);
        avfilter_inout_free(&outputs);
        av_free(buffersink_params);

三.编码时添加滤镜

上文已经介绍如何生成原始的AVFrame,添加滤镜后生成新的AVFrame,送入编码器编码。

            int ret;
            if (kUseAVFilter) {
                frame_out->pts = frameCount;
                frame_out->width = 1280;
                frame_out->height = 720;
                frame_out->format = AV_PIX_FMT_YUV420P;
                frame_out->linesize[0] = 1280;
                frame_out->linesize[1] = 640;
                frame_out->linesize[2] = 640;

                ret = av_buffersrc_add_frame(buffersrc_ctx, pFrame);
                if (ret < 0) {
                    printf("av_buffersrc_add_frame == %s\n", av_err2str(ret));
                    return;
                }
                
                ret = av_buffersink_get_frame(buffersink_ctx, frame_out);
                if (ret) {
                    printf("av_buffersink_get_frame == %s\n", av_err2str(ret));
                    return;
                }
                
                ret = avcodec_send_frame(pCodecContext, frame_out);
            }else{
                ret = avcodec_send_frame(pCodecContext, pFrame);
            }

四.滤镜的效果

选择滤镜在于上文的filter_descr设置,由3个滤镜组成:
1.geq滤镜:实现冷暖分割效果

geq=r='X/W*r(X,Y)':g='(1-X/W)*g(X,Y)':b='(H-Y)/H*b(X,Y)'

2.drawtext文字滤镜:在视频上添加文字,需要注意的是添加中文要选择支持中文的.ttf文件,否则会显示方框。

drawtext=fontfile=/Library/Fonts/Arial Unicode.ttf:x=10:y=10:fontcolor=white:fontsize=40:text='版权为爱奇艺官方所有,请勿应用于商业活动。'[text]

3.movie滤镜,添加图片logo,WH是视频的整体宽高,wh是图片的宽高。

movie=[path]aqylog.imageset/aqylog.png[wm];[text][wm]overlay=W-w-40:H-h-40

效果如下:


原图

添加滤镜效果图

FFmpeg视频滤镜目前大概有近200种,常用的有:
淡入淡出的fade滤镜,消除logo的delogo滤镜,增加亮度的brightness滤镜等等,具体详见官方文档。

FFmpeg-Fliters官方文档

你可能感兴趣的:(Mac OS使用FFmpeg添加视频滤镜)