FFMPEG AvFilter使用实例(实现视频缩放,裁剪,水印等)

FFMPEG官网给出了FFMPEG 滤镜使用的实例,它是将视频中的像素点替换成字符,然后从终端输出。我在该实例的基础上稍微的做了修改,使它能够保存滤镜处理过后的文件。在上代码之前先明白几个概念:

Filter:代表单个filter 
FilterPad:代表一个filter的输入或输出端口,每个filter都可以有多个输入和多个输出,只有输出pad的filter称为source,只有输入pad的filter称为sink 
FilterLink:若一个filter的输出pad和另一个filter的输入pad名字相同,即认为两个filter之间建立了link 
FilterChain:代表一串相互连接的filters,除了source和sink外,要求每个filter的输入输出pad都有对应的输出和输入pad 

经典示例:
在这里插入图片描述

图中的一系列操作共使用了四个filter,分别是 
splite:将输入的流进行分裂复制,分两路输出。 
crop:根据给定的参数,对视频进行裁剪 
vflip:根据给定参数,对视频进行翻转等操作 
overlay:将一路输入覆盖到另一路之上,合并输出为一路视频 

下面上代码:

[objc] view plain copy
 
 print?
/*=============================================================================  
#     FileName: filter_video.c  
#         Desc: an example of ffmpeg fileter 
#       Author: licaibiao  
#   LastChange: 2017-03-16   
=============================================================================*/   
#define _XOPEN_SOURCE 600 /* for usleep */  
#include <unistd.h>  
  
#include "avcodec.h"  
#include "avformat.h"  
#include "avfiltergraph.h"  
#include "avcodec.h"  
#include "buffersink.h"  
#include "buffersrc.h"  
#include "opt.h"  
  
#define SAVE_FILE  
  
const charchar *filter_descr = "scale=iw*2:ih*2";  
static AVFormatContext *fmt_ctx;  
static AVCodecContext *dec_ctx;  
AVFilterContext *buffersink_ctx;  
AVFilterContext *buffersrc_ctx;  
AVFilterGraph *filter_graph;  
static int video_stream_index = -1;  
static int64_t last_pts = AV_NOPTS_VALUE;  
  
static int open_input_file(const charchar *filename)  
{  
    int ret;  
    AVCodec *dec;  
  
    if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {  
        av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");  
        return ret;  
    }  
  
    if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {  
        av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");  
        return ret;  
    }  
  
    /* select the video stream  判断流是否正常 */  
    ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);  
    if (ret < 0) {  
        av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");  
        return ret;  
    }  
    video_stream_index = ret;  
    dec_ctx = fmt_ctx->streams[video_stream_index]->codec;  
    av_opt_set_int(dec_ctx, "refcounted_frames", 1, 0); /* refcounted_frames 帧引用计数 */  
  
    /* init the video decoder */  
    if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {  
        av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");  
        return ret;  
    }  
  
    return 0;  
}  
  
static int init_filters(const charchar *filters_descr)  
{  
    char args[512];  
    int ret = 0;  
    AVFilter *buffersrc  = avfilter_get_by_name("buffer");     /* 输入buffer filter */  
    AVFilter *buffersink = avfilter_get_by_name("buffersink"); /* 输出buffer filter */  
    AVFilterInOut *outputs = avfilter_inout_alloc();  
    AVFilterInOut *inputs  = avfilter_inout_alloc();  
    AVRational time_base = fmt_ctx->streams[video_stream_index]->time_base;   /* 时间基数 */  
  
#ifndef SAVE_FILE  
    enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };  
#else  
    enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };  
#endif  
  
    filter_graph = avfilter_graph_alloc();                     /* 创建graph  */  
    if (!outputs || !inputs || !filter_graph) {  
        ret = AVERROR(ENOMEM);  
        goto end;  
    }  
  
    /* 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",  
            dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,  
            time_base.num, time_base.den,  
            dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);  
  
    /* 创建并向FilterGraph中添加一个Filter */  
    ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",  
                                       args, NULL, filter_graph);             
    if (ret < 0) {  
        av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");  
        goto end;  
    }  
  
    /* buffer video sink: to terminate the filter chain. */  
    ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",  
                                       NULL, NULL, filter_graph);            
    if (ret < 0) {  
        av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");  
        goto end;  
    }  
  
     /* Set a binary option to an integer list. */  
    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");  
        goto end;  
    }  
  
    /* 
     * Set the endpoints for the filter graph. The filter_graph will 
     * be linked to the graph described by filters_descr. 
     */  
  
    /* 
     * 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. 
     */  
    outputs->name       = av_strdup("in");  
    outputs->filter_ctx = buffersrc_ctx;  
    outputs->pad_idx    = 0;  
    outputs->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. 
     */  
    inputs->name       = av_strdup("out");  
    inputs->filter_ctx = buffersink_ctx;  
    inputs->pad_idx    = 0;  
    inputs->next       = NULL;  
  
    /* Add a graph described by a string to a graph */  
    if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,  
                                    &inputs, &outputs, NULL)) < 0)      
        goto end;  
  
    /* Check validity and configure all the links and formats in the graph */  
    if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)     
        goto end;  
  
