libavformat/avformat.h
avformat_alloc_context
来创建,否则会出错。 struct AVInputFormat *iformat;
struct AVOutputFormat *oformat;
主要是用来复用和解复用的。
AVIOContext *pb;
有关I/O的上下文结构体,这个变量要用户手动释放,除非iformat/oformat
的flags
字段 被设置为AVFMT_NOFILE
。给一个释放的参考代码
if (outputContext->oformat->flags & AVFMT_NOFILE)
{
avio_close(m_outputContext->pb);
}
avformat_free_context(m_outputContext);
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])
}
char filename[1024]; // 弃用了
char *url;
首先filename
字段已经被弃用了,转而使用了url字段来存储输入或者输出的文件或url。相比filename字段,url没有长度的限制。
int64_t start_time;
int64_t duration;
start_time是第一帧时间戳,duration是整个流的时长。需要注意的是这两个变量的单位都是微秒(us),而不是秒(s)。start_time会由ffmpeg自动推导出来,不用手动设置。
int64_t bit_rate
整个流的比特率,这个值ffmpeg会自动设置,不需要手动设置。
libavformat/avformat.h
const char *name;
const char *long_name;
const char *extensions;
name是一个格式名称,long_name则是这个格式的补充说明,类似于备注。extensions很好理解,就是文件拓展名。
int (*read_header)(struct AVFormatContext *);
函数指针类型的字段。读取格式头部并初始化AVFormatContext结构体。如果成功,返回0。创建新的流需要调用avformat_new_stream
函数
int (*read_packet)(struct AVFormatContext *, AVPacket *pkt);
函数指针类型的字段。从上下文中(第一个参数)读取一个数据包,并将内容填充到pkt中。与此同时,pts和flag也会被设置危险相应的值。
int (*read_close)(struct AVFormatContext *);
函数指针类型的字段。关闭一条读取流,但是AVFormatContext和AVStreams的空间并不会释放。
libavformat/avformat.h
const char *name;
const char *long_name;
const char *extensions;
name是一个格式名称,long_name则是这个格式的补充说明,类似于备注。extensions很好理解,就是文件拓展名。
enum AVCodecID audio_codec; /**< default audio codec */
enum AVCodecID video_codec; /**< default video codec */
enum AVCodecID subtitle_codec; /**< default subtitle codec */
音视频和字幕的编码格式指定,这里都是用默认的编码格式的。
int (*write_header)(struct AVFormatContext *);
将对应格式的头部写到输出流中,比如FLV格式的头部信息。
int (*write_packet)(struct AVFormatContext *, AVPacket *pkt);
将一个数据包写入到输出流中。如果flag
字段设置了AVFMT_ALLOW_FLUSH
标志,pkt变量则可以为NULL,此时则表明要刷新数据到复用器中。当执行了刷新操作,如果这个函数返回0,则表示还有数据待刷新,如果返回为1,则表明所有的数据都被刷新到复用器中了。