ffmpeg滤镜学习一,movie+overlay滤镜实现视频加水印、画中画

1.说明

ffmpeg的滤镜上百种之多,其中ffmpeg4.2.1视频滤镜210+,音频滤镜110+,本博客主要记录学习使用ffmpeg api实现视频添加水印,画中画,使用到的滤镜为overlay。

2.代码

本篇代码主要参考了ffmpeg官方的示例,例子当中封装了一个lp_log的函数,用于将日志打印到文件中。

/*
 * 实现对现有视频增加水印,可以是图片、也可以是视频,若为视频,类似画中画
 */
#include "myffmpeg/util.h"
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
    int open_input_file(AVFormatContext *fmt, AVCodecContext **codecctx, AVCodec *codec, const char *filename, int index)
    {
        int ret = 0;
        char msg[500];
        *codecctx = avcodec_alloc_context3(codec);
        ret = avcodec_parameters_to_context(*codecctx, fmt->streams[index]->codecpar);
        if (ret < 0)
        {
            sprintf(msg, "avcodec_parameters_to_context error,ret:%d\n", ret);
            lp_log(msg);
            return -1;
        }

        // open 解码器
        ret = avcodec_open2(*codecctx, codec, NULL);
        if (ret < 0)
        {
            sprintf(msg, "avcodec_open2 error,ret:%d\n", ret);
            lp_log(msg);
            return -2;
        }
        printf("pix:%d\n", (*codecctx)->pix_fmt);
        return ret;
    }

    int init_filter(AVFilterContext **buffersrc_ctx, AVFilterContext **buffersink_ctx, AVFilterGraph **filter_graph, AVStream *stream, AVCodecContext *codecctx, const char *filter_desc)
    {
        int ret = -1;
        char args[512];
        char msg[500];
        const AVFilter *buffersrc = avfilter_get_by_name("buffer");
        const AVFilter *buffersink = avfilter_get_by_name("buffersink");

        AVFilterInOut *input = avfilter_inout_alloc();
        AVFilterInOut *output = avfilter_inout_alloc();

        AVRational time_base = stream->time_base;
        enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE};

        if (!output || !input || !filter_graph)
        {
            ret = -1;
            sprintf(msg, "avfilter_graph_alloc/avfilter_inout_alloc error,ret:%d\n", ret);
            lp_log(msg);
            goto end;
        }
        snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d", codecctx->width, codecctx->height, codecctx->pix_fmt, stream->time_base.num, stream->time_base.den, codecctx->sample_aspect_ratio.num, codecctx->sample_aspect_ratio.den);
        ret = avfilter_graph_create_filter(buffersrc_ctx, buffersrc, "in", args, NULL, *filter_graph);
        if (ret < 0)
        {
            sprintf(msg, "avfilter_graph_create_filter buffersrc error,ret:%d\n", ret);
            lp_log(msg);
            goto end;
        }

        ret = avfilter_graph_create_filter(buffersink_ctx, buffersink, "out", NULL, NULL, *filter_graph);
        if (ret < 0)
        {
            sprintf(msg, "avfilter_graph_create_filter buffersink error,ret:%d\n", ret);
            lp_log(msg);
            goto end;
        }
        ret = av_opt_set_int_list(*buffersink_ctx, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
        if (ret < 0)
        {
            sprintf(msg, "av_opt_set_int_list error,ret:%d\n", ret);
            lp_log(msg);
            goto end;
        }
        /*
     * The buffer source output must be connected to the input pad of
     * the first filter described by filters_descr; since the first
     * filter input label is not specified, it is set to "in" by
     * default.
     */
        output->name = av_strdup("in");
        output->filter_ctx = *buffersrc_ctx;
        output->pad_idx = 0;
        output->next = NULL;

        /*
     * The buffer sink input must be connected to the output pad of
     * the last filter described by filters_descr; since the last
     * filter output label is not specified, it is set to "out" by
     * default.
     */
        input->name = av_strdup("out");
        input->filter_ctx = *buffersink_ctx;
        input->pad_idx = 0;
        input->next = NULL;

        if ((ret = avfilter_graph_parse_ptr(*filter_graph, filter_desc, &input, &output, NULL)) < 0)
        {
            sprintf(msg, "avfilter_graph_parse_ptr error,ret:%d\n", ret);
            lp_log(msg);
            goto end;
        }

        if ((ret = avfilter_graph_config(*filter_graph, NULL)) < 0)
        {
            sprintf(msg, "avfilter_graph_config error,ret:%d\n", ret);
            lp_log(msg);
            goto end;
        }
    end:
        avfilter_inout_free(&input);
        avfilter_inout_free(&output);
        return ret;
    }

    int my_filter(const char *name)
    {
        int ret;
        char msg[500];
        const char *filter_descr = "movie=my_logo.png[wm];[in][wm]overlay=10:10[out]";
        // const char *filter_descr = "scale=640:360,transpose=cclock";
        AVFormatContext *pFormatCtx = NULL;
        AVCodecContext *pCodecCtx;
        AVFilterContext *buffersink_ctx;
        AVFilterContext *buffersrc_ctx;
        AVFilterGraph *filter_graph;
        AVCodec *codec;
        int video_stream_index = -1;

        AVPacket packet;
        AVFrame *pFrame;
        AVFrame *pFrame_out;
        filter_graph = avfilter_graph_alloc();
        FILE *fp_yuv = fopen("test.yuv", "wb+");
        ret = avformat_open_input(&pFormatCtx, name, NULL, NULL);
        if (ret < 0)
        {
            sprintf(msg, "avformat_open_input error,ret:%d\n", ret);
            lp_log(msg);
            return -1;
        }

        ret = avformat_find_stream_info(pFormatCtx, NULL);
        if (ret < 0)
        {
            sprintf(msg, "avformat_find_stream_info error,ret:%d\n", ret);
            lp_log(msg);
            return -2;
        }

        ret = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &codec, 0);
        if (ret < 0)
        {
            sprintf(msg, "av_find_best_stream error,ret:%d\n", ret);
            lp_log(msg);
            return -3;
        }
        // 获取到视频流索引
        video_stream_index = ret;

        av_dump_format(pFormatCtx, 0, name, 0);
        if ((ret = open_input_file(pFormatCtx, &pCodecCtx, codec, name, video_stream_index)) < 0)
        {
            ret = -1;
            sprintf(msg, "open_input_file error,ret:%d\n", ret);
            lp_log(msg);
            goto end;
        }

        if ((ret = init_filter(&buffersrc_ctx, &buffersink_ctx, &filter_graph, pFormatCtx->streams[video_stream_index], pCodecCtx, filter_descr)) < 0)
        {
            ret = -2;
            sprintf(msg, "init_filter error,ret:%d\n", ret);
            lp_log(msg);
            goto end;
        }
        pFrame = av_frame_alloc();
        pFrame_out = av_frame_alloc();
        while (1)
        {
            if ((ret = av_read_frame(pFormatCtx, &packet)) < 0)
                break;

            if (packet.stream_index == video_stream_index)
            {
                ret = avcodec_send_packet(pCodecCtx, &packet);
                if (ret < 0)
                {
                    sprintf(msg, "avcodec_send_packet error,ret:%d\n", ret);
                    lp_log(msg);
                    break;
                }

                while (ret >= 0)
                {
                    ret = avcodec_receive_frame(pCodecCtx, pFrame);
                    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
                    {
                        break;
                    }
                    else if (ret < 0)
                    {
                        sprintf(msg, "avcodec_receive_frame error,ret:%d\n", ret);
                        lp_log(msg);
                        goto end;
                    }

                    pFrame->pts = pFrame->best_effort_timestamp;

                    /* push the decoded frame into the filtergraph */
                    ret = av_buffersrc_add_frame_flags(buffersrc_ctx, pFrame, AV_BUFFERSRC_FLAG_KEEP_REF);
                    if (ret < 0)
                    {
                        sprintf(msg, "av_buffersrc_add_frame_flags error,ret:%d\n", ret);
                        lp_log(msg);
                        break;
                    }

                    /* pull filtered frames from the filtergraph */
                    while (1)
                    {
                        ret = av_buffersink_get_frame(buffersink_ctx, pFrame_out);
                        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
                            break;
                        if (ret < 0)
                            goto end;
                        if (pFrame_out->format == AV_PIX_FMT_YUV420P)
                        {
                            //Y, U, V
                            for (int i = 0; i < pFrame_out->height; i++)
                            {
                                fwrite(pFrame_out->data[0] + pFrame_out->linesize[0] * i, 1, pFrame_out->width, fp_yuv);
                            }
                            for (int i = 0; i < pFrame_out->height / 2; i++)
                            {
                                fwrite(pFrame_out->data[1] + pFrame_out->linesize[1] * i, 1, pFrame_out->width / 2, fp_yuv);
                            }
                            for (int i = 0; i < pFrame_out->height / 2; i++)
                            {
                                fwrite(pFrame_out->data[2] + pFrame_out->linesize[2] * i, 1, pFrame_out->width / 2, fp_yuv);
                            }
                        }
                        av_frame_unref(pFrame_out);
                    }
                    av_frame_unref(pFrame);
                }
            }
            av_packet_unref(&packet);
        }
    end:
        avcodec_free_context(&pCodecCtx);
        fclose(fp_yuv);
    }
}

3.执行

const char *filter_descr = "movie=my_logo.png[wm];[in][wm]overlay=10:10[out]";

这行代码,movie滤镜会读取水印图片,overlay滤镜将图片叠加到原始视频上,位置在(10,10)的位置。

执行代码,会在原始视频上添加水印。

播放生成的yuv数据,使用ffplay进行播放,命令行如下:

./ffplay -f rawvideo -video_size 1280*720 /Users/liupeng/Downloads/test.yuv

如上图,左上角的位置显示了添加的水印。

下面实现画中画:

觉着这篇文章对自己有益的土豪朋友可以扫描屏幕下方二维码金额随意,感谢大家支持,增加写作动力。

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页