热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

FFmpegFilter简单使用

本文主要分享【】,技术文章【FFmpegFilter简单使用】为【音视频开发老舅】投稿,如果你遇到音视频开发进阶相关问题,本文相关知识或能到你。Filter,可以认为是一些预定义的范式,可以实现

本文主要分享【】,技术文章【FFmpeg Filter简单使用】为【音视频开发老舅】投稿,如果你遇到音视频开发进阶相关问题,本文相关知识或能到你。

Filter,可以认为是一些预定义的范式,可以实现类似积木的多种功能的自由组合。每个filter都有固定数目的输入和输出,而且实际使用中不允许有空 悬的输入输出端。使用文本描述时我们可以通过标识符指定输入和输出端口,将不同filter串联起来,构成更复杂的filter。这就形成了嵌套的 filter。当然每个filter可以通过ffmpeg/ffplay命令行实现,但通常filter更方便。

Filter能做什么?

比较常见的有:

音视频的倍速播放

视频添加删除Logo

视频画中画

放大缩小

画面裁剪

源码

#include 
  
    #include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           static AVFormatContext *fmt_ctx; static AVCodecContext *dec_ctx; AVFilterContext *buffersink_ctx; AVFilterContext *buffersrc_ctx; AVFilterGraph *filter_graph; static int video_stream_index = -1; //打开输入文件 static int open_input_file(const char *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; } //选择视频流 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; /* create decoding context */ dec_ctx = avcodec_alloc_context3(dec); if (!dec_ctx) return AVERROR(ENOMEM); avcodec_parameters_to_context(dec_ctx, fmt_ctx->streams[video_stream_index]->codecpar); //初始化解码器 if ((ret = avcodec_open2(dec_ctx, dec, NULL)) <0) { av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n"); return ret; } return 0; } //初始化filter static int init_filters(const char *filters_descr) { char args[512]; int ret = 0; //滤镜输入缓冲区,解码器解码后的数据都会放到buffer中,是一个特殊的filter const AVFilter *buffersrc = avfilter_get_by_name("buffer"); //滤镜输出缓冲区,滤镜处理完后输出的数据都会放在buffersink中,是一个特殊的filter const AVFilter *buffersink = avfilter_get_by_name("buffersink"); AVFilterInOut *outputs = avfilter_inout_alloc(); AVFilterInOut *inputs = avfilter_inout_alloc(); AVRational time_base = fmt_ctx->streams[video_stream_index]->time_base; enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE}; //创建filter图,会包含本次使用到的所有过滤器 filter_graph = avfilter_graph_alloc(); 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); //创建过滤器实例并将其添加到现有graph中 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; } /* 缓冲视频接收器: 终止过滤器链 */ 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; } 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; //将由字符串描述的图形添加到图形中 if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr, &inputs, &outputs, NULL)) <0) goto end; if ((ret = avfilter_graph_config(filter_graph, NULL)) <0) goto end; end: avfilter_inout_free(&inputs); avfilter_inout_free(&outputs); return ret; } //保存YUV数据 static int save_frame(AVFrame *filt_frame, FILE *out){ av_log(NULL, AV_LOG_DEBUG, "do_frame %d\n",filt_frame->format); if(filt_frame->format==AV_PIX_FMT_YUV420P){ av_log(NULL, AV_LOG_ERROR, "save 1 frame\n"); for(int i=0;i
          
           height;i++){ fwrite(filt_frame->data[0]+filt_frame->linesize[0]*i,1,filt_frame->width,out); } for(int i=0;i
           
            height/2;i++){ fwrite(filt_frame->data[1]+filt_frame->linesize[1]*i,1,filt_frame->width/2,out); } for(int i=0;i
            
             height/2;i++){ fwrite(filt_frame->data[2]+filt_frame->linesize[2]*i,1,filt_frame->width/2,out); } } fflush(out); return 0; } int main(int argc, char **argv) { int ret; AVPacket packet; AVFrame *frame; AVFrame *filt_frame; FILE *out = NULL; const char *filter_desc="movie=my_logo.png[wm];[in][wm]overlay=5:5[out]"; //左上角绘制一个logo图片 //"drawbox=30:10:64:64:red";//x= 30,y=10,/Users/liuwei/Desktop/test.mp4"; const char* outfile = "/Users/liuwei/Desktop/new_test.yuv"; av_log_set_level(AV_LOG_DEBUG); frame = av_frame_alloc(); filt_frame = av_frame_alloc(); if (!frame || !filt_frame) { perror("Could not allocate frame"); exit(1); } out = fopen(outfile, "wb"); if(!out){ av_log(NULL, AV_LOG_ERROR, "Failed to open yuv file!\n"); exit(-1); } if ((ret = open_input_file(filename)) <0) goto end; if ((ret = init_filters(filter_desc)) <0) goto end; /* read all packets */ while (1) { // 1 if ((ret = av_read_frame(fmt_ctx, &packet)) <0) break; if (packet.stream_index == video_stream_index) { //2 ret = avcodec_send_packet(dec_ctx, &packet); if (ret <0) { av_log(NULL, AV_LOG_ERROR, "Error while sending a packet to the decoder\n"); break; } while (ret >= 0) { //3 ret = avcodec_receive_frame(dec_ctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret <0) { av_log(NULL, AV_LOG_ERROR, "Error while receiving a frame from the decoder\n"); goto end; } frame->pts = frame->best_effort_timestamp; /*4 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) { //5 ret = av_buffersink_get_frame(buffersink_ctx, filt_frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; if (ret <0) goto end; save_frame(filt_frame,out); av_frame_unref(filt_frame); } av_frame_unref(frame); } } av_packet_unref(&packet); } end: avfilter_graph_free(&filter_graph); avcodec_free_context(&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); } exit(0); } 
            
           
          
         
        
       
      
     
    
   
  

本文结尾底部,领取最新最全C++音视频学习提升资料,内容包括(C/C++Linux 服务器开发,FFmpeg webRTC rtmp hls rtsp ffplay srs↓↓↓↓↓↓文章底部

代码会将一个MP4文件在左上角添加一个图片后输出为yuv文件,这个图片如果背景是透明的就是水印啦(主要是懒得做透明背景图片)。

"drawbox=30:10:64:64:red";x= 30,y=10,width=64,height=64,color=red, 绘制一个红色正方形

`"scale=iw*2:ih*2"`视频缩放,iw 表示输入视频的宽,ih表示输入视频的高。*2 表示放大两倍,如果是/2表示缩小两倍

`"crop=320:240:0:0"`视频裁剪,crop=width:height : x : y,其中 width 和 height 表示裁剪后的尺寸,x:y 表示裁剪区域的左上角坐标

还有很多filter可以使用,并且可以实现自定义filter。

原文链接

本文《FFmpeg Filter简单使用》版权归音视频开发老舅所有,引用FFmpeg Filter简单使用需遵循CC 4.0 BY-SA版权协议。


推荐阅读
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 本文详细探讨了VxWorks操作系统中双向链表和环形缓冲区的实现原理及使用方法,通过具体示例代码加深理解。 ... [详细]
  • 本教程涵盖OpenGL基础操作及直线光栅化技术,包括点的绘制、简单图形绘制、直线绘制以及DDA和中点画线算法。通过逐步实践,帮助读者掌握OpenGL的基本使用方法。 ... [详细]
  • Scala 实现 UTF-8 编码属性文件读取与克隆
    本文介绍如何使用 Scala 以 UTF-8 编码方式读取属性文件,并实现属性文件的克隆功能。通过这种方式,可以确保配置文件在多线程环境下的一致性和高效性。 ... [详细]
  • 深入解析 Spring Security 用户认证机制
    本文将详细介绍 Spring Security 中用户登录认证的核心流程,重点分析 AbstractAuthenticationProcessingFilter 和 AuthenticationManager 的工作原理。通过理解这些组件的实现,读者可以更好地掌握 Spring Security 的认证机制。 ... [详细]
  • 本文详细介绍了Java Web应用程序中的过滤器(Filter)功能,包括其作用、实现方式及配置方法。过滤器可以在请求到达目标资源之前对其进行预处理,并在响应返回给客户端之前进行后处理。 ... [详细]
author-avatar
大果粒和小果粒
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有