文章首发于我的个人博客
我们在分析AVFormatContext结构的时候提到其中的一个成员streams。使用该成员我们就可以得到AVStream对象。
unsigned int nb_streams;
AVStream** streams;
nb_streams是当前轨道数,就是流数量,streams是轨道的指针数组。一般而言一个视频文件会有一个视频流和一个音频流,那就是两个AVStream结构。
对于AVStream,我们常用的成员有:
//(1)标识该视频/音频流
int index;
//(2)指向该音频/视频流的AVCodecContext(已被废弃,不推荐使用)
AVCodecContext* codec;
//(3)获得AVCodecParameters对象,用于替代AVCodecContext(推荐使用)
AVCodecParameters* codecpar;
//(4)该视频长度;
int64_t duration;
//(5)视频平均帧率
AVRational avg_frame_rate;
//其中,AVRational表示有理数,他有两个参数
//avg_frame_rate.num : 分子
//avg_frame_rate.den : 母
//平均帧率 = avg_frame_rate.num / avg_frame_rate.den
AVStream详细定义:
/**
* Stream structure.
* New fields can be added to the end with minor version bumps.
* Removal, reordering and changes to existing fields require a major
* version bump.
* sizeof(AVStream) must not be used outside libav*.
*/
typedef struct AVStream {
int index; /**< stream index in AVFormatContext */
/**
* Format-specific stream ID.
* decoding: set by libavformat
* encoding: set by the user, replaced by libavformat if left unset
*/
int id;
#if FF_API_LAVF_AVCTX
/**
* @deprecated use the codecpar struct instead
*/
attribute_deprecated
AVCodecContext *codec;
#endif
void *priv_data;
/**
* This is the fundamental unit of time (in seconds) in terms
* of which frame timestamps are represented.
*
* decoding: set by libavformat
* encoding: May be set by the caller before avformat_write_header() to
* provide a hint to the muxer about the desired timebase. In
* avformat_write_header(), the muxer will overwrite this field
* with the timebase that will actually be used for the timestamps
* written into the file (which may or may not be related to the
* user-provided one, depending on the format).
*/
AVRational time_base;
/**
* Decoding: pts of the first frame of the stream in presentation order, in stream time base.
* Only set this if you are absolutely 100% sure that the value you set
* it to really is the pts of the first frame.
* This may be undefined (AV_NOPTS_VALUE).
* @note The ASF header does NOT contain a correct start_time the ASF
* demuxer must NOT set this.
*/
int64_t start_time;
/**
* Decoding: duration of the stream, in stream time base.
* If a source file