main.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. 
  2. /**
  3. * 叶海辉
  4. * QQ群121376426
  5. * http://blog.yundiantech.com/
  6. */
  7. #include <stdio.h>
  8. extern "C"
  9. {
  10. #include "libavcodec/avcodec.h"
  11. #include "libavformat/avformat.h"
  12. #include "libswscale/swscale.h"
  13. #include "libavdevice/avdevice.h"
  14. }
  15. //'1' Use Dshow
  16. //'0' Use VFW
  17. #define USE_DSHOW 0
  18. //Show Dshow Device
  19. void show_dshow_device(){
  20. AVFormatContext *pFormatCtx = avformat_alloc_context();
  21. AVDictionary* options = NULL;
  22. av_dict_set(&options,"list_devices","true",0);
  23. AVInputFormat *iformat = av_find_input_format("dshow");
  24. printf("========Device Info=============\n");
  25. avformat_open_input(&pFormatCtx,"video=dummy",iformat,&options);
  26. printf("================================\n");
  27. }
  28. //Show Dshow Device Option
  29. void show_dshow_device_option(){
  30. AVFormatContext *pFormatCtx = avformat_alloc_context();
  31. AVDictionary* options = NULL;
  32. av_dict_set(&options,"list_options","true",0);
  33. AVInputFormat *iformat = av_find_input_format("dshow");
  34. printf("========Device Option Info======\n");
  35. avformat_open_input(&pFormatCtx,"video=Integrated Camera",iformat,&options);
  36. printf("================================\n");
  37. }
  38. //Show VFW Device
  39. void show_vfw_device(){
  40. AVFormatContext *pFormatCtx = avformat_alloc_context();
  41. AVInputFormat *iformat = av_find_input_format("vfwcap");
  42. printf("========VFW Device Info======\n");
  43. avformat_open_input(&pFormatCtx,"list",iformat,NULL);
  44. printf("=============================\n");
  45. }
  46. //Show AVFoundation Device
  47. void show_avfoundation_device(){
  48. AVFormatContext *pFormatCtx = avformat_alloc_context();
  49. AVDictionary* options = NULL;
  50. av_dict_set(&options,"list_devices","true",0);
  51. AVInputFormat *iformat = av_find_input_format("avfoundation");
  52. printf("==AVFoundation Device Info===\n");
  53. avformat_open_input(&pFormatCtx,"",iformat,&options);
  54. printf("=============================\n");
  55. }
  56. #define USE_DSHOW 1
  57. int main(int argc, char* argv[])
  58. {
  59. AVFormatContext *pFormatCtx;
  60. int i, videoindex;
  61. AVCodecContext *pCodecCtx;
  62. AVCodec *pCodec;
  63. av_register_all();
  64. avformat_network_init();
  65. avdevice_register_all();//Register Device
  66. //Show Dshow Device
  67. show_dshow_device();
  68. //Show Device Options
  69. // show_dshow_device_option();
  70. //Show VFW Options
  71. // show_vfw_device();
  72. pFormatCtx = avformat_alloc_context();
  73. #if USE_DSHOW
  74. //Use dshow
  75. //
  76. //这里需要先安装 screen-capture-recorder 才能使用dshow采集屏幕
  77. //screen-capture-recorder
  78. //Website: http://sourceforge.net/projects/screencapturer/
  79. //
  80. AVInputFormat *ifmt=av_find_input_format("dshow");
  81. if(avformat_open_input(&pFormatCtx,"video=screen-capture-recorder",ifmt,NULL)!=0){
  82. printf("Couldn't open input stream.\n");
  83. return -1;
  84. }
  85. #else
  86. //Use gdigrab
  87. AVDictionary* options = NULL;
  88. //Set some options
  89. //grabbing frame rate
  90. //av_dict_set(&options,"framerate","5",0);
  91. //The distance from the left edge of the screen or desktop
  92. //av_dict_set(&options,"offset_x","20",0);
  93. //The distance from the top edge of the screen or desktop
  94. //av_dict_set(&options,"offset_y","40",0);
  95. //Video frame size. The default is to capture the full screen
  96. //av_dict_set(&options,"video_size","640x480",0);
  97. AVInputFormat *ifmt=av_find_input_format("gdigrab");
  98. if(avformat_open_input(&pFormatCtx,"desktop",ifmt,&options)!=0){
  99. printf("Couldn't open input stream.\n");
  100. return -1;
  101. }
  102. #endif
  103. if(avformat_find_stream_info(pFormatCtx,NULL)<0)
  104. {
  105. printf("Couldn't find stream information.\n");
  106. return -1;
  107. }
  108. videoindex=-1;
  109. for(i=0; i<pFormatCtx->nb_streams; i++)
  110. {
  111. if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
  112. {
  113. videoindex=i;
  114. }
  115. }
  116. if(videoindex==-1)
  117. {
  118. printf("Couldn't find a video stream.\n");
  119. return -1;
  120. }
  121. pCodecCtx=pFormatCtx->streams[videoindex]->codec;
  122. pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
  123. if(pCodec==NULL)
  124. {
  125. printf("Codec not found.\n");
  126. return -1;
  127. }
  128. if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
  129. {
  130. printf("Could not open codec.\n");
  131. return -1;
  132. }
  133. AVFrame *pFrame,*pFrameYUV;
  134. pFrame=av_frame_alloc();
  135. pFrameYUV=av_frame_alloc();
  136. uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
  137. avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
  138. int ret, got_picture;
  139. AVPacket *packet=(AVPacket *)av_malloc(sizeof(AVPacket));
  140. FILE *fp_yuv=fopen("output.yuv","wb");
  141. struct SwsContext *img_convert_ctx;
  142. img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
  143. ///这里打印出视频的宽高
  144. fprintf(stderr,"w= %d h= %d\n",pCodecCtx->width, pCodecCtx->height);
  145. ///我们就读取100张图像
  146. for(int i=0;i<100;i++)
  147. {
  148. if(av_read_frame(pFormatCtx, packet) < 0)
  149. {
  150. break;
  151. }
  152. if(packet->stream_index==videoindex)
  153. {
  154. ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
  155. if(ret < 0){
  156. printf("Decode Error.\n");
  157. return -1;
  158. }
  159. if(got_picture)
  160. {
  161. sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
  162. int y_size=pCodecCtx->width*pCodecCtx->height;
  163. fwrite(pFrameYUV->data[0],1,y_size,fp_yuv); //Y
  164. fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv); //U
  165. fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv); //V
  166. }
  167. }
  168. av_free_packet(packet);
  169. }
  170. sws_freeContext(img_convert_ctx);
  171. fclose(fp_yuv);
  172. av_free(out_buffer);
  173. av_free(pFrameYUV);
  174. avcodec_close(pCodecCtx);
  175. avformat_close_input(&pFormatCtx);
  176. return 0;
  177. }