typedef struct AVStream { AVCodecContext *codec; //指向解码器 context,新版本已过期 AVCodecParameters *codecpar; //AVCodecContext解码结构体的结构体参数,解码以前都须要经过它来初始化AVCodecContext. void *priv_data; //指向解复用的流的 context,好比 mp4 的 MovStreamcontext AVRational time_base; . AVRational avg_frame_rate;
//音频频帧速率(1s多少帧,对于音频时,该值多是个浮点数,好比44.1khz, 每一帧里有channel*frame_size个样本数据,
那么帧率为441000/frame_size) AVIndexEntry *index_entries; //用于 seek (滑动至指定位置播放)时使用,用于快速索引关键帧,
如 flv 的 keyframes 索引表和 mp4 的 I帧的索引表都存于此,很重要 int nb_index_entries; //index_entries 的元素的个数 int index_entries_allocated_size; double frame_last_delay; } AVStream;
其中,AVCodecParameters结构体与AVCodecContext里的参数有不少相同的成员,以下所示:ide
typedef struct AVCodecParameters { enum AVMediaType codec_type; //编解码器的类型(视频,音频...) enum AVCodecID codec_id; //标示特定的编解码器,经过该id号能够来找到AVCodec /** * Additional information about the codec (corresponds to the AVI FOURCC). */ uint32_t codec_tag; //编码器的附加信息 /** * Extra binary data needed for initializing the decoder, codec-dependent. * * Must be allocated with av_malloc() and will be freed by * avcodec_parameters_free(). The allocated size of extradata must be at * least extradata_size + AV_INPUT_BUFFER_PADDING_SIZE, with the padding * bytes zeroed. */ uint8_t *extradata; /** * Size of the extradata content in bytes. */ int extradata_size; /** * - video: the pixel format, the value corresponds to enum AVPixelFormat. * - audio: the sample format, the value corresponds to enum AVSampleFormat. */ int format; //帧的格式,若是未知或未设置为-1
//对于视频帧,参考AVPixelFormat枚举值,好比:AV_PIX_FMT_YUV420P
//对于音频帧,参考AVSampleFormat枚举值,好比:AV_SAMPLE_FMT_U8 /** * The average bitrate of the encoded data (in bits per second). */ int64_t bit_rate; int bits_per_coded_sample; /** * Codec-specific bitstream restrictions that the stream conforms to. */ int profile; int level; /** * Video only. The dimensions of the video frame in pixels. */ int width; int height; //视频帧的尺寸(以像素为单位)
//这里的宽高不必定会有值(对于流媒体视频而言),不过用户能够在解码后经过if (frame->width > 0 && frame->height > 0)来判断是否为视频流 /** * Video only. The aspect ratio (width / height) which a single pixel * should have when displayed. * * When the aspect ratio is unknown / undefined, the numerator should be * set to 0 (the denominator may have any value). */ AVRational sample_aspect_ratio; //像素的宽高比,经过av_q2d()来获取值,若是未知/未指定,为0/1。 /** * Video only. The order of the fields in interlaced video. */ enum AVFieldOrder field_order; /** * Video only. Additional colorspace characteristics. */ enum AVColorRange color_range; //图像的编码格式(MPEG/JPEG),解码时,由库设置,编码时,由用户来设置 enum AVColorPrimaries color_primaries; //图像源初选的色度坐标 enum AVColorTransferCharacteristic color_trc; //图像颜色传输特性 enum AVColorSpace color_space; //图像彩色空间类型,解码时,由库设置,编码时,由用户来设置,好比等于AVCOL_SPC_RGB时,那么color_trc等于AVCOL_TRC_IEC61966_2_1 enum AVChromaLocation chroma_location; //储存的颜色色度样品的位置 /** * Video only. Number of delayed frames. */ int video_delay; //延迟帧数 /** * Audio only. The channel layout bitmask. May be 0 if the channel layout is * unknown or unspecified, otherwise the number of bits set must be equal to * the channels field. */ uint64_t channel_layout; //声道布局,仅用于音频 int channels; //音频通道数量,仅用于音频 int sample_rate; //音频数据的采样率。 //用户能够经过 if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))来判断该frame是否为音频
/** * Audio only. Audio frame size, if known. Required by some formats to be static. */ int frame_size; //音频帧的样本数据数量 ... ... } AVCodecParameters;
videoindex = -1; audioindex = -1; for (uint i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) //找到视频流 { videoindex = i; } else if(pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) //找到音频流 { audioindex = i; } }
第二种,使用av_find_best_stream()获取方法获取函数
int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream,
AVCodec **decoder_ret, int flags); //根据type参数从ic-> streams[]里获取用户要找的流,找到成功后则返回streams[]中对应的序列号,不然返回 //ic: AVFormatContext结构体句柄 //type:要找的流参数,好比: AVMEDIA_TYPE_VIDEO,AVMEDIA_TYPE_AUDIO,AVMEDIA_TYPE_SUBTITLE等 //wanted_stream_nb: 用户但愿请求的流号,设为-1用于自动选择 //related_stream: 试着找到一个相关的流(好比多路视频时),通常设为-1,表示不须要 //decoder_ret:若是非空,则返回所选流的解码器(至关于调用avcodec_find_decoder()函数) //flags:未定义
获取以下:布局
videoindex = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); audioindex = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);