1.
av_find_best_stream
a. 就是要获取音视频及字幕的stream_index
b.以前没有函数av_find_best_stream时,获取索引可以通过如下
2.video及audio的调用如下:
3.说明
av_find_best_stream函数其实跟以前实现思路是一样的,只不过这儿的条件更多一点而己。
a. 就是要获取音视频及字幕的stream_index
b.以前没有函数av_find_best_stream时,获取索引可以通过如下
- for(i=0; i<is->pFormatCtx->nb_streams; i++)
- {
- if(is->pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
- {
- is->videoindex= i;
- }
- if(is->pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
- {
- is->sndindex= i;
- }
- }
- ic type wanted_stream_nb related_stream decoder_ret flag
- st_index[AVMEDIA_TYPE_VIDEO] =
- av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, st_index[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
- st_index[AVMEDIA_TYPE_AUDIO] =
- av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, st_index[AVMEDIA_TYPE_AUDIO],st_index[AVMEDIA_TYPE_VIDEO], NULL, 0);
- int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream,
- AVCodec **decoder_ret, int flags)
- {
- int i, nb_streams = ic->nb_streams;
- int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1, best_bitrate = -1, best_multiframe = -1, count, bitrate, multiframe;
- unsigned *program = NULL;
- const AVCodec *decoder = NULL, *best_decoder = NULL;
-
- if (related_stream >= 0 && wanted_stream_nb < 0) { //没看到有什么作用,即参数related_stream在这儿没有用
- AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream);
- if (p) {
- program = p->stream_index;
- nb_streams = p->nb_stream_indexes;
- }
- }
- for (i = 0; i < nb_streams; i++) { //对于只有音频与视频流的媒体文件来说nb_streams=2
- int real_stream_index = program ? program[i] : i; //program=NULL,所以real_stream_index=i;
- AVStream *st = ic->streams[real_stream_index];
- AVCodecContext *avctx = st->codec;
- if (avctx->codec_type != type) //以下三个if是过滤条件
- continue;
- if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
- continue;
- if (wanted_stream_nb != real_stream_index &&
- st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED |
- AV_DISPOSITION_VISUAL_IMPAIRED))
- continue;
- if (type == AVMEDIA_TYPE_AUDIO && !(avctx->channels && avctx->sample_rate))
- continue;
- if (decoder_ret) { //decoder_ret=NULL,所以下面这个find_decoder也没有调用
- decoder = find_decoder(ic, st, st->codec->codec_id);
- if (!decoder) {
- if (ret < 0)
- ret = AVERROR_DECODER_NOT_FOUND;
- continue;
- }
- }
- count = st->codec_info_nb_frames;
- bitrate = avctx->bit_rate;
- if (!bitrate)
- bitrate = avctx->rc_max_rate;
- multiframe = FFMIN(5, count);
- if ((best_multiframe > multiframe) ||
- (best_multiframe == multiframe && best_bitrate > bitrate) ||
- (best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count))
- continue;
- best_count = count;
- best_bitrate = bitrate;
- best_multiframe = multiframe;
- ret = real_stream_index; //到这儿real_stream_index就是匹配了三个if的index了,不匹配的都continue了
- best_decoder = decoder;
- if (program && i == nb_streams - 1 && ret < 0) {
- program = NULL;
- nb_streams = ic->nb_streams;
- /* no related stream found, try again with everything */
- i = 0;
- }
- }
- if (decoder_ret)
- *decoder_ret = (AVCodec*)best_decoder;
- return ret; //返回就完事了
- }