• FFmpeg 常用API


    一、通用API

    1.1 av_register_all()

    初始化 libavformat 和注册所有的复用器、解复用器和协议处理器。如果不调用这个函数,可以调用下面的三个函数来选择支持的格式。

    • 注册复用器的函数是av_register_output_format()
    • 注册解复用器的函数是av_register_input_format()
    • 注册协议处理器的函数是ffurl_register_protocol()

    注:FFmpeg4.0 以上的版本,这个函数已经被废弃。


    1.2 内存的分配和释放(av_malloc()、av_free()等)

    av_malloc() 和 av_free() 都是简单的封装了系统函数 malloc() 和free(),并做了一些错误检查工作。同理的还有 av_realloc()。


    1.3 avcodec_find_encoder() 和 avcodec_find_decoder()

    avcodec_find_encoder() 用于查找 FFmpeg 的编码器,avcodec_find_decoder() 用于查找 FFmpeg 的解码器,声明都位于 libavcodec\avcodec.h。其原型如下:

    1. // 函数的参数是一个编码器的ID,返回查找到的编码器(没有找到就返回NULL)。
    2. AVCodec *avcodec_find_encoder(enum AVCodecID id);
    3. // 函数的参数是一个解码器的ID,返回查找到的解码器(没有找到就返回NULL)。
    4. AVCodec *avcodec_find_decoder(enum AVCodecID id);


    1.4 avcodec_open2()

    用于初始化一个视音频编解码器的 AVCodecContext,声明位于 libavcodec\utils.c。其原型如下:

    int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)  
    
    • avctx:需要初始化的 AVCodecContext。
    • codec:输入的AVCodec。
    • options:一些选项。例如使用libx264编码的时候,“preset”,“tune”等都可以通过该参数设置。


    1.5 avcodec_close()

    用于关闭编码器,声明位于 libavcodec\utils.c。其原型如下:

    int avcodec_close(AVCodecContext *avctx)
    

    该函数只有一个参数,就是需要关闭的编码器的 AVCodecContext。

    二、解码API

    下面介绍解码需要用到的几个函数,声明都位于 libavformat\avformat.h。

    2.1 avformat_open_input()

    打开输出的流和读取头信息。其原型如下:

    int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options) 
    
    • ps:函数调用成功之后处理过的 AVFormatContext 结构体。
    • url:打开的视音频流的 URL。
    • fmt:强制指定 AVFormatContext 中 AVInputFormat 的。这个参数一般情况下可以设置为 NULL,这样 FFmpeg 可以自动检测 AVInputFormat。
    • options:附加的一些选项,一般情况下可以设置为 NULL。

    函数执行成功的话,其返回值大于等于 0。

    【文末扫码进qun,免费分享资料包括《Andoird音视频开发必备手册+音视频学习视频+学习文档资料包+大厂面试真题+2022最新学习路线图》等等


     

    2.2 avformat_find_stream_info()

    读取音视频数据来获取一些相关的信息。其原型如下:

    int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)  
    


    2.3 av_read_frame

    读取码流中的音频若干帧或者视频一帧。例如,解码视频的时候,每解码一个视频帧,需要先调用 av_read_frame() 获得一帧视频的压缩数据,然后才能对该数据进行解码。其原型如下:

    int av_read_frame(AVFormatContext *s, AVPacket *pkt)    
    


    2.4 avformat_close_input()

    关闭打开的流。其原型如下:

    void avformat_close_input(AVFormatContext **s)  
    

    三、编码API

    在基于 FFmpeg 的音视频编码器程序中,avformat_alloc_output_context2() 函数通常是第一个调用的函数(除了组件注册函数 av_register_all())。另外介绍 FFmpeg 的写文件用到的 3 个函数,声明都位于 libavformat\avformat.h:

    • av_write_frame() 用于写视频数据;
    • avformat_write_header() 用于写视频文件头;
    • av_write_trailer() -用于写视频文件尾。


    3.1 avformat_alloc_output_context2()

    初始化一个用于输出的 AVFormatContext 结构体。其原型如下:

    int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat * oformat, const char * format_name, const char * filename)
    


    3.2 avformat_write_header()

    分配一个 stream 的私有数据而且写 stream 的 header 到一个输出的媒体文件。其原型如下:

    int avformat_write_header(AVFormatContext *s, AVDictionary ** options)
    


    3.3 av_write_frame()

    用于输出一帧视音频数据。其原型如下:

    int av_write_frame(AVFormatContext *s, AVPacket *pkt)
    

    参数 s 为用于输出的 AVFormatContext,参数 pkt 为等待输出的 AVPacket。


    3.4 av_write_trailer()

    用于输出文件尾。其原型如下:

    int av_write_trailer(AVFormatContext *s)
    

    它只需要指定一个参数,即用于输出的 AVFormatContext,函数正常执行后返回值等于 0。

    四、图像处理API

    libswscale 是一个主要用于处理图片像素数据的类库。可以完成图片像素格式的转换,图片的拉伸等工作。libswscale 常用的函数数量很少,一般情况下就3个,声明都位于 libswscale\swscale.h:

    1. sws_getContext():分配和返回一个SwsContext
    2. sws_scale():处理图像数据。
    3. sws_freeContext():释放一个SwsContext

    其中 sws_getContext() 也可以用 sws_getCachedContext() 取代。


    4.1 sws_getContext()

    分配和返回一个 SwsContext。其原型如下:

    1. struct SwsContext* sws_getContext ( int srcW,
    2. int srcH,
    3. enum AVPixelFormat srcFormat,
    4. int dstW,
    5. int dstH,
    6. enum AVPixelFormat dstFormat,
    7. int flags,
    8. SwsFilter * srcFilter,
    9. SwsFilter * dstFilter,
    10. const double * param
    11. )


    4.2 sws_scale()

    处理图像数据。其原型如下:

    1. int sws_scale(struct SwsContext *c,
    2. const uint8_t * const srcSlice[],
    3. const int srcStride[], int srcSliceY,
    4. int srcSliceH, uint8_t *const dst[],
    5. const int dstStride[]) )


    4.3 sws_freeContext()

    释放一个 SwsContext。其原型如下:

    void sws_freeContext(struct SwsContext *swsContext)

     

    五、其它API

    5.1 日志输出系统(av_log()等)

    av_log() 是 FFmpeg 中输出日志的函数。一般情况下 FFmpeg 类库的源代码中是不允许使用 printf() 这种的函数的,所有的输出一律使用 av_log()。av_log() 的声明位于 libavutil\log.h,其原型如下:

    1. void av_log(void* avcl, int level, const char *fmt, ...)

    函数最后一个参数是 “…”。
    在 C 语言中,在函数参数数量不确定的情况下使用 “…” 来代表参数。例如 printf() 的原型定义为:int printf (const char*, ...)

     

  • 相关阅读:
    vue 项目打包性能分析插件 webpack-bundle-analyzer
    基础会计学知识点汇总
    移动宣传舞台车设计及运动仿真(lunwen+开题报告+初稿+cad图纸)
    Alibaba内部传出的面试秘技,秋招offer尽收囊中
    DAST 黑盒漏洞扫描器 第六篇:运营篇(终)
    精品基于Uniapp+SSM实现的实验室设备预约管理APP
    001 Creating your first app with PySide6
    【GoWeb框架初探————XORM篇】
    本地MQTT服务器搭建(EMQX)
    华为云云耀云服务器L实例评测|使用宝塔面板管理服务器教学
  • 原文地址:https://blog.csdn.net/yinshipin007/article/details/125879606