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);