end:  
    avfilter_inout_free(&inputs);  
    avfilter_inout_free(&outputs);  
  
    return ret;  
}  
  
#ifndef SAVE_FILE  
static void display_frame(const AVFrame *frame, AVRational time_base)  
{  
    int x, y;  
    uint8_t *p0, *p;  
    int64_t delay;  
  
    if (frame->pts != AV_NOPTS_VALUE) {  
        if (last_pts != AV_NOPTS_VALUE) {  
            /* sleep roughly the right amount of time; 
             * usleep is in microseconds, just like AV_TIME_BASE. */  
             /* 计算 pts 是用来把时间戳从一个时基调整到另外一个时基时候用的函数 */  
            delay = av_rescale_q(frame->pts - last_pts,  
                                 time_base, AV_TIME_BASE_Q);  
            if (delay > 0 && delay < 1000000)  
                usleep(delay);  
        }  
        last_pts = frame->pts;  
    }  
  
    /* Trivial ASCII grayscale display. */  
    p0 = frame->data[0];  
    puts("\033c");  
    for (y = 0; y < frame->height; y++) {  
        p = p0;  
        for (x = 0; x < frame->width; x++)  
            putchar(" .-+#"[*(p++) / 52]);  
        putchar('\n');  
        p0 += frame->linesize[0];  
    }  
    fflush(stdout);  
}  
#else  
FILEFILE * file_fd;  
static void write_frame(const AVFrame *frame)  
{  
    static int printf_flag = 0;  
    if(!printf_flag){  
        printf_flag = 1;  
        printf("frame widht=%d,frame height=%d\n",frame->width,frame->height);  
          
        if(frame->format==AV_PIX_FMT_YUV420P){  
            printf("format is yuv420p\n");  
        }  
        else{  
            printf("formet is = %d \n",frame->format);  
        }  
      
    }  
  
    fwrite(frame->data[0],1,frame->width*frame->height,file_fd);  
    fwrite(frame->data[1],1,frame->width/2*frame->height/2,file_fd);  
    fwrite(frame->data[2],1,frame->width/2*frame->height/2,file_fd);  
}  
  
#endif  
  
int main(int argc, charchar **argv)  
{  
    int ret;  
    AVPacket packet;  
    AVFrame *frame = av_frame_alloc();  
    AVFrame *filt_frame = av_frame_alloc();  
    int got_frame;  
  
#ifdef SAVE_FILE  
    file_fd = fopen("test.yuv","wb+");  
#endif  
  
    if (!frame || !filt_frame) {  
        perror("Could not allocate frame");  
        exit(1);  
    }  
    if (argc != 2) {  
        fprintf(stderr, "Usage: %s file\n", argv[0]);  
        exit(1);  
    }  
  
    av_register_all();  
    avfilter_register_all();  
  
    if ((ret = open_input_file(argv[1])) < 0)  
        goto end;  
    if ((ret = init_filters(filter_descr)) < 0)  
        goto end;  
  
    /* read all packets */  
    while (1) {  
        if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)  
            break;  
  
        if (packet.stream_index == video_stream_index) {  
            got_frame = 0;  
            ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &packet);  
            if (ret < 0) {  
                av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");  
                break;  
            }  
  
            if (got_frame) {  
                frame->pts = av_frame_get_best_effort_timestamp(frame);    /* pts: Presentation Time Stamp */  
  
                /* push the decoded frame into the filtergraph */  
                if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0) {  
                    av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");  
                    break;  
                }  
  
                /* pull filtered frames from the filtergraph */  
                while (1) {  
                    ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);  
                    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)  
                        break;  
                    if (ret < 0)  
                        goto end;  
#ifndef SAVE_FILE  
                    display_frame(filt_frame, buffersink_ctx->inputs[0]->time_base);  
