ffmpeg解码视频流
//初始化、注册编解码器
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解码视频流的更多相关文章
- ffmpeg解码RTSP/TCP视频流H.264(QT界面显示视频画面)
源码下载地址: http://download.csdn.net/detail/liukang325/9489952 我用的ffmpeg版本为 ffmpeg-2.1.8.tar.bz2 版本低了恐怕有 ...
- 在iOS平台使用ffmpeg解码h264视频流(转)
在iOS平台使用ffmpeg解码h264视频流,有需要的朋友可以参考下. 对于视频文件和rtsp之类的主流视频传输协议,ffmpeg提供avformat_open_input接口,直接将文件路径或UR ...
- 在iOS平台使用ffmpeg解码h264视频流
来源:http://www.aichengxu.com/view/37145 在iOS平台使用ffmpeg解码h264视频流,有需要的朋友可以参考下. 对于视频文件和rtsp之类的主流视频传输协议,f ...
- Android开发之《ffmpeg解码mjpeg视频流》
MJPEG格式和码流分析,MJPEG格式的一些简介 FFmpeg解码USB摄像头MJPEG输出:http://blog.csdn.net/light_in_dark/article/details/5 ...
- FFMPEG解码流程
FFMPEG解码流程: 1. 注册所有容器格式和CODEC: av_register_all() 2. 打开文件: av_open_input_file() 3. 从文件中提取流信息: av_f ...
- 使用FFmpeg解码H264-2016.01.14
使用jni方式调用FFmepg项目中接口,对H264裸码进行解码. 该Demo主要实现从文件中读取H264编码的视频流,然后使用FFmpeg解码,将解码后的码流保存到文件. 工程目录结构如图所示: A ...
- FFmpeg解码H264及swscale缩放详解
本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何解码h264码流,比较详细阐述了其h264码流输入过程,解码原理,解码过程.同时,大部分应用环境下,以原始码流视频大小展示并不是最佳方式,因此 ...
- (转)FFMPEG解码流程
http://www.douban.com/note/228831821/ FFMPEG解码流程: 1. 注册所有容器格式和CODEC: av_register_all() 2. 打开 ...
- 【图像处理】FFmpeg解码H264及swscale缩放详解
http://blog.csdn.net/gubenpeiyuan/article/details/19548019 主题 FFmpeg 本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何 ...
随机推荐
- ElasticSearch之二——集群
ElasticSearch 集群 首先看下ElasticSearch(ES)的架构: 术语解释: cluster:代表一个集群,集群中有多个节点,其中有一个master节点,master通过选举自动产 ...
- AndroidManiFast 字段意义
每个Activity都要在本文件中注册. <Activity>下的<Intent-filter>中. 两个字段的意思是: <action android:name=&qu ...
- JS网页加载进度条
参考:http://www.cnblogs.com/timy/archive/2011/12/07/2279200.html
- SQL Server2008 MERGE指令用法
参考资料: 百度百科-MERGE
- Storm与Spark Streaming比较
前言spark与hadoop的比较我就不多说了,除了对硬件的要求稍高,spark应该是完胜hadoop(Map/Reduce)的.storm与spark都可以用于流计算,但storm对应的场景是毫秒级 ...
- c# List AddRange
https://msdn.microsoft.com/zh-cn/library/z883w3dc(v=vs.80).aspx List 中会保留集合中元素的顺序. 如果新的 Count(当前 Cou ...
- seajs的那点事(很坑的事),和本白的一点事(更坑的事)
在开始之前,偶先吐槽加逗比一下,2天前,CCAV的本白和百度的菊花成功潜入到了携程大楼 然后在没有找到他们运维的情况下,四处乱逛,企图把他们的服务器给root一下,然后再瞎逛之后到了一个很神奇的地方 ...
- Nginx 502 bad gateway问题的解决方法
Nginx 502 Bad Gateway的含义是请求的PHP-CGI已经执行,但是由于某种原因(一般是读取资源的问题)没有执行完毕而导致PHP-CGI进程终止,一般来说Nginx 502 Bad G ...
- ECshop安装及报错解决方案总结
一.安装ECshop ECShop是一款B2C独立网店系统 ,适合企业及个人快速构建个性化网上商店.系统是基于PHP语言及MYSQL数据库构架开发的跨平台开源程序.2006年3月推出以来1.0版以来, ...
- 【PHP面向对象(OOP)编程入门教程】20.PHP5接口技术(interface)
PHP与大多数面向对象编程语言一样,不支持多重继承.也就是说每个类只能继承一个父类.为了解决这个问题,PHP引入了接口,接口的思想是指定了一个实现了该接口的类必须实现的一系列方法.接口是一种特殊的抽象 ...