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如何 ...
随机推荐
- 新创建一个git远程仓库
1.git 服务器项目初始化已经完毕,请把相关的资料和源码上传到git服务器. 2.第一次需要clone,执行命令:git clone git@192.168.10.184:listenbox_mc_ ...
- yourphp搜索代码
HTML代码 <form method="GET" action="index.php?"> //指向地址 <input type=" ...
- SCI/EI期刊投稿 Reply Letter 常用格式总结
SCI/EI期刊投稿Reply Letter常用格式总结 整个论文投稿的过程中,会遇到各种问题,需要我们向主编询问或是回复.下面主要总结了responses to the comme ...
- Redis学习——ae事件处理源码分析
0. 前言 Redis在封装事件的处理采用了Reactor模式,添加了定时事件的处理.Redis处理事件是单进程单线程的,而经典Reator模式对事件是串行处理的.即如果有一个事件阻塞过久的话会导致整 ...
- How to know if file is complete on the server using FTP
This is a very old and well-known problem. There is no way to be absolutely certain a file being wri ...
- jar war
区别:Jar.war.EAR.在文件结构上,三者并没有什么不同,它们都采用zip或jar档案文件压缩格式.但是它们的使用目的有所区别: Jar文件(扩展名为. Jar,JavaApplication ...
- 使用 trash-cli 逃出 rm 命令误删除重要文件的阴影
今天是一个难忘的日子,当时本来想清除我们公司网站cms目录下面一些cdn推送网页后残留的垃圾文件,结果在执行rm -rf conten* 时打成了rm -rf conten *结果就悲剧了.cms目录 ...
- 使用phpize增加php模块
一,phpize的好处 什么时候我们要用phpize呢?我们在安装php时: ./configure --prefix=/apps/product/php --with-config-file-pat ...
- Python之路【第十六篇】Django基础
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- js中自定义事件,使用了jQuery
$(function(){ $('#btn').bind("myClick", function(){ //自定义myClick事件 $('#test').append(" ...