织梦CMS - 轻松建站从此开始!

罗索

利用FFmpeg将视频文件生成bmp图像帧(解决反转问题)

落鹤生 发布于 2010-06-22 21:03 点击:次 
#include stdio.h #include stdlib.h #include string.h #include windows.h #include avformat.h #include avcodec.h #include swscale.h #pragma comment (lib, avcodec.lib) #pragma comment (lib, avformat.lib) #pragma comment (lib, avutil.lib) #pragma commen
TAG:

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <string.h> 
  4. #include <windows.h> 
  5.  
  6. #include "avformat.h" 
  7. #include "avcodec.h" 
  8. #include "swscale.h" 
  9.  
  10. #pragma comment (lib, "avcodec.lib") 
  11. #pragma comment (lib, "avformat.lib") 
  12. #pragma comment (lib, "avutil.lib") 
  13. #pragma comment (lib, "swscale.lib") 
  14.  
  15. #ifndef _WINGDI_ 
  16. #define _WINGDI_ 
  17.  
  18. typedef struct tagBITMAPFILEHEADER { 
  19.         WORD    bfType; 
  20.         DWORD   bfSize; 
  21.         WORD    bfReserved1; 
  22.         WORD    bfReserved2; 
  23.         DWORD   bfOffBits; 
  24. } BITMAPFILEHEADER, FAR *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER; 
  25.  
  26. typedef struct tagBITMAPINFOHEADER{ 
  27.         DWORD      biSize; 
  28.         LONG       biWidth; 
  29.         LONG       biHeight; 
  30.         WORD       biPlanes; 
  31.         WORD       biBitCount; 
  32.         DWORD      biCompression; 
  33.         DWORD      biSizeImage; 
  34.         LONG       biXPelsPerMeter; 
  35.         LONG       biYPelsPerMeter; 
  36.         DWORD      biClrUsed; 
  37.         DWORD      biClrImportant; 
  38. } BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER; 
  39.  
  40. #endif 
  41.  
  42. void SaveAsBMP (AVFrame *pFrameRGB, int width, int height, int index, int bpp) 
  43. char buf[5] = {0}; 
  44. BITMAPFILEHEADER bmpheader; 
  45. BITMAPINFOHEADER bmpinfo; 
  46. FILE *fp; 
  47.  
  48. char filename[20] = "R:\\test"
  49. _itoa (index, buf, 10); 
  50. strcat (filename, buf); 
  51. strcat (filename, ".bmp"); 
  52.  
  53. if ( (fp=fopen(filename,"wb+")) == NULL ) 
  54.    printf ("open file failed!\n"); 
  55.    return
  56.  
  57. bmpheader.bfType = 0x4d42; 
  58. bmpheader.bfReserved1 = 0; 
  59. bmpheader.bfReserved2 = 0; 
  60. bmpheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); 
  61. bmpheader.bfSize = bmpheader.bfOffBits + width*height*bpp/8; 
  62.  
  63. bmpinfo.biSize = sizeof(BITMAPINFOHEADER); 
  64. bmpinfo.biWidth = width; 
  65. bmpinfo.biHeight = height; 
  66. bmpinfo.biPlanes = 1; 
  67. bmpinfo.biBitCount = bpp; 
  68. bmpinfo.biCompression = BI_RGB; 
  69. bmpinfo.biSizeImage = (width*bpp+31)/32*4*height; 
  70. bmpinfo.biXPelsPerMeter = 100; 
  71. bmpinfo.biYPelsPerMeter = 100; 
  72. bmpinfo.biClrUsed = 0; 
  73. bmpinfo.biClrImportant = 0; 
  74.  
  75. fwrite (&bmpheader, sizeof(bmpheader), 1, fp); 
  76. fwrite (&bmpinfo, sizeof(bmpinfo), 1, fp); 
  77. fwrite (pFrameRGB->data[0], width*height*bpp/8, 1, fp); 
  78.  
  79. fclose(fp); 
  80.  
  81. int main (void
  82. unsigned int i = 0, videoStream = -1; 
  83. AVCodecContext *pCodecCtx; 
  84. AVFormatContext *pFormatCtx; 
  85. AVCodec *pCodec; 
  86. AVFrame *pFrame, *pFrameRGB; 
  87. struct SwsContext *pSwsCtx; 
  88. const char *filename = "test.avi"
  89. AVPacket packet; 
  90. int frameFinished; 
  91. int PictureSize; 
  92. uint8_t *buf; 
  93.  
  94. av_register_all(); 
  95.  
  96. if ( av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL) != 0 ) 
  97. printf ("av open input file failed!\n"); 
  98. exit (1); 
  99.  
  100. if ( av_find_stream_info(pFormatCtx) < 0 ) 
  101. printf ("av find stream info failed!\n"); 
  102. exit (1); 
  103.  
  104. for ( i=0; i<pFormatCtx->nb_streams; i++ ) 
  105. if ( pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO ) 
  106.    videoStream = i; 
  107.    break
  108.  
  109. if (videoStream == -1) 
  110. printf ("find video stream failed!\n"); 
  111. exit (1); 
  112.  
  113. pCodecCtx = pFormatCtx->streams[videoStream]->codec; 
  114.  
  115. pCodec = avcodec_find_decoder (pCodecCtx->codec_id); 
  116.  
  117. if (pCodec == NULL) 
  118. printf ("avcode find decoder failed!\n"); 
  119. exit (1); 
  120.  
  121. if ( avcodec_open(pCodecCtx, pCodec)<0 ) 
  122. printf ("avcode open failed!\n"); 
  123. exit (1); 
  124.  
  125. pFrame = avcodec_alloc_frame(); 
  126. pFrameRGB = avcodec_alloc_frame(); 
  127.  
  128. if ( (pFrame==NULL)||(pFrameRGB==NULL) ) 
  129. printf("avcodec alloc frame failed!\n"); 
  130. exit (1); 
  131.  
  132. PictureSize = avpicture_get_size (PIX_FMT_BGR24,
  133.  pCodecCtx->width, pCodecCtx->height); 
  134.  
  135. buf = av_malloc(PictureSize); 
  136.  
  137. if ( buf == NULL ) 
  138. printf( "av malloc failed!\n"); 
  139. exit(1); 
  140.  
  141. avpicture_fill ( (AVPicture *)pFrameRGB, buf, PIX_FMT_BGR24,
  142.  pCodecCtx->width, pCodecCtx->height); 
  143.  
  144. pSwsCtx = sws_getContext (pCodecCtx->width, 
  145.          pCodecCtx->height, 
  146.          pCodecCtx->pix_fmt, 
  147.          pCodecCtx->width, 
  148.          pCodecCtx->height, 
  149.          PIX_FMT_BGR24, 
  150.          SWS_BICUBIC, 
  151.          NULL, NULL, NULL); 
  152.  
  153. i = 0; 
  154.  
  155. while(av_read_frame(pFormatCtx, &packet) >= 0) 
  156. if(packet.stream_index==videoStream) 
  157.    avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,
  158.  packet.data, packet.size); 
  159.    
  160.    if(frameFinished) 
  161.    {    
  162.     //反转图像 
  163.     //*(pFrame->data[0]) = pCodecCtx->width * (pCodecCtx->height-1); 
  164.     //pFrame ->linesize[0] = -(pCodecCtx->height); 
  165.     pFrame->data[0] += pFrame->linesize[0] * (pCodecCtx->height - 1); 
  166.     pFrame->linesize[0] *= -1; 
  167.     pFrame->data[1] += pFrame->linesize[1] * (pCodecCtx->height / 2 - 1); 
  168.     pFrame->linesize[1] *= -1; 
  169.     pFrame->data[2] += pFrame->linesize[2] * (pCodecCtx->height / 2 - 1); 
  170.     pFrame->linesize[2] *= -1; 
  171.     
  172.     sws_scale (pSwsCtx, pFrame->data, pFrame->linesize, 0,
  173.  pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize); 
  174.  
  175.     SaveAsBMP (pFrameRGB, pCodecCtx->width, pCodecCtx->height, i++, 24); 
  176.    }     
  177. av_free_packet(&packet); 
  178.  
  179. sws_freeContext (pSwsCtx); 
  180. av_free (pFrame); 
  181. av_free (pFrameRGB); 
  182. avcodec_close (pCodecCtx); 
  183. av_close_input_file (pFormatCtx); 
  184.  
  185. return 0; 

 

(myyb)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201006/9700.html]
本文出处:百度博客 作者:myyb
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容