//初始化、注册编解码器
avcodec_init();
av_register_all();
avformat_network_init(); //选取测试文件
char* FileName = "test.rmvb";
AVFormatContext *pFormatCtx;
if(av_open_input_file(&pFormatCtx, FileName, NULL, 0, NULL)!=0)
{
printf("打开文件失败\n");
return -1;
}
dump_format(pFormatCtx, 0, NULL, NULL);//打印相关参数 //寻找流信息,获取格式上下文信息
int err = av_find_stream_info(pFormatCtx);
if(err < 0)
{
printf("查看流信息失败");
return 0;
} //找到所有的音频流和视频流
vector<int> v_video_index;
vector<int> v_audio_index;
for(unsigned int i = 0;i < pFormatCtx->nb_streams; i++)
{
if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
v_audio_index.push_back(i);
}
if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
v_video_index.push_back(i);
}
} AVCodecContext *pAVCodecCtx = pFormatCtx->streams[v_video_index[0]]->codec;
AVCodec *pAVCodec = avcodec_find_decoder(pAVCodecCtx->codec_id);
if(!pAVCodec)
{
printf("Unsupported codec!\n");
return -1;
}
avcodec_open(pAVCodecCtx,pAVCodec); //初始化包
AVPacket packet;
av_init_packet(&packet); int got_picture_ptr=0;
int len;
int width;
int height;
int count=1; //为每帧图像分配内存
AVFrame *picture = avcodec_alloc_frame();
AVFrame *pFrameRGB = avcodec_alloc_frame();
if ( (picture == NULL)||(pFrameRGB == NULL) )
{
printf("avcodec alloc frame failed!\n");
exit (1);
} //处理第一条视频流
while(av_read_frame(pFormatCtx,&packet)>=0)
{
uint8_t* buff = (packet.data);
if(packet.stream_index==v_video_index[0])
{
int iReadTime = 0;
while(packet.size > 0)
{
iReadTime++;
len = avcodec_decode_video2(pAVCodecCtx, picture, &got_picture_ptr, &packet);
if(len==0)
{
got_picture_ptr=0;
}
if(len<0)
{
break;
}
if(got_picture_ptr)
{
#if 1
if(picture->key_frame==1)
{
printf("关键帧\n");
}
else
{
printf("非关键帧\n");
}
#endif #if 1 //判断I B P 帧
if(FF_I_TYPE == picture->pict_type)
{
o<<"I Frame:"<<"packet.dts:"<<packet.dts<<" "<<"packet.pts:"<<packet.pts<<endl;
}
else if(FF_B_TYPE==picture->pict_type)
{
o<<"B Frame:"<<"packet.dts:"<<packet.dts<<" "<<"packet.pts:"<<packet.pts<<endl;
}
else
{
o<<"P Frame:"<<"packet.dts:"<<packet.dts<<" "<<"packet.pts:"<<packet.pts<<endl;
}
#endif width = pAVCodecCtx->width;
height = pAVCodecCtx->height;
if(count)
{
//保存YUV
#if 1
int dstwidth = 800;
int dstheight = 600;
//uint8_t *BufferForSws = (uint8_t*)av_malloc(MAX_FRAME_SIZE);
int size = avpicture_get_size(PIX_FMT_YUV420P, dstwidth, dstheight);
uint8_t *BufferForSwsCtx = (uint8_t*)av_malloc(size);
AVFrame *dstframe = avcodec_alloc_frame();
avpicture_fill((AVPicture*)dstframe, BufferForSwsCtx, PIX_FMT_YUV420P, dstwidth, dstheight);
SwsContext* pSwsCtx = sws_getContext(pAVCodecCtx->width, pAVCodecCtx->height, pAVCodecCtx->pix_fmt, dstwidth, dstheight, PIX_FMT_YUV420P, SWS_BILINEAR,NULL,NULL,NULL);
int ret = sws_scale(pSwsCtx,picture->data, picture->linesize, 0, pAVCodecCtx->height, dstframe->data, dstframe->linesize);
dstframe->width = dstwidth;
dstframe->height = dstheight;
SaveYUV(dstframe, dstwidth, dstheight, count);
av_free(dstframe);
sws_freeContext(pSwsCtx);
#endif
}
count++;
break;
}
packet.size -= len;
packet.data += len;
}
if(packet.data != NULL)
{
av_free_packet(&packet);
}
}
}
av_free(picture);
av_free(pFrameRGB); avcodec_close(pAVCodecCtx);
av_close_input_file(pFormatCtx);