#else  
                    write_frame(filt_frame);  
#endif  
                    av_frame_unref(filt_frame);  
                }  
                /* Unreference all the buffers referenced by frame and reset the frame fields. */  
                av_frame_unref(frame);  
            }  
        }  
        av_packet_unref(&packet);  
    }  
end:  
    avfilter_graph_free(&filter_graph);  
    avcodec_close(dec_ctx);  
    avformat_close_input(&fmt_ctx);  
    av_frame_free(&frame);  
    av_frame_free(&filt_frame);  
  
    if (ret < 0 && ret != AVERROR_EOF) {  
        fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));  
        exit(1);  
    }  
#ifdef SAVE_FILE  
    fclose(file_fd);  
#endif  
    exit(0);  
}  

该工程中,我的Makefile文件如下:

[objc] view plain copy
 
 print?
OUT_APP      = test  
INCLUDE_PATH = /usr/local/include/  
INCLUDE = -I$(INCLUDE_PATH)libavutil/ -I$(INCLUDE_PATH)libavdevice/ \  
            -I$(INCLUDE_PATH)libavcodec/ -I$(INCLUDE_PATH)libswresample \  
            -I$(INCLUDE_PATH)libavfilter/ -I$(INCLUDE_PATH)libavformat \  
            -I$(INCLUDE_PATH)libswscale/  
  
FFMPEG_LIBS = -lavformat -lavutil -lavdevice -lavcodec -lswresample -lavfilter -lswscale  
SDL_LIBS    =   
LIBS        = $(FFMPEG_LIBS)$(SDL_LIBS)  
  
COMPILE_OPTS = $(INCLUDE)  
C            = c  
OBJ          = o  
C_COMPILER   = cc  
C_FLAGS      = $(COMPILE_OPTS) $(CPPFLAGS) $(CFLAGS)  
  
LINK         = cc -o   
LINK_OPTS    = -lz -lm  -lpthread  
LINK_OBJ     = test.o   
  
.$(C).$(OBJ):  
    $(C_COMPILER) -c $(C_FLAGS) $<  
  
  
$(OUT_APP): $(LINK_OBJ)  
    $(LINK)$@  $(LINK_OBJ)  $(LIBS) $(LINK_OPTS)  
  
clean:  
        -rm -rf *.$(OBJ) $(OUT_APP) core *.core *~ *yuv  

运行结果如下:

[objc] view plain copy

print?
licaibiao@ubuntu:~/test/FFMPEG/filter$ ls
Makefile school.flv test test.c test.o
licaibiao@ubuntu:~/test/FFMPEG/filter$ ./test school.flv
[flv @ 0x12c16c0] video stream discovered after head already parsed
[flv @ 0x12c16c0] audio stream discovered after head already parsed
frame widht=1024,frame height=576
format is yuv420p
licaibiao@ubuntu:~/test/FFMPEG/filter$ ls
Makefile school.flv test test.c test.o test.yuv

在这里,我打印出来了输出视频的格式和图片的长和宽,该实例生成的是一个YUV420 格式的视频,使用YUV播放器播放视频的时候,需要设置正确的视频长度和宽度。在代码中通过设置enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE };来设置输出格式。

过滤器的参数设置是通过const char *filter_descr = "scale=iw*2:ih*2"; 来设置。它表示将视频的长和框都拉伸到原来的两倍。具体的filter参数可以通过命令:ffmpeg -filters 来查询。结果如下:

[objc] view plain copy

print?
Filters:
T… = Timeline support
.S. = Slice threading
…C = Command support
A = Audio input/output
V = Video input/output
N = Dynamic number and/or type of input/output
| = Source or sink filter
… abench A->A Benchmark part of a filtergraph.
… acompressor A->A Audio compressor.
… acrossfade AA->A Cross fade two input audio streams.
… acrusher A->A Reduce audio bit resolution.

  在上面的代码中,我们设置的是将图片拉升到原来图像的两倍,其显示效果如下,可能是截图的问题,这里看好像没有拉伸到两倍。
在上面的代码中,我们设置的是:

    const char *filter_descr = "scale=iw*2:ih*2";   iw 表示输入视频的宽,ih表示输入视频的高。可以任意比例的缩放视频。这里*2 表示放大两倍,如果是/2表示缩小两倍。

视频缩放还可以直接设置:

     const char *filter_descr = "scale=320:240"; 设置视频输出宽为320,高位240,当然也是可以随意的设置其他的参数。

 

