ffmpeg从AVFrame取出yuv数据到保存到char*中

 
很多人一直不知道怎么利用ffmpeg从AVFrame取出yuv数据到保存到char*中,下面代码将yuv420p和yuv422p的数据取出并保存到char*buf中。
其他格式可以自己去扩展,前提先看戏yuv的各种格式,yuv的各种格式链接:数据格式分析
 
先确保视频格式sws_getContext()转换后是YUV格式:
     out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));//分配AVFrame所需内存
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);//填充AVFrame img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
那么在后续的视频数据处理时,可以把YUV视频格式数据进行存储:
 //如果是视频
else if (pstream_info[i].dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
{
int new_videosize = pkt.size;
int video_decode_size = avpicture_get_size(pstream_info->dec_ctx->pix_fmt, Zoom_Width,Zoom_Height);
uint8_t * video_decode_buf =( uint8_t *)calloc(,video_decode_size * * sizeof(char)); //最大分配的空间,能满足yuv的各种格式 // Decode video frame
avcodec_decode_video2(pstream_info->dec_ctx, pDecodeFrame, &frameFinished,&pkt);
if(frameFinished)
{
if (pstream_info->dec_ctx->pix_fmt == AV_PIX_FMT_YUV420P) //如果是yuv420p的
{
for(i = ; i < pstream_info->dec_ctx->height; i++)
{
memcpy(video_decode_buf+pstream_info->dec_ctx->width*i,
pDecodeFrame->data[]+pDecodeFrame->linesize[]*i,
pstream_info->dec_ctx->width);
}
for(j = ; j < pstream_info->dec_ctx->height/; j++)
{
memcpy(video_decode_buf+pstream_info->dec_ctx->width*i+pstream_info->dec_ctx->width/*j,
pDecodeFrame->data[]+pDecodeFrame->linesize[]*j,
pstream_info->dec_ctx->width/);
}
for(k =; k < pstream_info->dec_ctx->height/; k++)
{
memcpy(video_decode_buf+pstream_info->dec_ctx->width*i+pstream_info->dec_ctx->width/*j+pstream_info->dec_ctx->width/*k,
pDecodeFrame->data[]+pDecodeFrame->linesize[]*k,
pstream_info->dec_ctx->width/);
}
}
else if (pstream_info->dec_ctx->pix_fmt == AV_PIX_FMT_YUV422P)//如果是yuv422p的
{
for(i = ; i < pstream_info->dec_ctx->height; i++)
{
memcpy(video_decode_buf+pstream_info->dec_ctx->width*i,
pDecodeFrame->data[]+pDecodeFrame->linesize[]*i,
pstream_info->dec_ctx->width);
}
for(j = ; j < pstream_info->dec_ctx->height; j++)
{
memcpy(video_decode_buf+pstream_info->dec_ctx->width*i+pstream_info->dec_ctx->width/*j,
pDecodeFrame->data[]+pDecodeFrame->linesize[]*j,
pstream_info->dec_ctx->width/);
}
for(k =; k < pstream_info->dec_ctx->height; k++)
{
memcpy(video_decode_buf+pstream_info->dec_ctx->width*i+pstream_info->dec_ctx->width/*j+pstream_info->dec_ctx->width/*k,
pDecodeFrame->data[]+pDecodeFrame->linesize[]*k,
pstream_info->dec_ctx->width/);
}
}
else
{
//可扩展
}
video_decode_size = avpicture_get_size(pstream_info->dec_ctx->pix_fmt, pstream_info->dec_ctx->width,pstream_info->dec_ctx->height);
new_videosize = video_decode_size; //缩放或格式转换
if (pstream_info->dec_ctx->width != Zoom_Width ||
pstream_info->dec_ctx->height != Zoom_Height ||
pstream_info->dec_ctx->pix_fmt != Zoom_pix_fmt)
{
new_videosize = VideoScaleYuvZoom(Is_flip,pstream_info->dec_ctx->width ,pstream_info->dec_ctx->height,(int)pstream_info->dec_ctx->pix_fmt,
Zoom_Width,Zoom_Height,Zoom_pix_fmt,video_decode_buf);
}
//这里可以取出数据
frame_info->stream_idx = pstream_info->stream_idx;
//frame_info->pts = pDecodeFrame->pkt_pts * 1000 * av_q2d(pstream_info->stream->time_base); //转化成毫秒
frame_info->pts = pDecodeFrame->pkt_pts;
frame_info->timebase_den = pstream_info->stream->time_base.den;
frame_info->timebase_num = pstream_info->stream->time_base.num;
frame_info->bufsize = new_videosize;
memcpy(frame_info->buf,video_decode_buf,new_videosize);
}
else
{
//缓存
frame_info->stream_idx = pstream_info->stream_idx;
frame_info->pts = ;
frame_info->timebase_den = ;
frame_info->timebase_num = ;
frame_info->bufsize = ;
memset(frame_info->buf,,MAX_FRAME_SIZE);
}
if (video_decode_buf)
{
free(video_decode_buf);
video_decode_buf = NULL;
}
video_decode_size = ;
}

也可以把YUV数据进行存储为PPM格式(Linux系统下的图片格式):

 //如果是视频
else if (pstream_info[i].dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
{
// Decode video frame
avcodec_decode_video2(pstream_info->dec_ctx, pDecodeFrame, &frameFinished,&pkt);
if(frameFinished)
{
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, , pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
if((++k<=) && (k%==)) {
SaveFrame(pFrameYUV, pCodecCtx->width, pCodecCtx->height, k);
}
}
} void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame)
{
FILE *pFile;
char szFilename[];
int y; SDL_Log("%d * %d", width, height);
// Open file
sprintf(szFilename, "frame/frame%d.ppm", iFrame);
pFile=fopen(szFilename, "wb");
if(pFile==NULL)
return; // Write header
fprintf(pFile, "P6\n%d %d\n255\n", width, height); // Write pixel data
for(y=; y<height; y++) {
fwrite(pFrame->data[]+y*pFrame->linesize[], , width*, pFile);
} // Close file
fclose(pFile);
}

ffmpeg从AVFrame取出yuv数据到保存到char*中的更多相关文章

  1. python 数据如何保存到excel中--xlwt

    第一步:下载xlwt 首先要下载xlwt,(前提是你已经安装好了Python) 下载地址:  https://pypi.python.org/pypi/xlwt/   下载第二个   第二步:安装xl ...

  2. (转) 从ffmpeg中提取出YUV数据

    有时需要从ffmpeg中提取出YUV数据用作预览,另存什么的. ffmpeg是先解码成YUV, 再以这个YUV作为输入进行编码,所以YUV数据有两种:  解码后的YUV数据, 以及  编码重建的YUV ...

  3. 1.scrapy爬取的数据保存到es中

    先建立es的mapping,也就是建立在es中建立一个空的Index,代码如下:执行后就会在es建lagou 这个index.     from datetime import datetime fr ...

  4. c# 抓取和解析网页,并将table数据保存到datatable中(其他格式也可以,自己去修改)

    使用HtmlAgilityPack 基础请参考这篇博客:https://www.cnblogs.com/fishyues/p/10232822.html 下面是根据抓取的页面string 来解析并保存 ...

  5. Redis使用场景一,查询出的数据保存到Redis中,下次查询的时候直接从Redis中拿到数据。不用和数据库进行交互。

    maven使用: <!--redis jar包--> <dependency> <groupId>redis.clients</groupId> < ...

  6. Android把图片保存到SQLite中

    1.bitmap保存到SQLite 中 数据格式:Blob db.execSQL("Create table " + TABLE_NAME + "( _id INTEGE ...

  7. 【redis,1】java操作redis: 将string、list、map、自己定义的对象保存到redis中

    一.操作string .list .map 对象 1.引入jar: jedis-2.1.0.jar   2.代码 /**      * @param args      */     public s ...

  8. 将数字n转换为字符串并保存到s中

    将数字n转换为字符串并保存到s中 参考 C程序设计语言 #include <stdio.h> #include <string.h> //reverse函数: 倒置字符串s中各 ...

  9. Flask实战第43天:把图片验证码和短信验证码保存到memcached中

    前面我们已经获取到图片验证码和短信验证码,但是我们还没有把它们保存起来.同样的,我们和之前的邮箱验证码一样,保存到memcached中 编辑commom.vews.py .. from utils i ...

随机推荐

  1. Synergy 鼠标和键盘共享软件

    http://symless.com/nightly Synergy 正可以让你的多台电脑共享一套键鼠,甚至还可以共享剪贴板,如同一机多屏,并跨平台支持 .

  2. 临时存存储页面上的数据---js中的cookie

    实现的效果: 当点击某个按钮的时候,实现点击A的同时,弹出B的注册div,使填写在B信息数据保存下来,点击B的确定按钮,B消失,A的图标往后移动一格,原来的位置为图标C,点击C可以弹出来一个链接的页面 ...

  3. linux下对date和timestamp的互转

    1. date 到 timestamp: $ date -d '2009-12-01 23:20' +%s 12596808002. timestamp 到 date$ date -d '1970-0 ...

  4. 《奥威Power-BI销售计划填报 》精彩回顾

    我们经常遇到这样的问题:业务单据是来自ERP系统,销售计划是EXCEL做的,想把两者整合在一起做分析,怎么办? 单据大,导出EXCEL太费劲,也很难分析到历史数据,但又不能动ERP系统 (自己也不会改 ...

  5. jQuery源代码学习之八——jQuery属性操作模块

    一.jQuery属性模块整体介绍 jQuery的属性操作模块分四个部分:html属性操作,dom属性操作,类样式操作,和值操作. html属性操作(setAttribute/getAttribute) ...

  6. 去除行内(inline/inline-block)元素之间的间距

    先展示一下,行内元素之间存在间距,实例代码如下: <style> div { color: #fff; padding: 25px 50px; } .inline-f00 { displa ...

  7. 前向后瞻正则表达式及其JS例子

    定义 x(?=y) 匹配'x'仅仅当'x'后面跟着'y'.这种叫做正向肯定查找. 比如,/Jack(?=Sprat)/会匹配到'Jack'仅仅当它后面跟着'Sprat'./Jack(?=Sprat|F ...

  8. spring MVC配置详解

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  9. composer--------初体验,如何安装,如何下载

    最近PHP里面比较火的一款框架laravel,想学一下看下这个框架到底哪里好.这款框架的中文官网激励推荐composer,没办法就去学了一些composer.结果整了半天,还不如看一段短视频学的容易. ...

  10. webservice调用服务端数据时给soapenv:Envelope 添加自定义的命名空间

    最近做第三方接口,服务端需要 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/&qu ...