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

 
很多人一直不知道怎么利用ffmpeg从AVFrame取出yuv数据到保存到char*中,下面代码将yuv420p和yuv422p的数据取出并保存到char*buf中。
其他格式可以自己去扩展,前提先看戏yuv的各种格式,yuv的各种格式链接:数据格式分析
 
先确保视频格式sws_getContext()转换后是YUV格式:
  1. out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));//分配AVFrame所需内存
  2. avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);//填充AVFrame
  3.  
  4. img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
  5. pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
那么在后续的视频数据处理时,可以把YUV视频格式数据进行存储:
  1. //如果是视频
  2. else if (pstream_info[i].dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
  3. {
  4. int new_videosize = pkt.size;
  5. int video_decode_size = avpicture_get_size(pstream_info->dec_ctx->pix_fmt, Zoom_Width,Zoom_Height);
  6. uint8_t * video_decode_buf =( uint8_t *)calloc(,video_decode_size * * sizeof(char)); //最大分配的空间,能满足yuv的各种格式
  7.  
  8. // Decode video frame
  9. avcodec_decode_video2(pstream_info->dec_ctx, pDecodeFrame, &frameFinished,&pkt);
  10. if(frameFinished)
  11. {
  12. if (pstream_info->dec_ctx->pix_fmt == AV_PIX_FMT_YUV420P) //如果是yuv420p的
  13. {
  14. for(i = ; i < pstream_info->dec_ctx->height; i++)
  15. {
  16. memcpy(video_decode_buf+pstream_info->dec_ctx->width*i,
  17. pDecodeFrame->data[]+pDecodeFrame->linesize[]*i,
  18. pstream_info->dec_ctx->width);
  19. }
  20. for(j = ; j < pstream_info->dec_ctx->height/; j++)
  21. {
  22. memcpy(video_decode_buf+pstream_info->dec_ctx->width*i+pstream_info->dec_ctx->width/*j,
  23. pDecodeFrame->data[]+pDecodeFrame->linesize[]*j,
  24. pstream_info->dec_ctx->width/);
  25. }
  26. for(k =; k < pstream_info->dec_ctx->height/; k++)
  27. {
  28. memcpy(video_decode_buf+pstream_info->dec_ctx->width*i+pstream_info->dec_ctx->width/*j+pstream_info->dec_ctx->width/*k,
  29. pDecodeFrame->data[]+pDecodeFrame->linesize[]*k,
  30. pstream_info->dec_ctx->width/);
  31. }
  32. }
  33. else if (pstream_info->dec_ctx->pix_fmt == AV_PIX_FMT_YUV422P)//如果是yuv422p的
  34. {
  35. for(i = ; i < pstream_info->dec_ctx->height; i++)
  36. {
  37. memcpy(video_decode_buf+pstream_info->dec_ctx->width*i,
  38. pDecodeFrame->data[]+pDecodeFrame->linesize[]*i,
  39. pstream_info->dec_ctx->width);
  40. }
  41. for(j = ; j < pstream_info->dec_ctx->height; j++)
  42. {
  43. memcpy(video_decode_buf+pstream_info->dec_ctx->width*i+pstream_info->dec_ctx->width/*j,
  44. pDecodeFrame->data[]+pDecodeFrame->linesize[]*j,
  45. pstream_info->dec_ctx->width/);
  46. }
  47. for(k =; k < pstream_info->dec_ctx->height; k++)
  48. {
  49. memcpy(video_decode_buf+pstream_info->dec_ctx->width*i+pstream_info->dec_ctx->width/*j+pstream_info->dec_ctx->width/*k,
  50. pDecodeFrame->data[]+pDecodeFrame->linesize[]*k,
  51. pstream_info->dec_ctx->width/);
  52. }
  53. }
  54. else
  55. {
  56. //可扩展
  57. }
  58. video_decode_size = avpicture_get_size(pstream_info->dec_ctx->pix_fmt, pstream_info->dec_ctx->width,pstream_info->dec_ctx->height);
  59. new_videosize = video_decode_size;
  60.  
  61. //缩放或格式转换
  62. if (pstream_info->dec_ctx->width != Zoom_Width ||
  63. pstream_info->dec_ctx->height != Zoom_Height ||
  64. pstream_info->dec_ctx->pix_fmt != Zoom_pix_fmt)
  65. {
  66. new_videosize = VideoScaleYuvZoom(Is_flip,pstream_info->dec_ctx->width ,pstream_info->dec_ctx->height,(int)pstream_info->dec_ctx->pix_fmt,
  67. Zoom_Width,Zoom_Height,Zoom_pix_fmt,video_decode_buf);
  68. }
  69. //这里可以取出数据
  70. frame_info->stream_idx = pstream_info->stream_idx;
  71. //frame_info->pts = pDecodeFrame->pkt_pts * 1000 * av_q2d(pstream_info->stream->time_base); //转化成毫秒
  72. frame_info->pts = pDecodeFrame->pkt_pts;
  73. frame_info->timebase_den = pstream_info->stream->time_base.den;
  74. frame_info->timebase_num = pstream_info->stream->time_base.num;
  75. frame_info->bufsize = new_videosize;
  76. memcpy(frame_info->buf,video_decode_buf,new_videosize);
  77. }
  78. else
  79. {
  80. //缓存
  81. frame_info->stream_idx = pstream_info->stream_idx;
  82. frame_info->pts = ;
  83. frame_info->timebase_den = ;
  84. frame_info->timebase_num = ;
  85. frame_info->bufsize = ;
  86. memset(frame_info->buf,,MAX_FRAME_SIZE);
  87. }
  88. if (video_decode_buf)
  89. {
  90. free(video_decode_buf);
  91. video_decode_buf = NULL;
  92. }
  93. video_decode_size = ;
  94. }

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

  1. //如果是视频
  2. else if (pstream_info[i].dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
  3. {
  4. // Decode video frame
  5. avcodec_decode_video2(pstream_info->dec_ctx, pDecodeFrame, &frameFinished,&pkt);
  6. if(frameFinished)
  7. {
  8. sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, , pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
  9. if((++k<=) && (k%==)) {
  10. SaveFrame(pFrameYUV, pCodecCtx->width, pCodecCtx->height, k);
  11. }
  12. }
  13. }
  14.  
  15. void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame)
  16. {
  17. FILE *pFile;
  18. char szFilename[];
  19. int y;
  20.  
  21. SDL_Log("%d * %d", width, height);
  22. // Open file
  23. sprintf(szFilename, "frame/frame%d.ppm", iFrame);
  24. pFile=fopen(szFilename, "wb");
  25. if(pFile==NULL)
  26. return;
  27.  
  28. // Write header
  29. fprintf(pFile, "P6\n%d %d\n255\n", width, height);
  30.  
  31. // Write pixel data
  32. for(y=; y<height; y++) {
  33. fwrite(pFrame->data[]+y*pFrame->linesize[], , width*, pFile);
  34. }
  35.  
  36. // Close file
  37. fclose(pFile);
  38. }

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. phpcms流程

    phpcms流程1: 安装 将下载好的文件放到www目录下 地址栏中输入 http://localhost/phpcms/install_package/install 打开安装页面 进行安装即可. ...

  2. yii框架分页

  3. 关于echarts的疑问

    echarts-例子--待解决:模拟迁徙里面的 var planePath = 'path://M1705.06,1318.313v-89.254l-319.9-221.799l0.073-208.0 ...

  4. TCP 长连接与短连接的区别

    TCP连接 当网络通信时采用TCP协议时,在真正的读写操作之前,server与client之间必须建立一个连接,当读写操作完成后,双方不再需要这个连接时它们可以释放这个连接,连接的建立是需要三次握手的 ...

  5. Android-Activity使用(1)

    一.添加 activity类  Aty1 继承Activity package activitylc.eoe.cn.l002activieylc; import android.app.Activit ...

  6. 两种方法解决tomcat的 Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"]

    出现这种原因主要是8080端口被占用了. 解决1: 打开任务管理器看看里面有没有javaw的线程,把它关了再重新启动tomcat看看. 解决2: 修改tomcat /conf /server.xml ...

  7. 服务器IP地址后修改SQL Server配置

    1. 修改TCP/IP 属性的IP 地址 修改该实例的协议.修改TCP/IP协议的属性,将IP地址更新为当前的最新IP 地址.然后重启该实例. 2.查看全部侦听再检查SQL Server 实例的TCP ...

  8. AJAX-----14HTML5中新增的API---files

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. swift uiview弹出动画

    UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.0, ...

  10. wex5 教程 之 图文讲解 bind-css和bind-sytle的异同

    wex5作为网页开发利器,在前台UI数据交互设计中大量使用了绑定技术,即官方视频教学中也提到了KO,实质是数据绑定与追踪.在前台组件的属性中,为我们提供了两个重要的样式绑定属性,bind-css和bi ...