ffmpeg-filter 入门




filter 思想

  • filter 架构思想中第一个概念是 Graph,一般翻译为画布,如果 Graph 看做是桌子的话,那 filters 们就是桌子上的“悲剧”。所以先要有 Graph,然后再将 filter 摆在上面, filter是身上有 pin 接口, pin 的作用是统一数据接口,然后还需要一个 link 的动作, link 的作用是将指定的 2 个 filter 通过其 pin 接口连接起来,这样就形成了一个完整的 filter graph或是叫 filter link list。
  • 如果只有 filter graph 的存在,它只是一堆参数数据和代码,并不能运行,需要一个动力泵或是动力引擎将整个过程驱动起来,这就像人还缺一颗心脏一样,那人的血液就是filter graph 的数据流。
  • 这样 FFmpeg 就把驱动的能力交给了 filter 框架外面来做,通过向 filter graph 的首个 filter 推数据和从 filter graph 的末尾 filter 拉数据从而驱动整个 filter graph 的数据流动。







ffmpeg -i file_copy.ts -i logo.png -filter_complex "[1:v]scale=100:100[logo];[0:v][logo]overlay=x=main_w-100:y=main_h-100" output.mp4


  • -filter_complex:滤镜必选参数,后面跟滤镜命令
  • [1:v] :输入pin,表示视频的第1路
  • scale=100:100 :对[1:v]输入pin的处理,缩放成100:100
  • [logo] :输出pin
  • ; 每个滤镜分割
  • 0:v :两个输入,第一个视频,上一个滤镜的输出
  • overlay=x=main_w-100:y=main_h-100 :滤镜动作




/** An instance of a filter */
struct AVFilterContext {
    const AVClass *av_class;        ///< needed for av_log() and filters common options

    const AVFilter *filter;         ///< the AVFilter of which this is an instance

    char *name;                     ///< name of this filter instance

    AVFilterPad   *input_pads;      ///< array of input pads
    AVFilterLink **inputs;          ///< array of pointers to input links
    unsigned    nb_inputs;          ///< number of input pads

    AVFilterPad   *output_pads;     ///< array of output pads
    AVFilterLink **outputs;         ///< array of pointers to output links
    unsigned    nb_outputs;         ///< number of output pads

    void *priv;                     ///< private data for use by the filter

    struct AVFilterGraph *graph;    ///< filtergraph this filter belongs to

     * Type of multithreading being allowed/used. A combination of
     * AVFILTER_THREAD_* flags.
     * May be set by the caller before initializing the filter to forbid some
     * or all kinds of multithreading for this filter. The default is allowing
     * everything.
     * When the filter is initialized, this field is combined using bit AND with
     * AVFilterGraph.thread_type to get the final mask used for determining
     * allowed threading types. I.e. a threading type needs to be set in both
     * to be allowed.
     * After the filter is initialized, libavfilter sets this field to the
     * threading type that is actually used (0 for no multithreading).
    int thread_type;

     * An opaque struct for libavfilter internal use.
    AVFilterInternal *internal;

    struct AVFilterCommand *command_queue;

    char *enable_str;               ///< enable expression string
    void *enable;                   ///< parsed expression (AVExpr*)
    double *var_values;             ///< variable values for the enable expression
    int is_disabled;                ///< the enabled state from the last expression evaluation

     * For filters which will create hardware frames, sets the device the
     * filter should create them in.  All other filters will ignore this field:
     * in particular, a filter which consumes or processes hardware frames will
     * instead use the hw_frames_ctx field in AVFilterLink to carry the
     * hardware context information.
    AVBufferRef *hw_device_ctx;

     * Max number of threads allowed in this filter instance.
     * If <= 0, its value is ignored.
     * Overrides global number of threads set per filter graph.
    int nb_threads;

     * Ready status of the filter.
     * A non-0 value means that the filter needs activating;
     * a higher value suggests a more urgent activation.
    unsigned ready;

     * Sets the number of extra hardware frames which the filter will
     * allocate on its output links for use in following filters or by
     * the caller.
     * Some hardware filters require all frames that they will use for
     * output to be defined in advance before filtering starts.  For such
     * filters, any hardware frame pools used for output must therefore be
     * of fixed size.  The extra frames set here are on top of any number
     * that the filter needs internally in order to operate normally.
     * This field must be set before the graph containing this filter is
     * configured.
    int extra_hw_frames;