封装API:avformat_write_header,av_write_trailer,av_interleaved_write_frame

avformat_write_header 写头部,

FFmpeg源码分析:写媒体文件头avformat_write_header()_ffmpeg 加入header-****博客

原型

/**
 * Allocate the stream private data and write the stream header to
 * an output media file.
 *
 * @param s Media file handle, must be allocated with avformat_alloc_context().
 *          Its oformat field must be set to the desired output format;
 *          Its pb field must be set to an already opened AVIOContext.
 * @param options  An AVDictionary filled with AVFormatContext and muxer-private options.
 *                 On return this parameter will be destroyed and replaced with a dict containing
 *                 options that were not found. May be NULL.
 *
 * @return AVSTREAM_INIT_IN_WRITE_HEADER on success if the codec had not already been fully initialized in avformat_init,
 *         AVSTREAM_INIT_IN_INIT_OUTPUT  on success if the codec had already been fully initialized in avformat_init,
 *         negative AVERROR on failure.
 *
 * @see av_opt_find, av_dict_set, avio_open, av_oformat_next, avformat_init_output.
 */
av_warn_unused_result
int avformat_write_header(AVFormatContext *s, AVDictionary **options);

函数作用:

 * Allocate the stream private data and write the stream header to an output media file.

 在复用 时,写文件的头部,这个文件指的就是 mp4,等具体文件的头部。

在使用时,AVDictionary 一般用于 特殊具体的mp4,MP3等的特殊参数,

而通用的参数一般都在 AVFormatContext 中设置好

参数

AVFormatContext *s, 给那个头部写

AVDictionary **options 其他参数

返回值

negative AVERROR on failure.

成功 AVSTREAM_INIT_IN_WRITE_HEADER (对应0)

或 AVSTREAM_INIT_IN_INIT_OUTPUT  (对应1)

av_interleaved_write_frame

原型


/**
 * Write a packet to an output media file ensuring correct interleaving.
 *
 * This function will buffer the packets internally as needed to make sure the
 * packets in the output file are properly interleaved in the order of
 * increasing dts. Callers doing their own interleaving should call
 * av_write_frame() instead of this function.
 *
 * Using this function instead of av_write_frame() can give muxers advance
 * knowledge of future packets, improving e.g. the behaviour of the mp4
 * muxer for VFR content in fragmenting mode.
 *
 * @param s media file handle
 * @param pkt The packet containing the data to be written.
 *            <br>
 *            If the packet is reference-counted, this function will take
 *            ownership of this reference and unreference it later when it sees
 *            fit.
 *            The caller must not access the data through this reference after
 *            this function returns. If the packet is not reference-counted,
 *            libavformat will make a copy.
 *            <br>
 *            This parameter can be NULL (at any time, not just at the end), to
 *            flush the interleaving queues.
 *            <br>
 *            Packet's @ref AVPacket.stream_index "stream_index" field must be
 *            set to the index of the corresponding stream in @ref
 *            AVFormatContext.streams "s->streams".
 *            <br>
 *            The timestamps (@ref AVPacket.pts "pts", @ref AVPacket.dts "dts")
 *            must be set to correct values in the stream's timebase (unless the
 *            output format is flagged with the AVFMT_NOTIMESTAMPS flag, then
 *            they can be set to AV_NOPTS_VALUE).
 *            The dts for subsequent packets in one stream must be strictly
 *            increasing (unless the output format is flagged with the
 *            AVFMT_TS_NONSTRICT, then they merely have to be nondecreasing).
 *            @ref AVPacket.duration "duration") should also be set if known.
 *
 * @return 0 on success, a negative AVERROR on error. Libavformat will always
 *         take care of freeing the packet, even if this function fails.
 *
 * @see av_write_frame(), AVFormatContext.max_interleave_delta
 */
int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);

函数作用

av_interleaved_write_frame是FFmpeg库中的一个函数,用于将一个已编码的媒体数据包(AVPacket)写入到输出媒体文件或流中。
此函数特别关注于维护正确的交错(interleaving)顺序,即确保视频和音频帧按照它们应该播放的顺序被写入,这对于生成可正确播放的多媒体文件至关重要。

参数

AVFormatContext *s,  写入到 AVFormatContext 中

AVPacket *pkt   已编码的媒体数据包(AVPacket)

返回值

 * @return 0 on success, a negative AVERROR on error. Libavformat will always
 *         take care of freeing the packet, even if this function fails.

av_write_trailer 写 尾部

原型

/**
 * Write the stream trailer to an output media file and free the
 * file private data.
 *
 * May only be called after a successful call to avformat_write_header.
 *
 * @param s media file handle
 * @return 0 if OK, AVERROR_xxx on error
 */
int av_write_trailer(AVFormatContext *s);

函数作用

给 AVFormatContext 写 尾部。

av_write_trailer函数是FFmpeg库中的一个函数,用于将文件尾部信息写入媒体文件。它通常在读取和编码媒体流数据后调用,以确保文件结尾的正确性。

参数

返回值

return 0 if OK, AVERROR_xxx on error

上一篇:青训营-豆包MarsCode技术训练营试题解析三十三


下一篇:如何发布及管理npm包