当前位置: 首页 > 编程日记 > 正文

ffmpeg解码视频存为BMP文件

ffmpeg解码视频存为BMP文件

分类: ffmpeg2011-07-28 12:13 8人阅读 评论(0) 收藏 举报
view plain
  1. #include <windows.h>  
  2.  #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #pragma once    
  6.   
  7.  #ifdef __cplusplus  
  8. extern "C" {  
  9. #endif  
  10. #include <libavcodec/avcodec.h>  
  11. #include <libavformat/avformat.h>  
  12. #include <libswscale/swscale.h>  
  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=0; i<pFormatCtx->nb_streams; i++ )   
  118.     if ( pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO )   
  119.     {   
  120.        videoStream = i;   
  121.        break;   
  122.     }   
  123.        
  124.     if (videoStream == -1)   
  125.     {   
  126.         printf ("find video stream failed!\n");   
  127.         exit (1);   
  128.     }   
  129.        
  130.     pCodecCtx = pFormatCtx->streams[videoStream]->codec;   
  131.        
  132.     pCodec = avcodec_find_decoder (pCodecCtx->codec_id);   
  133.        
  134.     if (pCodec == 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 = avcodec_alloc_frame();   
  148.     pFrameRGB = avcodec_alloc_frame();   
  149.        
  150.     if ( (pFrame==NULL)||(pFrameRGB==NULL) )   
  151.     {   
  152.         printf("avcodec alloc frame failed!\n");   
  153.         exit (1);   
  154.     }   
  155.        
  156.     PictureSize = avpicture_get_size (PIX_FMT_BGR24, pCodecCtx->width, pCodecCtx->height);   
  157.     buf = (uint8_t*)av_malloc(PictureSize);   
  158.        
  159.     if ( buf == 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 = 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 = 0;   
  176.     while(av_read_frame(pFormatCtx, &packet) >= 0)   
  177.     {   
  178.     if(packet.stream_index==videoStream)   
  179.     {   
  180.        avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,  
  181.      packet.data, packet.size);   
  182.          
  183.        if(frameFinished)   
  184.        {      
  185.             //反转图像 ,否则生成的图像是上下调到的  
  186.             pFrame->data[0] += pFrame->linesize[0] * (pCodecCtx->height - 1);   
  187.             pFrame->linesize[0] *= -1;   
  188.             pFrame->data[1] += pFrame->linesize[1] * (pCodecCtx->height / 2 - 1);   
  189.             pFrame->linesize[1] *= -1;   
  190.             pFrame->data[2] += pFrame->linesize[2] * (pCodecCtx->height / 2 - 1);   
  191.             pFrame->linesize[2] *= -1;   
  192.      //转换图像格式,将解压出来的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++, 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

相关文章:

(四)Asp.net web api中的坑-【api的返回值】

&#xff08;四&#xff09;Asp.net web api中的坑-【api的返回值】 原文:&#xff08;四&#xff09;Asp.net web api中的坑-【api的返回值】void无返回值IHttpActionResultHttpResponseMessage自定义类型我这里并不想赘述这些返回类型&#xff0c; 可以参考博文http://blog.c…

如何提高编程能力?

其实很多人学编程都会遇到困难&#xff0c;我觉得其中一个根本原因是他们没搞明白学编程到底是学什么。编程不是一种知识&#xff0c;而是一门手艺。我们从小到大的学习都是学习知识&#xff0c;流程一般是课前看书预习&#xff0c;上课听讲&#xff0c;下课做作业&#xff0c;…

【HTML】兴唐第二十八节课之初识HTML

1、HTML&#xff1a;hyper text markup language&#xff08;超级文本标记语言&#xff09;算编程&#xff0c;但HTML不是编程语言 2、注意&#xff1a; &#xff08;1&#xff09;所有的HTML文件都是以.html或者htm作为扩展名 &#xff08;2&#xff09;html文件需要被浏览…

Nagios插件NDOUtils安装

1.DBI的安装# wget http://www.cpan.org/modules/by-module/DBI/DBI-1.608.tar.gz # tar zxvf DBI-1.608.tar.gz # cd DBI-1.608# perl Makefile.PL# make# make test# make install2.DBD的安装# wget http://www.cpan.org/modules/by-module/DBD/DBD-mysql-4.011.tar.gz # tar…

maven 获取pom.xml的依赖---即仓库搜索服务

常用仓库地址&#xff1a; http://repository.sonatype.org/ (https://repository.sonatype.org/)如下图&#xff1a; http://www.mvnrepository.com 转载于:https://www.cnblogs.com/hblthink/p/8643137.html

XFile 关键帧动画的解析遇到的问题

一、mesh 数据储存方式的修改 由于在设计CXFileMesh类时考虑不够全面&#xff0c;原CXFileMesh 类内部储存mesh数据采用的是vector模板。这使后来试图为该类添加支持3dsmax关键帧动画功能时带来很大麻烦。最后还是对CXFileMesh 类做了整体修改&#xff1a;用二叉树储存mesh数据…

【HTML】兴唐二十八节课之常用标签(不定期更新)

部分属性的详细参数见菜鸟教程 &#xff08;1&#xff09;换行 <br/> (2)字体设置颜色和大小 <font size 6 color blue>小米巨能写</font> &#xff08;3&#xff09;添加图片 <img src "index.jpg" width "300px"> 注&…

Python中自定义类如果重写了__repr__方法为什么会影响到str的输出?

这是因为Python3中&#xff0c;str的输出是调用类的实例方法__str__来输出&#xff0c;如果__str__方法没有重写&#xff0c;则自动继承object类的__str__方法&#xff0c;而object类的__str__方法是调用__repr__方法&#xff0c;因此自定义类未重写__str__方法的情况下&#x…

IT人士的人际关系压力

感谢听心心理学网站的投递在造成IT从业者的众多压力之中&#xff0c;人际关系带来的压力或许是最明显并且循环效应最强的一种。IT行业的冷漠环境是出了名的&#xff0c;在这样的状态之下&#xff0c;如何调整我们的人际关系&#xff0c;将恶性循环改造成良性循环&#xff0c;对…

软件工程网络15结对编程作业

软件工程网络15结对编程作业 1.项目成员 学号&#xff1a;201521123014 博客地址&#xff1a;http://www.cnblogs.com/huangsh/学号&#xff1a; 201521123102 博客地址&#xff1a;http://home.cnblogs.com/u/hyy786030686/结对编程码云项目地址&#xff1a;https://gitee.com…

【THML】兴唐第二十八节课 几个小程序

1、第一个html文件 <HTML><Head><title>小米商城</title></head><body><font size 6 color blue>小米巨能写</font><hr /><img src "index.jpg" width "300px"><p>标签组成部分&a…

Android系统默认Home应用程序(Launcher)的启动过程源代码分析

在前面一篇文章中&#xff0c;我们分析了Android系统在启动时安装应用程序的过程&#xff0c;这些应用程序安装好之后&#xff0c;还需要有一个Home应用程序来负责把它们在桌面上展示出来&#xff0c;在Android系统中&#xff0c;这个默认的Home应用程序就是Launcher了&#xf…

Data - 数据思维 - 下篇

9 - 数据解读与表达 数据解读 数据解读需要选择一个基点、一个参照系&#xff0c;单独的一个数值往往不具备价值&#xff0c;它只是数字。 注意点&#xff1a; 关注异常值&#xff0c;并深究WHY?相互验证、大胆假设、多方验证。把握趋势或者规律。归纳总结、数清理明。数据表达…

cas server 配置

1.修改cas server的deployerConfigContext.xml <bean id"dataSource" class"org.apache.commons.dbcp.BasicDataSource"> <property name"driverClassName"> <value>com.microsoft.sqlserver.jdbc.SQLServerDriv…

四 Vue学习 router学习

index.js: 按需加载组件&#xff1a; const login r > require.ensure([], () > r(require(/page/login)), login); 把JS文件分模块&#xff0c;安需加载&#xff0c;而不是&#xff0c;整个都加载。 routes &#xff1a; 定义路径和组件的mapping关系。c…

Oracle 10.2.0.5.4 Patch Set Update (PSU) – Patch No: p12419392

有关Oracle patch和PSU&#xff0c;PSR 说明参考我的blog&#xff1a;Oracle 补丁体系 及opatch 工具 介绍http://blog.csdn.net/tianlesoftware/article/details/5809526Oracle 10g 最新的版本是10.2.0.5.4. 其中的5是PSR 版本号&#xff0c;4是PSU版本号。MOS 上的2篇文档&am…

【数据库】兴唐第二十八节课零散知识点汇总

1、group by order by等都要放到语句的最后 2、表格标签&#xff1a; <table> <tr>表示行 <td>表示一个行里的单元格 </table> 3、表格调整 内容水平方向跳整&#xff1a; align"center" 表示水平居中 align 有三个值&#xff1a;left…

服务器端往手机端推送数据的问题(手机解决方案)

1.方案一&#xff1a; 思路&#xff1a;使用socket连接&#xff0c;在手机端开个socketserver&#xff0c;然后服务器端连接手机端&#xff0c;实现服务器端的不定时发送数据。 MIDlet关闭时, 你可以通过sms激活它. midlet运行时, 你可以通过socket来解决双向推数据的功能. 个人…

软件测试实验一

实验报告 a) The brief description that I install junit, hamcrest and eclemma. Junit&#xff0c;hamcrest 上网下载junit,hamrest包&#xff0c;然后在项目中新建文件夹lib,复制包到其中&#xff0c;然后单击项目->build path -> configer build path,然后在把包加入…

【java】兴唐第二十九节课作业

将用户在网页填写的信息输入数据库 数据库&#xff1a; create table user_infer(id int(2) not null auto_increment primary key,user_name varchar(12), password varchar(64) not null,real_name varchar(8) not null,age int(3) ); JAAVEE stuList <% page langu…

【php】【psr】psr2 编码风格规范

为避免浏览多个作者参与编写的项目时&#xff0c;因风格的不同造成不便时&#xff0c;大家可以使用同一套风格规范来统一标准 代码必须遵循PSR1的规范缩进使用4个空格&#xff0c;而不是TAB键缩进每行代码控制在80-120个每个namespace申明语句后&#xff0c;每个use申明语句块后…

代码生成器项目正式启动

SVN地址是&#xff1a; svn://www.oksvn.com/CodeAssistant J2EE的项目开发工作本身充斥着各种重复&#xff0c;各种复制&#xff0c;各种粘贴&#xff0c;所以&#xff0c;才会出现了Spring和Struts2这些优秀的框架。 但是在使用这些框架的时候&#xff0c;有些问题也会不停的…

查询XML节点 value

查询XML节点 value&#xff1a;通过nodes 指定到节点通过Value属性取出值Declare Xml xmlset Xml<Employee><ID>1</ID><ID>2</ID></Employee>SELECT ID.value(.,Nvarchar(500)) as EmployeeID FROM Xml.nodes(Employee/ID) Employee(…

JQUERY动态生成当前年份的前5年以及后 2年

由于工作需要&#xff0c;赶紧记录下来转载于:https://www.cnblogs.com/wjhaaa/p/8652298.html

osgearth+vs2010安装

OSGEARTH VS2010 安装*VS 平台不重要,本教程也适用于VS2008等。假设我的OSG目录为&#xff1a;D&#xff1a;/OSG*本教程参考网上osgearthvs2008安装。 一、准备工作下载: http://osgearth.org/wiki/Downloads 1. CURL (curl-7.21.7.tar.gz): http://curl.haxx.se/downl…

Contos7 克隆实例 以及 配置网络-服务-等相关信息

以下为我自己整理的克隆虚拟机和设置固定IP的方法&#xff0c;记录一下&#xff0c;以防忘记&#xff1a; 桥接模式网络配置 1、配置ip地址等信息在文件里做如下配置&#xff1a; /etc/sysconfig/network-scripts/ifcfg-ens33 命令&#xff1a; vi /etc/sysconfig/network-sc…

【jdbc】兴唐第三十一节课之修改数据和查询数据(使用自己写的DBUtil)

一、修改数据 方法一 代码实现&#xff1a; public static void opDBByNormal() {DruidDataSource dds new DruidDataSource(); dds.setUsername("root");dds.setPassword("root");dds.setUrl("jdbc:mysql://localhost:3306/system");dds.se…

ios4.2文件夹及多任务

ios4.2的文件夹和多任务可谓是主要特性,但是安装完后我却丝毫不知道该怎么做...在网上找了好久总算解决了 多任务:双击home键,在屏幕下方就会显示当前正在执行的任务,如http://tech.sina.com.cn/it/2010-11-22/22334894444.shtml所示. 文件夹操作:见视频http://v.youku.com/v_s…

利用Unity3D制作简易2D计算器

利用Unity3D制作简易2D计算器 标签&#xff08;空格分隔&#xff09;&#xff1a; uiniy3D 1. 操作流程 在unity3DD中创建一个新项目 注意选择是2D的&#xff08;因为默认3D&#xff09; 在Assets框右键新建C#脚本 在新建的C#脚本中写入下列代码 代码下载地址 https://downlo…

将moss 2007的模板文件导入到moss 2010

最近公司HR请请将一个moss2007的调查模板文件导入到我们部门的Moss protal 上面。 我想这是举手之劳&#xff0c;就爽快的答应了。 但是导入时却报如下错误&#xff1a; ErrorMicrosoft SharePoint Foundation version 3 templates are not supported in this version of the p…