ffmpeg解码视频流的更多相关文章

  1. ffmpeg解码RTSP/TCP视频流H.264(QT界面显示视频画面)

    源码下载地址: http://download.csdn.net/detail/liukang325/9489952 我用的ffmpeg版本为 ffmpeg-2.1.8.tar.bz2 版本低了恐怕有 ...

  2. 在iOS平台使用ffmpeg解码h264视频流(转)

    在iOS平台使用ffmpeg解码h264视频流,有需要的朋友可以参考下. 对于视频文件和rtsp之类的主流视频传输协议,ffmpeg提供avformat_open_input接口,直接将文件路径或UR ...

  3. 在iOS平台使用ffmpeg解码h264视频流

    来源:http://www.aichengxu.com/view/37145 在iOS平台使用ffmpeg解码h264视频流,有需要的朋友可以参考下. 对于视频文件和rtsp之类的主流视频传输协议,f ...

  4. Android开发之《ffmpeg解码mjpeg视频流》

    MJPEG格式和码流分析,MJPEG格式的一些简介 FFmpeg解码USB摄像头MJPEG输出:http://blog.csdn.net/light_in_dark/article/details/5 ...

  5. FFMPEG解码流程

    FFMPEG解码流程:  1. 注册所有容器格式和CODEC: av_register_all()  2. 打开文件: av_open_input_file()  3. 从文件中提取流信息: av_f ...

  6. 使用FFmpeg解码H264-2016.01.14

    使用jni方式调用FFmepg项目中接口,对H264裸码进行解码. 该Demo主要实现从文件中读取H264编码的视频流,然后使用FFmpeg解码,将解码后的码流保存到文件. 工程目录结构如图所示: A ...

  7. FFmpeg解码H264及swscale缩放详解

    本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何解码h264码流,比较详细阐述了其h264码流输入过程,解码原理,解码过程.同时,大部分应用环境下,以原始码流视频大小展示并不是最佳方式,因此 ...

  8. (转)FFMPEG解码流程

    http://www.douban.com/note/228831821/ FFMPEG解码流程:     1. 注册所有容器格式和CODEC: av_register_all()     2. 打开 ...

  9. 【图像处理】FFmpeg解码H264及swscale缩放详解

      http://blog.csdn.net/gubenpeiyuan/article/details/19548019 主题 FFmpeg 本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何 ...

随机推荐

  1. centos 7.0 PHP 5.6.5 安装过程 (php+nginx)

    php网址 http://php.net/downloads.php 首先下载 php-5.6.5.tar.gz [root@localhost src]# wget http://cn2.php.n ...

  2. css3d总结

    3d舞台 设置 perspective(景深): length, 可以设置overflow:hidden 3d舞台下 -> 3d元素容器  设置 transform-style: preserv ...

  3. 【转】快速理解Kafka分布式消息队列框架

     from:http://blog.csdn.net/colorant/article/details/12081909 快速理解Kafka分布式消息队列框架 标签: kafkamessage que ...

  4. [整理]AngularJS学习资源

    https://angular.io/docs/js/latest/(2.0官方网站) http://www.linuxidc.com/Linux/2014-05/102139.htm(Angular ...

  5. hdu4751 Divide Groups

    This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is goi ...

  6. 湖南国庆模拟赛day1 分组

    题目大意:给你一个n个数的数列s,要对这些数进行分组,当有任意两个数在一种方案在一起而在另一种方案中不在一起算是两种不同的方案,一个组的"不和谐程度"为组内数的极差,如果只有一个人 ...

  7. activiti数据库表结构全貌解析

    http://www.jianshu.com/p/e6971e8a8dad 下面本人介绍一些activiti这款开源流程设计引擎的数据库表结构,首先阐述:我们刚开始接触或者使用一个新的东西(技术)时我 ...

  8. ML—随机森林·1

    Introduction to Random forest(Simplified) With increase in computational power, we can now choose al ...

  9. mysql-mysql悲观锁和乐观锁

    1.mysql的四种事务隔离级别 I. 对于同时运行多个事务,当这些事务访问数据库中的相同数据时,如果没有采取必要的隔离机制,就会导致各种并发问题. (1)脏读: 对于两个事物 T1, T2, T1 ...

  10. js时间格式化(yy年MM月dd日 hh:mm)

    //时间格式化 Date.prototype.format = function (format) { var o = { "M+": this.getMonth() + 1, / ...