FFmpeg實現錄屏+錄音
阿新 • • 發佈:2019-02-03
1、概述
最簡單的基於FFmpeg的AVDevice例子(螢幕錄製+聲音採集),視訊採用mpeg4編碼,音訊採用aac編碼,並生成mp4檔案,其中fifo是此程式的關鍵,此程式只是一個demo很多優化都沒做,僅供參考。2、程式碼
- <pre name="code"class="cpp">/**
- *最簡單的基於FFmpeg的AVDevice例子(螢幕錄製+聲音採集),視訊採用mpeg4編碼,音訊採用aac編碼,並生成mp4檔案
- *繆國凱 MK
- *[email protected]
- */
- #include "stdafx.h"
- #ifdef __cplusplus
- extern"C"
- {
- #endif
- #include "libavcodec/avcodec.h"
- #include "libavformat/avformat.h"
- #include "libswscale/swscale.h"
- #include "libavdevice/avdevice.h"
- #include "libavutil/audio_fifo.h"
- #pragma comment(lib, "avcodec.lib")
- #pragma comment(lib, "avformat.lib")
- #pragma comment(lib, "avutil.lib")
- #pragma comment(lib, "avdevice.lib")
- #pragma comment(lib, "avfilter.lib")
- //#pragma comment(lib, "avfilter.lib")
- //#pragma comment(lib, "postproc.lib")
- //#pragma comment(lib, "swresample.lib")
- #pragma comment(lib, "swscale.lib")
- #ifdef __cplusplus
- };
- #endif
- AVFormatContext *pFormatCtx_Video = NULL, *pFormatCtx_Audio = NULL, *pFormatCtx_Out = NULL;
- AVCodecContext *pCodecCtx_Video;
- AVCodec *pCodec_Video;
- AVFifoBuffer *fifo_video = NULL;
- AVAudioFifo *fifo_audio = NULL;
- int VideoIndex, AudioIndex;
- CRITICAL_SECTION AudioSection, VideoSection;
- SwsContext *img_convert_ctx;
- int frame_size = 0;
- uint8_t *picture_buf = NULL, *frame_buf = NULL;
- bool bCap = true;
- DWORD WINAPI ScreenCapThreadProc( LPVOID lpParam );
- DWORD WINAPI AudioCapThreadProc( LPVOID lpParam );
- int OpenVideoCapture()
- {
- AVInputFormat *ifmt=av_find_input_format("gdigrab");
- //這裡可以加引數開啟,例如可以指定採集幀率
- AVDictionary *options = NULL;
- av_dict_set(&options, "framerate", "15", NULL);
- //av_dict_set(&options,"offset_x","20",0);
- //The distance from the top edge of the screen or desktop
- //av_dict_set(&options,"offset_y","40",0);
- //Video frame size. The default is to capture the full screen
- //av_dict_set(&options,"video_size","320x240",0);
- if(avformat_open_input(&pFormatCtx_Video, "desktop", ifmt, &options)!=0)
- {
- printf("Couldn't open input stream.(無法開啟視訊輸入流)\n");
- return -1;
- }
- if(avformat_find_stream_info(pFormatCtx_Video,NULL)<0)
- {
- printf("Couldn't find stream information.(無法獲取視訊流資訊)\n");
- return -1;
- }
- if (pFormatCtx_Video->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO)
- {
- printf("Couldn't find video stream information.(無法獲取視訊流資訊)\n");
- return -1;
- }
- pCodecCtx_Video = pFormatCtx_Video->streams[0]->codec;
- pCodec_Video = avcodec_find_decoder(pCodecCtx_Video->codec_id);
- if(pCodec_Video == NULL)
- {
- printf("Codec not found.(沒有找到解碼器)\n");
- return -1;
- }
- if(avcodec_open2(pCodecCtx_Video, pCodec_Video, NULL) < 0)
- {
- printf("Could not open codec.(無法開啟解碼器)\n");
- return -1;
- }
- img_convert_ctx = sws_getContext(pCodecCtx_Video->width, pCodecCtx_Video->height, pCodecCtx_Video->pix_fmt,
- pCodecCtx_Video->width, pCodecCtx_Video->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
- frame_size = avpicture_get_size(pCodecCtx_Video->pix_fmt, pCodecCtx_Video->width, pCodecCtx_Video->height);
- //申請30幀快取
- fifo_video = av_fifo_alloc(30 * avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx_Video->width, pCodecCtx_Video->height));
- return 0;
- }
- staticchar *dup_wchar_to_utf8(wchar_t *w)
- {
- char *s = NULL;
- int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
- s = (char *) av_malloc(l);
- if (s)
- WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
- return s;
- }
- int OpenAudioCapture()
- {
- //查詢輸入方式
- AVInputFormat *pAudioInputFmt = av_find_input_format("dshow");
- //以Direct Show的方式開啟裝置,並將 輸入方式 關聯到格式上下文
- char * psDevName = dup_wchar_to_utf8(L"audio=麥克風 (Realtek High Definition Au");
- if (avformat_open_input(&pFormatCtx_Audio, psDevName, pAudioInputFmt,NULL) < 0)
- {
- printf("Couldn't open input stream.(無法開啟音訊輸入流)\n");
- return -1;
- }
- if(avformat_find_stream_info(pFormatCtx_Audio,NULL)<0)
- return -1;
- if(pFormatCtx_Audio->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
- {
- printf("Couldn't find video stream information.(無法獲取音訊流資訊)\n");
- return -1;
- }
- AVCodec *tmpCodec = avcodec_find_decoder(pFormatCtx_Audio->streams[0]->codec->codec_id);
- if(0 > avcodec_open2(pFormatCtx_Audio->streams[0]->codec, tmpCodec, NULL))
- {
- printf("can not find or open audio decoder!\n");
- }
- return 0;
- }
- int OpenOutPut()
- {
- AVStream *pVideoStream = NULL, *pAudioStream = NULL;
- constchar *outFileName = "test.mp4";
- avformat_alloc_output_context2(&pFormatCtx_Out, NULL, NULL, outFileName);
- if (pFormatCtx_Video->streams[0]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
- {
- AVCodecContext *videoCodecCtx;
- VideoIndex = 0;
- pVideoStream = avformat_new_stream(pFormatCtx_Out, NULL);
- if (!pVideoStream)
- {
- printf("can not new stream for output!\n");
- return -1;
- }
- //set codec context param
- pVideoStream->codec->codec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);