一个星期的努力终于搞定了视频的播放,利用FFmpeg解码视频,将解码的数据通过OpenGLES渲染播放。搞清楚了自己想知道的和完成了自己的学习计划,有点小兴奋。明天就是“五一”,放假三天,更开心啦。

  本文实现视频文件的播放是在自己之前写的文章实战FFmpeg--iOS平台使用FFmpeg将视频文件转换为YUV文件 、 实战OpenGLES--iOS平台使用OpenGLES渲染YUV图片 的基础上改进合成来完成的。不多种解释,直接上代码,清晰明了。

NSString *path = [[NSBundle mainBundle] pathForResource:@"nwn" ofType:@"mp4"];
const char *filepath= [path UTF8String]; av_register_all();
avformat_network_init();
pFormatCtx = avformat_alloc_context();
if(avformat_open_input(&pFormatCtx,filepath,NULL,NULL)!=){
printf("Couldn't open input stream.\n");
exit();
} /*
* av_find_stream_info():ffmpeg版本更新后没有这个函数了,用avformat_find_stream_info这个函数,可以自己看一下版本更新改动
*/
// if(av_find_stream_info(pFormatCtx)<0)
if(avformat_find_stream_info(pFormatCtx, NULL) < )
{
printf("Couldn't find stream information.\n");
exit();
} videoindex=-;
for(int i=; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
{
videoindex=i;
break;
}
if(videoindex==-)
{
printf("Didn't find a video stream.\n");
exit();
}
pCodecCtx=pFormatCtx->streams[videoindex]->codec;
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
{
printf("Codec not found.\n");
exit();
}
/*
* avcodec_open():ffmpeg版本更新后没有这个函数了,用avformat_find_stream_info这个函数,可以自己看一下版本更新改动
* avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
*/
// if(avcodec_open(pCodecCtx, pCodec)<0)
if(avcodec_open2(pCodecCtx, pCodec, NULL)<)
{
printf("Could not open codec.\n");
exit();
}
AVFrame *pFrame,*pFrameYUV;
pFrame=avcodec_alloc_frame();
pFrameYUV=avcodec_alloc_frame();
uint8_t *out_buffer;
out_buffer=new uint8_t[avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)];
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height); int ret, got_picture;
int y_size = pCodecCtx->width * pCodecCtx->height; AVPacket *packet=(AVPacket *)malloc(sizeof(AVPacket));
av_new_packet(packet, y_size); printf("video infomation:\n");
av_dump_format(pFormatCtx,,filepath,); while(av_read_frame(pFormatCtx, packet)>=)
{
if(packet->stream_index==videoindex)
{
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if(ret < )
{
printf("Decode Error.\n");
exit();
}
if(got_picture)
{
char *buf = (char *)malloc(pFrame->width * pFrame->height * / ); AVPicture *pict;
int w, h;
char *y, *u, *v;
pict = (AVPicture *)pFrame;//这里的frame就是解码出来的AVFrame
w = pFrame->width;
h = pFrame->height;
y = buf;
u = y + w * h;
v = u + w * h / ; for (int i=; i<h; i++)
memcpy(y + w * i, pict->data[] + pict->linesize[] * i, w);
for (int i=; i<h/; i++)
memcpy(u + w / * i, pict->data[] + pict->linesize[] * i, w / );
for (int i=; i<h/; i++)
memcpy(v + w / * i, pict->data[] + pict->linesize[] * i, w / ); [myview setVideoSize:pFrame->width height:pFrame->height];
[myview displayYUV420pData:buf width:pFrame->width height:pFrame->height]; //利用OpenGLES渲染YUV
free(buf);
}
}
av_free_packet(packet);
}
delete[] out_buffer;
av_free(pFrameYUV);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);

  本周的学习任务完成啦,写完这篇博客就下班回家,哈哈。五一长假,我要休息下,列出自己下一步的学习计划。下周我想做的是本地音频文件解码播放,以及本地视频文件中音频与视频同步播放。现在所做的都是本地音视频文件的处理,因为本地文件处理起来方便一些,这也让自己学起来也快速一些。关于网络音视频实时流的同步与播放,这个涉及的内容就要复杂一些了,有效学习+时间规划,我肯定能玩透播放器的开发。

  本文完整工程 FFmpegDecode_OpenGLESRenderYUV 的下载地址为:http://pan.baidu.com/s/1dDvpECh

最新文章

  1. 为Office365增加密码过期自动提醒功能
  2. xamarin.forms 版本自动更新(针对android)
  3. CLR via C#深解笔记二 - 类型设计
  4. Qt根据汉字生成位图,可连续调用,生成的位图不会有杂点
  5. Doragon Kuesuto 1.0
  6. Android好用且常用的插件及工具
  7. 如何用拉姆达表达式(Lambda Expressions) 书写左链接查询
  8. spring官方文档中文版
  9. UVA 12378 Ball Blasting Game 【Manacher回文串】
  10. WindowsForm 打印
  11. logback与Spring、SpringMVC结合使用教程(转) logback good
  12. Masonry1.0.2 源码解析
  13. [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
  14. 使用GDB命令行调试器调试C/C++程序
  15. Net包管理NuGet(2)nuget包的生成方法
  16. 996.ICU
  17. 1.Zabbix配置[仅环境搭建]
  18. swift protocol的几种形式
  19. 第二十篇:不为客户连接创建子进程的并发回射服务器(poll实现)
  20. [AtCoder ARC101D/ABC107D] Median of Medians

热门文章

  1. GEOS/GDAL 交叉编译ARM64-linux版本
  2. JAVA中使用LDAP登录的三种方式
  3. copy函数是有返回值的!
  4. webService和Restful
  5. ASP.NET LinqDataSource数据绑定后,遇到[MissingMethodException: 没有为该对象定义无参数的构造函数。]问题。
  6. Classic BAdi and New BAdi
  7. DevExpress XtraReport - 动态加载报表布局模板
  8. mysql批量更新数据,循环select记录然后更新某一字段
  9. Servlet3.0对异步处理的支持
  10. ECharts 避免变窄