ffmepg 获取视频音频流索引的2种方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangpengzp/article/details/88928503

遍历流的方式

 AVFormatContext *ic = NULL;
 int re = avformat_open_input(&ic,videoPath,0,0);
 int vedioStream = 0;
 int audioStream = 0;
 for(int i= 0; i < ic->nb_streams; i++)
    {
        AVStream *avStream = ic->streams[i];
        if(avStream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            LOGW("视频数据");         

        }else if(avStream->codecpar->codec_type =AVMEDIA_TYPE_AUDIO )
        {
            LOGW("音频数据");
            audioStream= i;
        }
    }

av_find_best_stream 的方式

函数定义:

/**
 * Find the "best" stream in the file.
 * The best stream is determined according to various heuristics as the most
 * likely to be what the user expects.
 * If the decoder parameter is non-NULL, av_find_best_stream will find the
 * default decoder for the stream's codec; streams for which no decoder can
 * be found are ignored.
 *
 * @param ic                media file handle
 * @param type              stream type: video, audio, subtitles, etc.
 * @param wanted_stream_nb  user-requested stream number,
 *                          or -1 for automatic selection
 * @param related_stream    try to find a stream related (eg. in the same
 *                          program) to this one, or -1 if none
 * @param decoder_ret       if non-NULL, returns the decoder for the
 *                          selected stream
 * @param flags             flags; none are currently defined
 * @return  the non-negative stream number in case of success,
 *          AVERROR_STREAM_NOT_FOUND if no stream with the requested type
 *          could be found,
 *          AVERROR_DECODER_NOT_FOUND if streams were found but no decoder
 * @note  If av_find_best_stream returns successfully and decoder_ret is not
 *        NULL, then *decoder_ret is guaranteed to be set to a valid AVCodec.
 */
int av_find_best_stream(AVFormatContext *ic,
                        enum AVMediaType type,
                        int wanted_stream_nb,
                        int related_stream,
                        AVCodec **decoder_ret,
                        int flags);

参数含义:

AVFormatContext *ic,  解码出来的上下文

enum AVMediaType type, 寻找的流的type

int wanted_stream_nb,流的固定索引,这里一般不用给指定传入-1

 int related_stream, 视频 节目组 相关流的索引信息 这里也传入 -1

AVCodec **decoder_ret, 这里是解码器 ,可以返回来,这里不需要,因为解码 和 封装 要独立开来。

 int flags 传入 0

调用如下:

 videoStream = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,-1,-1,NULL, 0);

猜你喜欢

转载自blog.csdn.net/zhangpengzp/article/details/88928503