封装API:avformat_alloc_output_context2,avformat_free_context,avformat_new_stream,avio_open,avio_close

avformat_alloc_output_context2

原型

/**
 * 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           pointee 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, const AVOutputFormat *oformat,
                                   const char *format_name, const char *filename);

函数作用

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.

创建一个 avformatContext,这个avformatContext是为了 封装使用的,

最后需要使用 avformat_free_context()函数来释放 avformatContext

            //实验测试如果 _avformatContext->pb 是存在的,最好调用 avio_closep 先将 _avformatContext->pb 关闭。
            //我们观察 在 avformat_free_context 实现方法中并没有释放 this->_avformatContext->pb,因此这里要自己写一下。
            if (this->_avformatContext->pb) {
                avio_closep(&this->_avformatContext->pb);
            }
            avformat_free_context(this->_avformatContext);

参数

AVFormatContext **ctx,

实际开发中 AVFormatContext *avFormatContetx = nullptr;

avformat_alloc_output_context2(&avFormatContetx ,nullptr,nullptr,"a.mp4");

const AVOutputFormat *oformat,

实际开发中传递为NULL

AVOutputFormat* oformat用来指定 avformatContext的 参数 -- (AVOutputFormat* oformat),如果设置为NULL,则会根据第三个参数 format_name 和 第四个参数 filename 决定avformatContext的 AVOutputFormat

format to use for allocating the context, if NULL format_name and filename are used instead

const char *format_name,

实际开发中传递为NULL

指定AVOutputFormat* oformat的name,进而用来指定 avformatContext的 参数 -- (AVOutputFormat* oformat),如果为null,会根据第四个参数filename 决定avformatContext的 AVOutputFormat

the name of output format to use for allocating the  context, if NULL filename is used instead

const char *filename

打开那个文件,此filename一般会有后缀名,例如 a.mp4。

返回值

return  >= 0 in case of success, a negative AVERROR code in case of  failure

avformat_free_context

原型


/**
 * Free an AVFormatContext and all its streams.
 * @param s context to free
 */
void avformat_free_context(AVFormatContext *s);

函数作用

将 AVFormatContext* 指向的空间全部释放。

但是 不会将自己置为nullptr

因此使用时:一般会同时将 avformatContext设置为nullptr

avformat_free_context(avformatContext);

avformatContext = nullptr;

另外,在实际开发中,我们 

avformatContext  中有一个 AVIOContext *pb;,是用来打开IO的,写文件的时候用的,使用的时候要调用  int avio_open(AVIOContext **s, const char *url, int flags);,不用的时候,要调用int avio_closep(AVIOContext **s)关闭。

实际开发中:

avfromat_free_context并不会调用avio_closep函数,因此我们要自己调用 avfromat_free_context 之前,要调用 avio_closep。

参数

返回值

avformat_new_stream

原型

/**
 * Add a new stream to a media file.
 *
 * When demuxing, it is called by the demuxer in read_header(). If the
 * flag AVFMTCTX_NOHEADER is set in s.ctx_flags, then it may also
 * be called in read_packet().
 *
 * When muxing, should be called by the user before avformat_write_header().
 *
 * User is required to call avformat_free_context() to clean up the allocation
 * by avformat_new_stream().
 *
 * @param s media file handle
 * @param c unused, does nothing
 *
 * @return newly created stream or NULL on error.
 */
AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c);

函数作用

new一个avstream,使用的额

参数

AVFormatContext *s,

const AVCodec *c  没有使用,一般传递NULL

返回值

成功返回一个 avstream *, 失败返回nullptr

avio_open

原型

/**
 * Create and initialize a AVIOContext for accessing the
 * resource indicated by url.
 * @note When the resource indicated by url has been opened in
 * read+write mode, the AVIOContext can be used only for writing.
 *
 * @param s Used to return the pointer to the created AVIOContext.
 * In case of failure the pointed to value is set to NULL.
 * @param url resource to access
 * @param flags flags which control how the resource indicated by url
 * is to be opened
 * @return >= 0 in case of success, a negative value corresponding to an
 * AVERROR code in case of failure
 */
int avio_open(AVIOContext **s, const char *url, int flags);

函数作用

打开AVIOContext用于 用什么样的方式(flags,类似AVIO_FLAG_WRITE,AVIO_FLAG_READ)  访问 url

实际调用

avio_open(&outfileAVFormatContext->pb, outfilename, AVIO_FLAG_WRITE);

参数

AVIOContext **s,

在复用的时候传递 AVFormatContext->pb

const char *url,

要打开的文件

int flags

AVIO_FLAG_WRITE,AVIO_FLAG_READ,AVIO_FLAG_READ_WRITE,AVIO_FLAG_NONBLOCK,AVIO_FLAG_DIRECT

返回值

return >= 0 in case of success, a negative value corresponding to an
 * AVERROR code in case of failure

avio_closep

原型

/**
 * Close the resource accessed by the AVIOContext *s, free it
 * and set the pointer pointing to it to NULL.
 * This function can only be used if s was opened by avio_open().
 *
 * The internal buffer is automatically flushed before closing the
 * resource.
 *
 * @return 0 on success, an AVERROR < 0 on error.
 * @see avio_close
 */

int avio_closep(AVIOContext **s);
 

函数作用:

关闭参数Close the resource accessed by the AVIOContext *s,顺便将 *s 置为nullptr

实际开发中 

avio_closep(&s);

参数:

AVIOContext **s

返回值:

0表示成功,<0表示有error,可以用 AVERROR 打印error

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值