天天看點

FFMPEG 最簡濾鏡filter使用執行個體(實作視訊縮放,裁剪,水印等)

 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 

經典示例:

FFMPEG 最簡濾鏡filter使用執行個體(實作視訊縮放,裁剪,水印等)

    圖中的一系列操作共使用了四個filter,分别是 

    splite:将輸入的流進行分裂複制,分兩路輸出。 

    crop:根據給定的參數,對視訊進行裁剪 

    vflip:根據給定參數,對視訊進行翻轉等操作 

    overlay:将一路輸入覆寫到另一路之上,合并輸出為一路視訊 

下面上代碼:

/*=============================================================================  

#     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 "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");  

    /* 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");  

    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 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);             

        av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");  

    /* buffer video sink: to terminate the filter chain. */  

    ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",  

                                       NULL, NULL, filter_graph);            

        av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");  

     /* 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);     

        av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");  

    /* 

     * 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 

    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)      

    /* Check validity and configure all the links and formats in the graph */  

    if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)     

end:  

    avfilter_inout_free(&inputs);  

    avfilter_inout_free(&outputs);  

    return ret;  

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);  

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);  

int main(int argc, charchar **argv)  

    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+");  

    if (!frame || !filt_frame) {  

        perror("Could not allocate frame");  

        exit(1);  

    if (argc != 2) {  

        fprintf(stderr, "Usage: %s file\n", argv[0]);  

    av_register_all();  

    avfilter_register_all();  

    if ((ret = open_input_file(argv[1])) < 0)  

    if ((ret = init_filters(filter_descr)) < 0)  

    /* 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;  

                    display_frame(filt_frame, buffersink_ctx->inputs[0]->time_base);  

                    write_frame(filt_frame);  

                    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);  

    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));  

    fclose(file_fd);  

    exit(0);  

該工程中,我的Makefile檔案如下:

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  

運作結果如下:

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  

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 來查詢。結果如下:

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.  

.............................................................................  

    在上面的代碼中,我們設定的是将圖檔拉升到原來圖像的兩倍,其顯示效果如下,可能是截圖的問題,這裡看好像沒有拉伸到兩倍。

FFMPEG 最簡濾鏡filter使用執行個體(實作視訊縮放,裁剪,水印等)

原圖

FFMPEG 最簡濾鏡filter使用執行個體(實作視訊縮放,裁剪,水印等)

拉伸後

在上面的代碼中,我們設定的是:

    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:[email protected]";    具體含義是 width 和 height 表示添加網格的寬和高,thickness表示網格的線寬,color表示顔色 。其效果如下:

FFMPEG 最簡濾鏡filter使用執行個體(實作視訊縮放,裁剪,水印等)

繼續閱讀