视频的裁剪可以设置为:

    const char *filter_descr = "crop=320:240:0:0";   具体含义是 crop=width:height:x:y,其中 width 和 height 表示裁剪后的尺寸,x:y 表示裁剪区域的左上角坐标。

  

视频添加一个网格水印可以设置为:

    const char *filter_descr = "drawgrid=width=100:height=100:thickness=2:color=red@0.5";    具体含义是 width 和 height 表示添加网格的宽和高,thickness表示网格的线宽,color表示颜色 
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: FFmpeg是一个开源的跨平台音视频处理工具,提供了丰富的API接口,可以用C语言实现视频水印。下面是一个简单的示例,演示了如何使用FFmpeg视频上添加静态图片水印。 1. 安装FFmpeg 首先需要在本地安装FFmpeg,可以参考FFmpeg官网的安装指南。 2. 打开视频文件 使用FFmpeg打开视频文件,代码如下: ``` AVFormatContext *format_ctx = NULL; if (avformat_open_input(&format_ctx, input_file_name, NULL, NULL) < 0) { // 打开视频文件失败 return -1; } if (avformat_find_stream_info(format_ctx, NULL) < 0) { // 查找流信息失败 return -1; } int video_stream_index = -1; for (int i = 0; i < format_ctx->nb_streams; i++) { if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; break; } } if (video_stream_index == -1) { // 没有找到视频流 return -1; } AVCodecParameters *codec_params = format_ctx->streams[video_stream_index]->codecpar; AVCodec *codec = avcodec_find_decoder(codec_params->codec_id); if (!codec) { // 找不到视频解码器 return -1; } AVCodecContext *codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { // 分配解码器上下文失败 return -1; } if (avcodec_parameters_to_context(codec_ctx, codec_params) < 0) { // 复制解码器参数失败 return -1; } if (avcodec_open2(codec_ctx, codec, NULL) < 0) { // 打开解码器失败 return -1; } AVFrame *frame = av_frame_alloc(); if (!frame) { // 分配帧内存失败 return -1; } ``` 3. 打开水印图片 使用FFmpeg打开水印图片,代码如下: ``` AVFormatContext *watermark_format_ctx = NULL; if (avformat_open_input(&watermark_format_ctx, watermark_file_name, NULL, NULL) < 0) { // 打开水印图片失败 return -1; } if (avformat_find_stream_info(watermark_format_ctx, NULL) < 0) { // 查找流信息失败 return -1; } int watermark_stream_index = -1; for (int i = 0; i < watermark_format_ctx->nb_streams; i++) { if (watermark_format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { watermark_stream_index = i; break; } } if (watermark_stream_index == -1) { // 没有找到视频流 return -1; } AVCodecParameters *watermark_codec_params = watermark_format_ctx->streams[watermark_stream_index]->codecpar; AVCodec *watermark_codec = avcodec_find_decoder(watermark_codec_params->codec_id); if (!watermark_codec) { // 找不到视频解码器 return -1; } AVCodecContext *water ### 回答2: FFmpeg是一个开源的跨平台的音视频处理库,它提供了多种功能,包括视频水印实现。 在FFmpeg中,可以使用AVFilterGraph库来实现视频水印的添加。具体的实现步骤如下: 1. 引入FFmpeg库并初始化AVFilterGraph对象。 2. 创建输入(Input)和输出(Output)的AVFilter对象。 3. 配置输入(Input)和输出(Output)的AVFilterContext对象。 4. 创建文本AVFilterAVFilter对象,并进行文本水印设置。 5. 创建滤镜链(Filter Chain),将输入(Input)和输出(Output)的AVFilterContext对象与文本AVFilter对象连接在一起。 6. 将滤镜链(Filter Chain)添加到AVFilterGraph对象中。 7. 打开输入(Input)和输出(Output)的AVFilterContext对象。 8. 循环读取每一帧的视频数据,通过滤镜链(Filter Chain)处理视频数据,将水印应用到视频中。 9. 将处理后的视频数据写入输出(Output)的AVFilterContext对象。 10. 关闭输入(Input)和输出(Output)的AVFilterContext对象,并进行内存释放。 通过以上步骤,可以在使用FFmpeg的C语言编程中实现视频水印的添加。需要注意的是,具体的实现代码会根据所使用FFmpeg版本和编译环境的不同而有所变化,可以参考FFmpeg官方文档和相关的示例代码进行开发。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安卓兼职framework应用工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值