ffmpeg之如何获得一个媒体文件的AVStream,并从该基础上获得AVCodec,AVCodecContext

#include <stdio.h>
#include <stdlib.h>
extern "C"{
#include <libavcodec\avcodec.h>
#include <libavutil\avutil.h>
#include <libavformat\avformat.h>


}
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avformat.lib")


int main(int argc, char* argv[])
{
// 设置日志的标准, 高于该标准的将不会被显示
av_log_set_level(AV_LOG_DEBUG);
// 注册所有的编解码器
av_register_all();
AVFormatContext* pCtx = NULL;

if (avformat_open_input(&pCtx, "D:/test.mp4", NULL, NULL) != 0)
{
av_log(NULL, AV_LOG_ERROR, "打开输入文件失败了!\n");
goto end;
}
if (avformat_find_stream_info(pCtx, NULL) < 0)
{
av_log(&pCtx, AV_LOG_ERROR, "查找失败了");
goto end;
}
int audioIndex = -1;
int videoIndex = -1;
for (int i = 0; i < pCtx->nb_streams; ++i)
{
if (pCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoIndex = i;
}
if (pCtx->streams[i]->codec->codec_type = AVMEDIA_TYPE_AUDIO)
{
audioIndex = i;
}
}
end:
av_log(NULL, AV_LOG_ERROR, "audio : %d\n",audioIndex);
av_log(NULL, AV_LOG_ERROR, "video : %d\n", videoIndex);


AVCodecContext* pCodecContext =  pCtx->streams[videoIndex]->codec;
if (!pCodecContext) av_log(NULL, AV_LOG_ERROR, "video : %s\n", "获得编解码器环境失败!");
AVCodec* pCodec = avcodec_find_decoder(pCodecContext->codec_id);
if (!pCodec) av_log(NULL, AV_LOG_ERROR, "video : %s\n","获得编解码器失败!");
av_log(NULL, AV_LOG_ERROR, "audio : %s\n", " ");
system("pause");
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值