直播入门(附录一)FFmpeg关键类一览表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SKY453589103/article/details/89366943

关键类

AVFormatContext

  • 定义
    这里只给出源码的路径libavformat/avformat.h
  • 简介
    FFmpeg将各种封装格式通过在这个结构体进行抽象。这个结构体在解封装中扮演者穿针引线的作用,通过这个结构体,可以将编码后的数据分装成FLV,MP4,AVI等格式。创建这个结构体不能通过简单的mallloc或者new来创建,需要通过函数avformat_alloc_context来创建,否则会出错。
  • 关键变量
    • iformat 和 oformat
    struct AVInputFormat *iformat;
    struct AVOutputFormat *oformat;

主要是用来复用和解复用的。

    • pb
AVIOContext *pb;

有关I/O的上下文结构体,这个变量要用户手动释放,除非iformat/oformatflags字段 被设置为AVFMT_NOFILE。给一个释放的参考代码

if (outputContext->oformat->flags & AVFMT_NOFILE)
{
    avio_close(m_outputContext->pb);
}
avformat_free_context(m_outputContext);
    • nb_streams 和 streams
    unsigned int nb_streams;
    AVStream **streams;

nb_streams 表示音视频流的个数,streams则是音频流的数据,数组大小为nb_streams 。需要注意的是,要操作这两个变量只能通过avformat_new_stream函数来增加,通过avformat_free_context函数来释放。
这里给个使用的例子。

for (int index  = 0; index < nb_streams; ++index)
{
    handle(streams[index])
}
    • filename 和 url
char filename[1024];  // 弃用了
char *url;

首先filename字段已经被弃用了,转而使用了url字段来存储输入或者输出的文件或url。相比filename字段,url没有长度的限制。

    • start_time 和 duration
int64_t start_time;
int64_t duration;

start_time是第一帧时间戳,duration是整个流的时长。需要注意的是这两个变量的单位都是微秒(us),而不是秒(s)。start_time会由ffmpeg自动推导出来,不用手动设置。

    • bit_rate
int64_t bit_rate

整个流的比特率,这个值ffmpeg会自动设置,不需要手动设置。

AVInputFormat

  • 定义
    这里只给出源码的路径libavformat/avformat.h
  • 简介
    AVInputFormat 通过一系列的函数指针,实现了各种不同文件操作。从名字就可以看出来,该结构体主要是用来处理输入流的。
    通过调用av_register_all(),FFmpeg会将所有可用的解复用器保存在以first_iformat为头,last_iformat为尾的链表。
  • 关键变量
    • name 、long_name 和 extensions
const char *name;
const char *long_name;
const char *extensions;

name是一个格式名称,long_name则是这个格式的补充说明,类似于备注。extensions很好理解,就是文件拓展名。

    • read_header
int (*read_header)(struct AVFormatContext *);

函数指针类型的字段。读取格式头部并初始化AVFormatContext结构体。如果成功,返回0。创建新的流需要调用avformat_new_stream函数

    • read_packet
int (*read_packet)(struct AVFormatContext *, AVPacket *pkt);

函数指针类型的字段。从上下文中(第一个参数)读取一个数据包,并将内容填充到pkt中。与此同时,pts和flag也会被设置危险相应的值。

    • read_close
int (*read_close)(struct AVFormatContext *);

函数指针类型的字段。关闭一条读取流,但是AVFormatContext和AVStreams的空间并不会释放。

AVOutputFormat

  • 定义
    这里只给出源码的路径libavformat/avformat.h
  • 简介
    AVOutputFormat跟AVInputFormat非常类似,也是通过一系列的函数指针,实现了各种不同文件操作。该结构体主要是用来处理输出的。
  • 关键变量
    • name 、long_name 和 extensions
const char *name;
const char *long_name;
const char *extensions;

name是一个格式名称,long_name则是这个格式的补充说明,类似于备注。extensions很好理解,就是文件拓展名。

    • audio_codec,video_codec和subtitle_codec
enum AVCodecID audio_codec;    /**< default audio codec */
enum AVCodecID video_codec;    /**< default video codec */
enum AVCodecID subtitle_codec; /**< default subtitle codec */

音视频和字幕的编码格式指定,这里都是用默认的编码格式的。

    • write_header
int (*write_header)(struct AVFormatContext *);

将对应格式的头部写到输出流中,比如FLV格式的头部信息。

    • write_packet
int (*write_packet)(struct AVFormatContext *, AVPacket *pkt);

将一个数据包写入到输出流中。如果flag字段设置了AVFMT_ALLOW_FLUSH标志,pkt变量则可以为NULL,此时则表明要刷新数据到复用器中。当执行了刷新操作,如果这个函数返回0,则表示还有数据待刷新,如果返回为1,则表明所有的数据都被刷新到复用器中了。

猜你喜欢

转载自blog.csdn.net/SKY453589103/article/details/89366943