热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ffmpeg解码视频存为BMP文件

ffmpeg解码视频存为BMP文件分类:ffmpeg2011-07-2812:138人阅读评论(0)收藏举报viewplain#include#

ffmpeg解码视频存为BMP文件

分类: ffmpeg2011-07-28 12:13 8人阅读 评论(0) 收藏 举报
view plain
  1. #include   
  2.  #include   
  3. #include   
  4. #include   
  5. #pragma once    
  6.   
  7.  #ifdef __cplusplus  
  8. extern "C" {  
  9. #endif  
  10. #include   
  11. #include   
  12. #include   
  13.   
  14.   
  15. #ifdef __cplusplus  
  16. }  
  17. #endif  
  18.   
  19. //定义BMP文件头  
  20.  #ifndef _WINGDI_   
  21. #define _WINGDI_  
  22. typedef struct tagBITMAPFILEHEADER {   
  23.         WORD    bfType;   
  24.         DWORD   bfSize;   
  25.         WORD    bfReserved1;   
  26.         WORD    bfReserved2;   
  27.         DWORD   bfOffBits;   
  28. } BITMAPFILEHEADER, FAR *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER;   
  29.    
  30. typedef struct tagBITMAPINFOHEADER{   
  31.         DWORD      biSize;   
  32.         LONG       biWidth;   
  33.         LONG       biHeight;   
  34.         WORD       biPlanes;   
  35.         WORD       biBitCount;   
  36.         DWORD      biCompression;   
  37.         DWORD      biSizeImage;   
  38.         LONG       biXPelsPerMeter;   
  39.         LONG       biYPelsPerMeter;   
  40.         DWORD      biClrUsed;   
  41.         DWORD      biClrImportant;   
  42. } BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;   
  43.    
  44. #endif   
  45.    
  46. //保存BMP文件的函数  
  47. void SaveAsBMP (AVFrame *pFrameRGB, int width, int height, int index, int bpp)   
  48. {   
  49.     char buf[5] = {0};   
  50.     BITMAPFILEHEADER bmpheader;   
  51.     BITMAPINFOHEADER bmpinfo;   
  52.     FILE *fp;   
  53.        
  54.     char *filename = new char[255];  
  55.        //文件存放路径,根据自己的修改  
  56.     sprintf_s(filename,255,"%s%d.bmp","D:/My Documents/Visual Studio 2008/Projects/WriteVideo/Debug/test",index);  
  57.     if ( (fp=fopen(filename,"wb+")) == NULL )   
  58.     {   
  59.        printf ("open file failed!\n");   
  60.        return;   
  61.     }   
  62.    
  63.     bmpheader.bfType = 0x4d42;   
  64.     bmpheader.bfReserved1 = 0;   
  65.     bmpheader.bfReserved2 = 0;   
  66.     bmpheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);   
  67.     bmpheader.bfSize = bmpheader.bfOffBits + width*height*bpp/8;   
  68.        
  69.     bmpinfo.biSize = sizeof(BITMAPINFOHEADER);   
  70.     bmpinfo.biWidth = width;   
  71.     bmpinfo.biHeight = height;   
  72.     bmpinfo.biPlanes = 1;   
  73.     bmpinfo.biBitCount = bpp;   
  74.     bmpinfo.biCompression = BI_RGB;   
  75.     bmpinfo.biSizeImage = (width*bpp+31)/32*4*height;   
  76.     bmpinfo.biXPelsPerMeter = 100;   
  77.     bmpinfo.biYPelsPerMeter = 100;   
  78.     bmpinfo.biClrUsed = 0;   
  79.     bmpinfo.biClrImportant = 0;   
  80.        
  81.     fwrite (&bmpheader, sizeof(bmpheader), 1, fp);   
  82.     fwrite (&bmpinfo, sizeof(bmpinfo), 1, fp);   
  83.     fwrite (pFrameRGB->data[0], width*height*bpp/8, 1, fp);   
  84.        
  85.     fclose(fp);   
  86. }   
  87.    
  88. //主函数  
  89. int main (void)   
  90. {   
  91.     unsigned int i = 0, videoStream = -1;   
  92.     AVCodecContext *pCodecCtx;   
  93.     AVFormatContext *pFormatCtx;   
  94.     AVCodec *pCodec;   
  95.     AVFrame *pFrame, *pFrameRGB;   
  96.     struct SwsContext *pSwsCtx;   
  97.     const char *filename = "D:/My Documents/Visual Studio 2008/Projects/WriteVideo/Debug/DELTA.MPG";   
  98.     AVPacket packet;   
  99.     int frameFinished;   
  100.     int PictureSize;   
  101.     uint8_t *buf;   
  102.      //注册编解码器  
  103.     av_register_all();   
  104.      //打开视频文件  
  105.     if ( av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL) != 0 )   
  106.     {   
  107.         printf ("av open input file failed!\n");   
  108.         exit (1);   
  109.     }   
  110.      //获取流信息  
  111.     if ( av_find_stream_info(pFormatCtx) < 0 )   
  112.     {   
  113.         printf ("av find stream info failed!\n");   
  114.         exit (1);   
  115.     }   
  116.      //获取视频流  
  117.     for ( i&#61;0; inb_streams; i&#43;&#43; )   
  118.     if ( pFormatCtx->streams[i]->codec->codec_type &#61;&#61; CODEC_TYPE_VIDEO )   
  119.     {   
  120.        videoStream &#61; i;   
  121.        break;   
  122.     }   
  123.        
  124.     if (videoStream &#61;&#61; -1)   
  125.     {   
  126.         printf ("find video stream failed!\n");   
  127.         exit (1);   
  128.     }   
  129.        
  130.     pCodecCtx &#61; pFormatCtx->streams[videoStream]->codec;   
  131.        
  132.     pCodec &#61; avcodec_find_decoder (pCodecCtx->codec_id);   
  133.        
  134.     if (pCodec &#61;&#61; NULL)   
  135.     {   
  136.         printf ("avcode find decoder failed!\n");   
  137.         exit (1);   
  138.     }   
  139.      //打开解码器  
  140.     if ( avcodec_open(pCodecCtx, pCodec)<0 )   
  141.     {   
  142.         printf ("avcode open failed!\n");   
  143.         exit (1);   
  144.     }   
  145.        
  146.    //为每帧图像分配内存  
  147.     pFrame &#61; avcodec_alloc_frame();   
  148.     pFrameRGB &#61; avcodec_alloc_frame();   
  149.        
  150.     if ( (pFrame&#61;&#61;NULL)||(pFrameRGB&#61;&#61;NULL) )   
  151.     {   
  152.         printf("avcodec alloc frame failed!\n");   
  153.         exit (1);   
  154.     }   
  155.        
  156.     PictureSize &#61; avpicture_get_size (PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height);   
  157.     buf &#61; (uint8_t*)av_malloc(PictureSize);   
  158.        
  159.     if ( buf &#61;&#61; NULL )   
  160.     {   
  161.         printf( "av malloc failed!\n");   
  162.         exit(1);   
  163.     }   
  164.     avpicture_fill ( (AVPicture *)pFrameRGB, buf, PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height);   
  165.        
  166. //设置图像转换上下文  
  167.     pSwsCtx &#61; sws_getContext (pCodecCtx->width,   
  168.              pCodecCtx->height,   
  169.              pCodecCtx->pix_fmt,   
  170.              pCodecCtx->width,   
  171.              pCodecCtx->height,   
  172.              PIX_FMT_BGR24,   
  173.              SWS_BICUBIC,   
  174.              NULL, NULL, NULL);   
  175.     i &#61; 0;   
  176.     while(av_read_frame(pFormatCtx, &packet) >&#61; 0)   
  177.     {   
  178.     if(packet.stream_index&#61;&#61;videoStream)   
  179.     {   
  180.        avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,  
  181.      packet.data, packet.size);   
  182.          
  183.        if(frameFinished)   
  184.        {      
  185.             //反转图像 &#xff0c;否则生成的图像是上下调到的  
  186.             pFrame->data[0] &#43;&#61; pFrame->linesize[0] * (pCodecCtx->height - 1);   
  187.             pFrame->linesize[0] *&#61; -1;   
  188.             pFrame->data[1] &#43;&#61; pFrame->linesize[1] * (pCodecCtx->height / 2 - 1);   
  189.             pFrame->linesize[1] *&#61; -1;   
  190.             pFrame->data[2] &#43;&#61; pFrame->linesize[2] * (pCodecCtx->height / 2 - 1);   
  191.             pFrame->linesize[2] *&#61; -1;   
  192.      //转换图像格式&#xff0c;将解压出来的YUV420P的图像转换为BRG24的图像  
  193.             sws_scale (pSwsCtx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);   
  194.      SaveAsBMP (pFrameRGB, pCodecCtx->width, pCodecCtx->height, i&#43;&#43;, 24);   
  195.        }       
  196.     }   
  197.     av_free_packet(&packet);   
  198.     }   
  199.        
  200.     sws_freeContext (pSwsCtx);   
  201.     av_free (pFrame);   
  202.     av_free (pFrameRGB);   
  203.     avcodec_close (pCodecCtx);   
  204.     av_close_input_file (pFormatCtx);   
  205.        
  206.     return 0;   
  207. }   

转载于:https://www.cnblogs.com/moonvan/archive/2011/09/11/2173467.html


推荐阅读
  • 本教程涵盖OpenGL基础操作及直线光栅化技术,包括点的绘制、简单图形绘制、直线绘制以及DDA和中点画线算法。通过逐步实践,帮助读者掌握OpenGL的基本使用方法。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 本文深入探讨了C++对象模型中的一些细节问题,特别是虚拟继承和析构函数的处理。通过具体代码示例和详细分析,揭示了书中某些观点的不足之处,并提供了更合理的解释。 ... [详细]
  • 深入理解Redis的数据结构与对象系统
    本文详细探讨了Redis中的数据结构和对象系统的实现,包括字符串、列表、集合、哈希表和有序集合等五种核心对象类型,以及它们所使用的底层数据结构。通过分析源码和相关文献,帮助读者更好地理解Redis的设计原理。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 在金融和会计领域,准确无误地填写票据和结算凭证至关重要。这些文件不仅是支付结算和现金收付的重要依据,还直接关系到交易的安全性和准确性。本文介绍了一种使用C语言实现小写金额转换为大写金额的方法,确保数据的标准化和规范化。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文详细探讨了VxWorks操作系统中双向链表和环形缓冲区的实现原理及使用方法,通过具体示例代码加深理解。 ... [详细]
  • 基因组浏览器中的Wig格式解析
    本文详细介绍了Wiggle(Wig)格式及其在基因组浏览器中的应用,涵盖variableStep和fixedStep两种主要格式的特点、适用场景及具体使用方法。同时,还提供了关于数据值和自定义参数的补充信息。 ... [详细]
  • 实体映射最强工具类:MapStruct真香 ... [详细]
  • 作者:守望者1028链接:https:www.nowcoder.comdiscuss55353来源:牛客网面试高频题:校招过程中参考过牛客诸位大佬的面经,但是具体哪一块是参考谁的我 ... [详细]
  • 本题旨在通过给定的评级信息,利用拓扑排序和并查集算法来确定全球 Tetris 高手排行榜。题目要求判断是否可以根据提供的信息生成一个明确的排名表,或者是否存在冲突或信息不足的情况。 ... [详细]
  • 本文详细探讨了HTML表单中GET和POST请求的区别,包括它们的工作原理、数据传输方式、安全性及适用场景。同时,通过实例展示了如何在Servlet中处理这两种请求。 ... [详细]
  • 本问题探讨了在特定条件下排列儿童队伍的方法数量。题目要求计算满足条件的队伍排列总数,并使用递推算法和大数处理技术来解决这一问题。 ... [详细]
author-avatar
迷人的小刀
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有