[转载] ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片

时间:2023-03-10 05:03:48
[转载] ffmpeg Windows下采集摄像头一帧数据,并保存为bmp图片

这里请注意,在编译ffmpeg时,不要使用--disable-devices选项。

使用

--enable-encoder=rawvideo
 --enable-decoder=rawvideo

启用rawvideo codec。

代码如下:

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <libavformat/avformat.h>
  5. #include <libavcodec/avcodec.h>
  6. #include <libavdevice/avdevice.h>
  7. #include <libswscale/swscale.h>
  8. #include <windows.h>
  9. #include <time.h>
  10. #define MAX_INPUT_DEVICE_NUM 10
  11. #ifdef _WIN32
  12. int strcasecmp(const char *s1, const char *s2)
  13. {
  14. while ((*s1 != '\0')
  15. && (tolower(*(unsigned char *) s1) ==
  16. tolower(*(unsigned char *) s2)))
  17. {
  18. s1++;
  19. s2++;
  20. }
  21. return tolower(*(unsigned char *) s1) - tolower(*(unsigned char *) s2);
  22. }
  23. int strncasecmp(const char *s1, const char *s2, unsigned int n)
  24. {
  25. if (n == 0)
  26. return 0;
  27. while ((n-- != 0)
  28. && (tolower(*(unsigned char *) s1) ==
  29. tolower(*(unsigned char *) s2))) {
  30. if (n == 0 || *s1 == '\0' || *s2 == '\0')
  31. return 0;
  32. s1++;
  33. s2++;
  34. }
  35. return tolower(*(unsigned char *) s1) - tolower(*(unsigned char *) s2);
  36. }
  37. #endif
  38. void save_bmp(unsigned char * data,int data_size,int w,int h,FILE * out)
  39. {
  40. // 位图文件头
  41. BITMAPFILEHEADER bmpheader;
  42. BITMAPINFO bmpinfo;
  43. int bit = 24;
  44. bmpheader.bfType = ('M' <<8)|'B';
  45. bmpheader.bfReserved1 = 0;
  46. bmpheader.bfReserved2 = 0;
  47. bmpheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  48. bmpheader.bfSize = bmpheader.bfOffBits + w*h*bit/8;
  49. bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  50. bmpinfo.bmiHeader.biWidth = w;
  51. bmpinfo.bmiHeader.biHeight = 0-h;
  52. bmpinfo.bmiHeader.biPlanes = 1;
  53. bmpinfo.bmiHeader.biBitCount = bit;
  54. bmpinfo.bmiHeader.biCompression = BI_RGB;
  55. bmpinfo.bmiHeader.biSizeImage = 0;
  56. bmpinfo.bmiHeader.biXPelsPerMeter = 100;
  57. bmpinfo.bmiHeader.biYPelsPerMeter = 100;
  58. bmpinfo.bmiHeader.biClrUsed = 0;
  59. bmpinfo.bmiHeader.biClrImportant = 0;
  60. fwrite(&bmpheader,sizeof(BITMAPFILEHEADER),1,out);
  61. fwrite(&bmpinfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,out);
  62. fwrite(data,data_size,1,out);
  63. }
  64. int CaptureFromLocalCamera()
  65. {
  66. AVFormatContext *ic = NULL;
  67. AVFormatParameters in_fmt_para={0};
  68. AVPacket packet;
  69. char buffer[MAX_PATH]={0};
  70. int width = 0,height = 0;
  71. int ret,video_stream = -1,i=0;
  72. //查找输入(vfwcap)格式
  73. AVInputFormat *in_fmt = av_find_input_format ("vfwcap");
  74. if (in_fmt == NULL)
  75. {
  76. printf("not support input device vfwcap.\n");
  77. return -1;
  78. }
  79. memset (&in_fmt_para, 0, sizeof(in_fmt_para));
  80. //指定需要采集图像的高度
  81. in_fmt_para.height = height;
  82. //指定需要采集图像的宽度
  83. in_fmt_para.width  = width;
  84. //设置帧率
  85. av_parse_video_frame_rate(&in_fmt_para.time_base,"20");
  86. //打开摄像头设备,从"0"到MAX_INPUT_DEVICE_NUM依次尝试打开
  87. for( i=0 ; i < MAX_INPUT_DEVICE_NUM + 1; i++ )
  88. {
  89. sprintf(buffer,"%d",i);
  90. ret = av_open_input_file ( &ic, buffer, in_fmt,sizeof(in_fmt_para),&in_fmt_para);
  91. if ( ret == 0 && ic)
  92. {
  93. break;
  94. }
  95. }
  96. //open success?
  97. if(!ic || ret != 0)
  98. {
  99. if(ic)
  100. av_close_input_file(ic);
  101. printf("can not open input file.\n");
  102. return -2;
  103. }
  104. printf("input device no. is %d\n",i);
  105. //find the video stream
  106. for(i=0;i<ic ->nb_streams;i++)
  107. {
  108. if ( CODEC_TYPE_VIDEO == ic ->streams[i] ->codec ->codec_type )
  109. {
  110. video_stream = i;
  111. break;
  112. }
  113. }
  114. if(video_stream < 0)
  115. {
  116. av_close_input_file(ic);
  117. printf("can not find a video stream.\n");
  118. return -3;
  119. }
  120. //获取视频时间宽度和高度
  121. width  = ic ->streams[video_stream] ->codec ->width;
  122. height = ic ->streams[video_stream] ->codec ->height;
  123. printf("video size: %dx%d\n",width,height);
  124. //从摄像头获取图像数据
  125. if( 0 == av_read_frame(ic,&packet))
  126. {
  127. //find the decode codec
  128. AVCodec * decodec =  avcodec_find_decoder(ic ->streams[video_stream] ->codec ->codec_id);
  129. if(decodec)
  130. {
  131. //open the decode codec
  132. if( 0 == avcodec_open(ic ->streams[video_stream] ->codec,decodec) )
  133. {
  134. int got_picture = 0;
  135. AVFrame * frame = avcodec_alloc_frame();
  136. avcodec_decode_video2(ic ->streams[video_stream] ->codec,frame,&got_picture,&packet);
  137. //decode success
  138. if(got_picture)
  139. {
  140. uint8_t * buffer = NULL;
  141. size_t buffer_size = 0;
  142. struct SwsContext *pSwsCtx=NULL;
  143. AVFrame * rgb_frame = avcodec_alloc_frame();
  144. buffer_size = avpicture_get_size(PIX_FMT_BGR24,width,height);
  145. buffer = (uint8_t *)av_malloc(buffer_size);
  146. avpicture_fill((AVPicture*)rgb_frame,(uint8_t *)buffer,PIX_FMT_BGR24,width,height);
  147. //get swscale ctx
  148. pSwsCtx = sws_getContext(
  149. ic ->streams[video_stream] ->codec ->width,
  150. ic ->streams[video_stream] ->codec ->height,
  151. ic ->streams[video_stream] ->codec ->pix_fmt,
  152. width,
  153. height,
  154. PIX_FMT_BGR24,
  155. SWS_BILINEAR,
  156. NULL,
  157. NULL,
  158. NULL);
  159. if(pSwsCtx)
  160. {
  161. FILE *fp = NULL;
  162. SYSTEMTIME dt={0};
  163. //图像格式转换
  164. sws_scale(
  165. pSwsCtx,
  166. frame ->data,
  167. frame ->linesize,
  168. 0,
  169. ic ->streams[video_stream] ->codec ->height,
  170. rgb_frame ->data,
  171. rgb_frame ->linesize);
  172. //create the image file name
  173. GetLocalTime(&dt);
  174. srand(0);
  175. sprintf(buffer,"imgs/%04d_%02d_%02d %02d_%02d_%02d %02d.bmp",dt.wYear,dt.wMonth,dt.wDay,dt.wHour,dt.wMinute,dt.wSecond,rand()%30);
  176. //
  177. CreateDirectoryA("imgs",NULL);
  178. //open file
  179. fp = fopen(buffer, "wb");
  180. if(fp)
  181. {
  182. save_bmp(rgb_frame ->data[0],rgb_frame ->linesize[0]*height,width,height,fp);
  183. fclose(fp);
  184. }
  185. //free sws ctx
  186. sws_freeContext(pSwsCtx);
  187. }
  188. //free buffer
  189. av_free(rgb_frame);
  190. av_free(buffer);
  191. }
  192. //free buffer
  193. av_free(frame);
  194. //close the decode codec
  195. avcodec_close(ic ->streams[video_stream] ->codec);
  196. }
  197. }
  198. }
  199. //close the input device
  200. av_close_input_file(ic);
  201. return 0;
  202. }
  203. int main()
  204. {
  205. //avcodec_init();
  206. avcodec_register_all();
  207. avdevice_register_all();
  208. CaptureFromLocalCamera();
  209. return 0;
  210. }