视频编码的流程:
![](https://img-blog.csdnimg.cn/img_convert/70012dfb52e8611454f667dca5a705c8.webp?x-oss-process=image/format,png)
流程中各个函数的含义:
1. av_register_all()
功能:
初始化所有组件,该函数在所有基于ffmpeg的应用程序中几乎都是第一个被调用的。只有调用了该函数,才能使用复用器,编码器等。
该接口内部的调用为:
(1).avcodec_register_all(),该接口内部执行步骤:
- 注册硬件加速器:REGISTER_HWACCEL()
- 注册音视频编码器:REGISTER_ENCODER()
- 注册音视频解码器:REGISTER_DECODER()
- 打包注册:REGISTER_ENCDEC()
- 注册解析器:REGISTER_PARSER()
(2).执行复用器和解复用器的注册:
- 注册复用器:REGISTER_MUXER()
- 注册解复用器:REGISTER_DEMUXER()
- 既有解复用器又有复用器: REGISTER_MUXDEMUX(X,x));
2. avformat_alloc_output_context2()
功能
avformat_alloc_output_context2()函数可以初始化一个用于输出的AVFormatContext结构体。它的声明位于libavformat\avformat.h。
函数声明
/**
* Allocate an AVFormatContext for an output format.
* avformat_free_context() can be used to free the context and
* everything allocated by the framework within it.
*
* @param *ctx is set to the created format context, or to NULL in
* case of failure
* @param oformat format to use for allocating the context, if NULL
* format_name and filename are used instead
* @param format_name the name of output format to use for allocating the
* context, if NULL filename is used instead
* @param filename the name of the filename to use for allocating the
* context, may be NULL
* @return >= 0 in case of success, a negative AVERROR code in case of
* failure
*/
int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat,
const char *format_name, const char *filename);
参数说明
ctx:函数调用成功之后创建的AVFormatContext结构体。
oformat:指定AVFormatContext中的AVOutputFormat,用于确定输出格式。如果指定为NULL,可以设定后两个参数(format_name或者filename)由FFmpeg猜测输出格式。
PS:使用该参数需要自己手动获取AVOutputFormat,相对于使用后两个参数来说要麻烦一些。
format_name:指定输出格式的名称。根据格式名称,FFmpeg会推测输出格式。输出格式可以是“flv”,“mkv”等等。
返回值
函数执行成功的话,其返回值大于等于0。
调用关系
avformat_alloc_output_context2()的流程如要包含以下2步:
调用avformat_alloc_context()初始化一个默认的AVFormatContext。
如果指定了输入的AVOutputFormat,则直接将输入的AVOutputFormat赋值给AVOutputFormat的oformat。如果没有指定输入的AVOutputFormat,就需要根据文件格式名称或者文件名推测输出的AVOutputFormat。无论是通过文件格式名称还是文件名推测输出格式,都会调用一个函数av_guess_format()。