实战FFmpeg + OpenGLES--iOS平台上视频解码和播放
一个星期的努力终于搞定了视频的播放,利用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
实战FFmpeg + OpenGLES--iOS平台上视频解码和播放的更多相关文章
- 使用Vitamio开发iOS平台上的万能播放器
迅速了解 Vitamio是干什么的?看官方怎么说: "Vitamio SDK for iOS是Yixia Ltd官方推出的 iOS 平台上使用的软件开发工具包(SDK),为iOS开发人员提供 ...
- iOS 平台上常见的安装包有三种,deb、ipa 和 pxl
前言:目前 iOS 平台上常见的安装包有三种,deb.ipa 和 pxl. 其中 deb 格式是 Debian 系统(包含 Debian 和 Ubuntu )专属安装包格式,配合 APT 软件管理系统 ...
- 实战AudioToolbox--在iOS平台上播放音频
上午看了关于AudioToolbox.framework相关的资料,结合网上的资料对AudioToolbox的基本使用有了整体上的认识,上一篇文章 笔谈AudioToolbox(一) 中提到使用Aud ...
- unity 在安卓个IOS平台上 同一个按钮 点击后实现不同的功能
#if UNITY_IOS UIEventListener.Get(mSprites["Recharge"].gameObject).onClick = OnIOSRecharge ...
- mui搜索框在ios平台上点击多次才弹出键盘的解决方法
今天使用Hbuilder调试手机端时,发现搜索框在安卓系统下,点击一次就可以弹出键盘 但是在iso下非常的不规律,要点击多次 代码实现如下: <div class="mui-input ...
- 实战FFmpeg--编译iOS平台使用的FFmpeg库(支持arm64的FFmpeg2.6.2)
编译环境:Mac OS X 10.10.2 ,Xcode 6.3 iOS SDK 8.3 FFmpeg库的下载地址是 http://www.ffmpeg.org/releases/ . ...
- iOS平台在ffmpeg中使用librtmp
转载请注明出处:http://www.cnblogs.com/fpzeng/p/3202344.html 系统版本:OS X 10.8 一.在iOS平台上交叉编译librtmp librtmp lin ...
- (译)cocos2d-x跨android&ios平台开发入门教程
免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切勿进行商业传播.同时,转载时不要移除本申明.如产生任何纠纷,均与本博客所有人.发表该翻译稿之人无任何关系.谢谢合作 ...
- U3D中IOS平台泛型方法尽少使用
U3D的IOS最小运行库use micro mscorlib是不包含泛型反射方法的,如FieldType.GetGenericArguments方法.所以尽量少用List而直接使用array. 另外请 ...
随机推荐
- 【C++】C++中的异常解析
异常是程序在执行期间产生的问题.C++ 异常是指在程序运行时发生的特殊情况,比如尝试除以零的操作. 异常提供了一种转移程序控制权的方式.C++ 异常处理涉及到三个关键字:try.catch.throw ...
- [原]使用global mapper 修改影像数据DOM的投影变换(将数据转换成osgearth支持的投影)
osgearth默认使用的投影基准面为: Geographic(Latitude/Longitude)的 WGS84 有这样一份数据需要修改: 1.在菜单栏种选择“工具”---->“配置” 2. ...
- FastStone Capture 9.3 强烈推荐,常用功能介绍
http://www.dayanzai.me/faststone-capture.html 经典优秀屏幕截图录像工具 FastStone Capture 9.3 绿色汉化中文版 下载 官网: ht ...
- SQL Server 2008 R2 安装 下载
[参考]https://www.aiweibk.com/6697.html winrm 服务未启动,需要先配置.以管理员身份启动 cmd,执行 winrm quickconfig 命令. 微信截图_2 ...
- SDN实验---Ryu的应用开发(四)北向接口RESTAPI
一:推文 软件定义网络基础---REST API概述 软件定义网络基础---REST API的设计规范 二:掌握Ryu基本RESTAPI使用方法 (一)Ryu的RESTAPI (二) REST应用样例 ...
- iOS开发 判定某个时间是否属于这个时间段
- (BOOL)isBetweenDate { //设置的是中国时间 NSString *startTime=@"13:01"; NSString *expireTime=@&qu ...
- 【数据集】FDDB-Face Detection Data Set and Benchmark
前言 参考 1. FDDB官网: 完
- angular4.x实现一个全选,反选,外加从下一页返回上一页,选中上一次的操作记录
productMap:any = new Map<string, string>(); //定义一个map的数据模型 //只要操作这个checkbox 那么只管把数据全部勾起了就行了 刷新 ...
- HTML5自定义select标签样式的方法
HTML5自定义select标签样式的方法 -webkit-appearance: none; 这个东西可以隐藏箭头 不过手机端就直接 设置透明度为0就行了(如果这种做法比前面个要麻烦点 毕竟还要对他 ...
- 单点登录(SSO)解决方案介绍
一.单点登录的介绍 单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用 ...