1. 程式人生 > >FFmpeg實現錄屏+錄音

FFmpeg實現錄屏+錄音

1、概述

最簡單的基於FFmpeg的AVDevice例子(螢幕錄製+聲音採集),視訊採用mpeg4編碼,音訊採用aac編碼,並生成mp4檔案,其中fifo是此程式的關鍵,此程式只是一個demo很多優化都沒做,僅供參考。

2、程式碼

  1. <pre name="code"class="cpp">/** 
  2.  *最簡單的基於FFmpeg的AVDevice例子(螢幕錄製+聲音採集),視訊採用mpeg4編碼,音訊採用aac編碼,並生成mp4檔案 
  3.  *繆國凱 MK 
  4.  *[email protected] 
  5.  */
  6. #include "stdafx.h"
  7. #ifdef  __cplusplus
  8. extern"C"
  9. {  
  10. #endif
  11. #include "libavcodec/avcodec.h"
  12. #include "libavformat/avformat.h"
  13. #include "libswscale/swscale.h"
  14. #include "libavdevice/avdevice.h"
  15. #include "libavutil/audio_fifo.h"
  16. #pragma comment(lib, "avcodec.lib")
  17. #pragma comment(lib, "avformat.lib")
  18. #pragma comment(lib, "avutil.lib")
  19. #pragma comment(lib, "avdevice.lib")
  20. #pragma comment(lib, "avfilter.lib")
  21. //#pragma comment(lib, "avfilter.lib")
  22. //#pragma comment(lib, "postproc.lib")
  23. //#pragma comment(lib, "swresample.lib")
  24. #pragma comment(lib, "swscale.lib")
  25. #ifdef __cplusplus
  26. };  
  27. #endif
  28. AVFormatContext *pFormatCtx_Video = NULL, *pFormatCtx_Audio = NULL, *pFormatCtx_Out = NULL;  
  29. AVCodecContext  *pCodecCtx_Video;  
  30. AVCodec         *pCodec_Video;  
  31. AVFifoBuffer    *fifo_video = NULL;  
  32. AVAudioFifo     *fifo_audio = NULL;  
  33. int VideoIndex, AudioIndex;  
  34. CRITICAL_SECTION AudioSection, VideoSection;  
  35. SwsContext *img_convert_ctx;  
  36. int frame_size = 0;  
  37. uint8_t *picture_buf = NULL, *frame_buf = NULL;  
  38. bool bCap = true;  
  39. DWORD WINAPI ScreenCapThreadProc( LPVOID lpParam );  
  40. DWORD WINAPI AudioCapThreadProc( LPVOID lpParam );  
  41. int OpenVideoCapture()  
  42. {  
  43.     AVInputFormat *ifmt=av_find_input_format("gdigrab");  
  44.     //這裡可以加引數開啟,例如可以指定採集幀率
  45.     AVDictionary *options = NULL;  
  46.     av_dict_set(&options, "framerate""15", NULL);  
  47.     //av_dict_set(&options,"offset_x","20",0);
  48.     //The distance from the top edge of the screen or desktop
  49.     //av_dict_set(&options,"offset_y","40",0);
  50.     //Video frame size. The default is to capture the full screen
  51.     //av_dict_set(&options,"video_size","320x240",0);
  52.     if(avformat_open_input(&pFormatCtx_Video, "desktop", ifmt, &options)!=0)  
  53.     {  
  54.         printf("Couldn't open input stream.(無法開啟視訊輸入流)\n");  
  55.         return -1;  
  56.     }  
  57.     if(avformat_find_stream_info(pFormatCtx_Video,NULL)<0)  
  58.     {  
  59.         printf("Couldn't find stream information.(無法獲取視訊流資訊)\n");  
  60.         return -1;  
  61.     }  
  62.     if (pFormatCtx_Video->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO)  
  63.     {  
  64.         printf("Couldn't find video stream information.(無法獲取視訊流資訊)\n");  
  65.         return -1;  
  66.     }  
  67.     pCodecCtx_Video = pFormatCtx_Video->streams[0]->codec;  
  68.     pCodec_Video = avcodec_find_decoder(pCodecCtx_Video->codec_id);  
  69.     if(pCodec_Video == NULL)  
  70.     {  
  71.         printf("Codec not found.(沒有找到解碼器)\n");  
  72.         return -1;  
  73.     }  
  74.     if(avcodec_open2(pCodecCtx_Video, pCodec_Video, NULL) < 0)  
  75.     {  
  76.         printf("Could not open codec.(無法開啟解碼器)\n");  
  77.         return -1;  
  78.     }  
  79.     img_convert_ctx = sws_getContext(pCodecCtx_Video->width, pCodecCtx_Video->height, pCodecCtx_Video->pix_fmt,   
  80.         pCodecCtx_Video->width, pCodecCtx_Video->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);   
  81.     frame_size = avpicture_get_size(pCodecCtx_Video->pix_fmt, pCodecCtx_Video->width, pCodecCtx_Video->height);  
  82.     //申請30幀快取
  83.     fifo_video = av_fifo_alloc(30 * avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx_Video->width, pCodecCtx_Video->height));  
  84.     return 0;  
  85. }  
  86. staticchar *dup_wchar_to_utf8(wchar_t *w)  
  87. {  
  88.     char *s = NULL;  
  89.     int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);  
  90.     s = (char *) av_malloc(l);  
  91.     if (s)  
  92.         WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);  
  93.     return s;  
  94. }  
  95. int OpenAudioCapture()  
  96. {  
  97.     //查詢輸入方式
  98.     AVInputFormat *pAudioInputFmt = av_find_input_format("dshow");  
  99.     //以Direct Show的方式開啟裝置,並將 輸入方式 關聯到格式上下文
  100.     char * psDevName = dup_wchar_to_utf8(L"audio=麥克風 (Realtek High Definition Au");  
  101.     if (avformat_open_input(&pFormatCtx_Audio, psDevName, pAudioInputFmt,NULL) < 0)  
  102.     {  
  103.         printf("Couldn't open input stream.(無法開啟音訊輸入流)\n");  
  104.         return -1;  
  105.     }  
  106.     if(avformat_find_stream_info(pFormatCtx_Audio,NULL)<0)    
  107.         return -1;   
  108.     if(pFormatCtx_Audio->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO)  
  109.     {  
  110.         printf("Couldn't find video stream information.(無法獲取音訊流資訊)\n");  
  111.         return -1;  
  112.     }  
  113.     AVCodec *tmpCodec = avcodec_find_decoder(pFormatCtx_Audio->streams[0]->codec->codec_id);  
  114.     if(0 > avcodec_open2(pFormatCtx_Audio->streams[0]->codec, tmpCodec, NULL))  
  115.     {  
  116.         printf("can not find or open audio decoder!\n");  
  117.     }  
  118.     return 0;  
  119. }  
  120. int OpenOutPut()  
  121. {  
  122.     AVStream *pVideoStream = NULL, *pAudioStream = NULL;  
  123.     constchar *outFileName = "test.mp4";  
  124.     avformat_alloc_output_context2(&pFormatCtx_Out, NULL, NULL, outFileName);  
  125.     if (pFormatCtx_Video->streams[0]->codec->codec_type == AVMEDIA_TYPE_VIDEO)  
  126.     {  
  127.         AVCodecContext *videoCodecCtx;  
  128.         VideoIndex = 0;  
  129.         pVideoStream = avformat_new_stream(pFormatCtx_Out, NULL);  
  130.         if (!pVideoStream)  
  131.         {  
  132.             printf("can not new stream for output!\n");  
  133.             return -1;  
  134.         }  
  135.         //set codec context param
  136.         pVideoStream->codec->codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);