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如何 ...
随机推荐
- Nginx报错403 forbidden (13: Permission denied)的解决办法
由于开发需要,在本地环境中配置了LNMP环境,使用的是Centos 6.5 的yum安装,安装一切正常,但是由于默认网站文件夹比较奇葩,于是把网站文件用mv命令移动到了新的目录,并相应修改了配置文件, ...
- C#中事件的使用
C#中事件的使用 http://www.cnblogs.com/wayfarer/archive/2004/04/20/6712.html 用一个例子来说明事件的使用. 创建一个简单的类,名为Fil ...
- 在Razor中如何引入命名空间?("import namespace in razor view") 【转】
原文链接 找了半天,原来如此: 在aspx中: <%@ Import Namespace = "Martian.Areas.SFC.Models" %><%@ I ...
- PuzzleGame部分核心算法
#include "mainwindow.h" #include <QGridLayout> #include <QPushButton> #i ...
- Linux下的一个图形管理工具webmin
这个工具其实我在两年前的小白时期还是经常用的,因为那时候对Linux比较陌生在为数server的时候帮了我很多工作,现在周末外面下雨,闲来无事莫名其妙的想起他来. 工具优点:最大特点是他是脚本安装 不 ...
- CSS鼠标响应事件经过、移动、点击示例介绍
本文为大家介绍下CSS 鼠标响应事件:鼠标经过CSS.鼠标移动CSS.鼠标点击CSS以及示例,喜欢的朋友可以参考下 几种鼠标触发CSS事件. 说明: onMouseDown 按下鼠标时触发 onM ...
- 移动WebApp利用Chrome浏览器进行调试
详细的请看这个(HBuilder是我长期使用,而且值得支持的国内前端开发编辑器) http://ask.dcloud.net.cn/article/151 http://ask.dcloud.net. ...
- 开始使用pycharm了
我将python的主力开发工具从eclipse+pydev切换到pycharm社区版了. 选择pycharm 的原因:1. pycharm可以实时按照pep8的规范检查code style和namin ...
- mobile touch事件
touch.js 众所周知,mobile与pc 前端开发的不同中,有一点就是事件的不同,mobile上有touchstart,touchmove,touchend等,而pc上用最多的应该还是我们的cl ...
- R语言练习(二)
op <- par(mfrow = c(2, 2)) #设置画布 p2 <- curve(x^2, 0, 1) #绘制曲线 legend("topleft", inse ...