1. av_find_best_stream
a. 就是要获取音视频及字幕的stream_index
b.以前没有函数av_find_best_stream时,获取索引可以通过如下

for(i=; 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;
}
}

2.video及audio的调用如下:

                       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], -, NULL, );
st_index[AVMEDIA_TYPE_AUDIO] =
av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, st_index[AVMEDIA_TYPE_AUDIO],st_index[AVMEDIA_TYPE_VIDEO], NULL, );

3.说明

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 = -, best_bitrate = -, best_multiframe = -, count, bitrate, multiframe;
unsigned *program = NULL;
const AVCodec *decoder = NULL, *best_decoder = NULL; if (related_stream >= && wanted_stream_nb < ) { //没看到有什么作用,即参数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 = ; 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 >= && 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 < )
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(, 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 - && ret < ) {
program = NULL;
nb_streams = ic->nb_streams;
/* no related stream found, try again with everything */
i = ;
}
}
if (decoder_ret)
*decoder_ret = (AVCodec*)best_decoder;
return ret; //返回就完事了
}

av_find_best_stream函数其实跟以前实现思路是一样的,只不过这儿的条件更多一点而己。

转载地址:http://blog.chinaunix.net/uid-26009923-id-5730055.html

ffmpeg源码分析--av_find_best_stream <转>的更多相关文章

  1. 最新版ffmpeg源码分析

    最新版ffmpeg源码分析一:框架 (ffmpeg v0.9) 框架 最新版的ffmpeg中发现了一个新的东西:avconv,而且ffmpeg.c与avconv.c一个模样,一研究才发现是libav下 ...

  2. ffmpeg 源码分析

    http://blog.csdn.net/liuhongxiangm/article/details/8824761 https://code.google.com/p/ffmpegsource/is ...

  3. ffmpeg源码分析一:概述 (转1)

    原帖地址:http://blog.csdn.net/austinblog/article/details/24800381 首先先看ffmpeg.c文件,有类似于如下的一些变量: InputStrea ...

  4. ffmpeg源码分析五:ffmpeg调用x264编码器的过程分析 (转5)

    原帖地址:http://blog.csdn.net/austinblog/article/details/25127533 该文将以X264编码器为例,解释说明FFMPEG是怎么调用第三方编码器来进行 ...

  5. ffmpeg源码分析三:transcode_init函数 (转3)

    原帖地址:http://blog.csdn.net/austinblog/article/details/25061945 transcode_init()函数是在转换前做准备工作的.下面看看其源代码 ...

  6. ffmpeg源码分析二:main函数和transcode函数 (转2)

    原帖地址:http://blog.csdn.net/austinblog/article/details/24804455 首先从main函数看起,关键解释部分已加注释,该函数在ffmpeg.c文件中 ...

  7. ffmpeg源码分析之媒体打开过程

    int avformat_open_input(AVFormatContext **ps,           const char *filename,           AVInputForma ...

  8. ffmpeg源码分析四:transcode_step函数 (转4)

    原帖地址:http://blog.csdn.net/austinblog/article/details/25099979 该函数的主要功能是一步完整的转换工作,下面看看源代码: static int ...

  9. FFmpeg源码简单分析:libswscale的sws_scale()

    ===================================================== FFmpeg的库函数源码分析文章列表: [架构图] FFmpeg源码结构图 - 解码 FFm ...

随机推荐

  1. linux 命令-case

    case 命令作用: case语句使用于需要进行多重分支的应用情况 case 命令使用场景 在shell中的case结构与C/C++中的switch结构是相同的. 它允许通过判断来选择代码块中多条路径 ...

  2. 认识Applet和Ajax

    一.Applet 1.Applet的定义:Applet是采用Java编程语言编写的小应用程序,该程序可以包含在HTML(标准通用标记语言的一个应用)页中,与在页中包含图像的方式大致相同. Java写出 ...

  3. Windows 10 安装 Docker

    下载Docker For Windows 地址:https://download.docker.com/win/stable/Docker%20for%20Windows%20Installer.ex ...

  4. SSH整合报错:Unable to instantiate Action, testAction, defined for 'test' in namespace '/'testAction

    报错如下: Struts Problem Report Struts has detected an unhandled exception: Messages: testAction Unable ...

  5. jinja 2 filter 使用

    文档地址 http://jinja.pocoo.org/docs/templates/#builtin-filters indent indent(s, width=4, indentfirst=Fa ...

  6. OPENCV Linux安装

    https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

  7. spring boot 教程(二)模板依赖

    在Spring boot中有一个很重要的概念,叫做约定优于配置--软件开发的简约原则.所以Spring boot会按照约定好的文件位置去找我们的包和类. 默认配置 Spring Boot默认提供静态资 ...

  8. 强化学习 车杆游戏 DQN 深度强化学习 Demo

    网上搜寻到的代码,亲测比较好用,分享如下. import gym import time env = gym.make('CartPole-v0') # 获得游戏环境 observation = en ...

  9. I.MX6 Android mmm convenient to use

    # # 主要是记录mmm的简便自动化执行方式,为了减少键盘输入 # ]; then echo echo " Usage: ./remmm.sh <source dirctory> ...

  10. idea 使用spring boot 搭建freemarker模板

      一丶新建maven spring boot 项目 新建好了开始使用模板 先看一个目录结构 二丶配置pox.xml <?xml version="1.0" encoding ...