资源简介

ffmpeg实现读取摄像头麦克,编码保存mp4文件,参考了雷神的博客,在自己电脑上调试通过,代码作了优化,流程更加清晰, 能跑,供学习用。遇到任何问题,请联系

资源截图

代码片段和文件信息

#ifdef	__cplusplus
#include
#include
#include
#define __STDC_CONSTANT_MACROS
#define AV_CODEC_FLAG_GLOBAL_HEADER (1 << 22)
#define CODEC_FLAG_GLOBAL_HEADER AV_CODEC_FLAG_GLOBAL_HEADER
#define AVFMT_RAWPICTURE 0x0020
extern “C“
{
#endif
#include “libavcodec/avcodec.h“
#include “libavformat/avformat.h“
#include “libswscale/swscale.h“
#include “libavdevice/avdevice.h“
#include “libavutil/audio_fifo.h“
#include “libswresample/swresample.h“
#include “SDL/SDL.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 “SDL.lib“)
#pragma comment(lib “SDLmain.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 );

#define SFM_REFRESH_EVENT  (SDL_USEREVENT + 1)

#define SFM_BREAK_EVENT  (SDL_USEREVENT + 2)

int thread_exit=0;

void CapCamera(AVframe* AVframe*);

int sfp_refresh_thread(void *opaque)
{
thread_exit=0;
while (!thread_exit) {
SDL_Event event;
event.type = SFM_REFRESH_EVENT;
SDL_PushEvent(&event);
SDL_Delay(33);
}
thread_exit=0;
//Break
SDL_Event event;
event.type = SFM_BREAK_EVENT;
SDL_PushEvent(&event);

return 0;
}

int OpenVideoCapture()
{
//AVInputFormat *ifmt=av_find_input_format(“gdigrab“);
AVInputFormat *ifmt=av_find_input_format(“dshow“);
//这里可以加参数打开,例如可以指定采集帧率
AVDictionary *options = NULL;
av_dict_set(&options “framerate“ “30“ 0);
//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)
    if(avformat_open_input(&pFormatCtx_Video “video=Integrated Camera“ ifmt &options)!=0)
{
printf(“Couldn‘t open input stream.(无法打开视频输入流)\n“);
return -1;
}
if(avformat_find_stream_info(pFormatCtx_VideoNULL)<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 str

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-12-07 21:27  ffmpeg-20181206-win32-dev\
     文件       35823  2018-12-07 05:23  ffmpeg-20181206-win32-dev\LICENSE.txt
     文件        4460  2018-12-07 05:23  ffmpeg-20181206-win32-dev\README.txt
     目录           0  2019-01-28 22:33  ffmpeg-20181206-win32-dev\bin\
     文件      303616  2016-03-12 12:51  ffmpeg-20181206-win32-dev\bin\SDL.dll
     文件    31714304  2018-12-07 05:23  ffmpeg-20181206-win32-dev\bin\avcodec-58.dll
     文件     1305600  2018-12-07 05:23  ffmpeg-20181206-win32-dev\bin\avdevice-58.dll
     文件     7165440  2018-12-07 05:23  ffmpeg-20181206-win32-dev\bin\avfilter-7.dll
     文件     8583168  2018-12-07 05:23  ffmpeg-20181206-win32-dev\bin\avformat-58.dll
     文件      669696  2018-12-07 05:23  ffmpeg-20181206-win32-dev\bin\avutil-56.dll
     文件      114688  2018-12-07 05:23  ffmpeg-20181206-win32-dev\bin\postproc-55.dll
     文件      311808  2018-12-07 05:23  ffmpeg-20181206-win32-dev\bin\swresample-3.dll
     文件      515584  2018-12-07 05:23  ffmpeg-20181206-win32-dev\bin\swscale-5.dll
     目录           0  2019-01-08 21:20  ffmpeg-20181206-win32-dev\ffmpeg_test\
     目录           0  2019-01-06 22:54  ffmpeg-20181206-win32-dev\ffmpeg_test\ffmpeg_test\
     文件       48532  2019-01-06 16:29  ffmpeg-20181206-win32-dev\ffmpeg_test\ffmpeg_test\AudioVideoCapture.cpp
     目录           0  2019-01-28 22:34  ffmpeg-20181206-win32-dev\ffmpeg_test\ffmpeg_test\Debug\
     文件        4264  2019-01-06 15:35  ffmpeg-20181206-win32-dev\ffmpeg_test\ffmpeg_test\ffmpeg_test.vcproj
     文件        1411  2019-01-28 22:32  ffmpeg-20181206-win32-dev\ffmpeg_test\ffmpeg_test\ffmpeg_test.vcproj.yinq-PC.yinq.user
     文件    13691904  2019-01-28 22:32  ffmpeg-20181206-win32-dev\ffmpeg_test\ffmpeg_test.ncb
     文件        2299  2019-01-08 21:24  ffmpeg-20181206-win32-dev\ffmpeg_test\ffmpeg_test.sln
     文件       61952  2019-01-28 22:32  ffmpeg-20181206-win32-dev\ffmpeg_test\ffmpeg_test.suo
     目录           0  2018-12-11 22:06  ffmpeg-20181206-win32-dev\include\
     目录           0  2018-12-11 21:53  ffmpeg-20181206-win32-dev\include\SDL\
     文件        3233  2016-03-12 12:51  ffmpeg-20181206-win32-dev\include\SDL\SDL.h
     文件        1933  2016-03-12 12:51  ffmpeg-20181206-win32-dev\include\SDL\SDL_active.h
     文件       11215  2016-03-12 12:51  ffmpeg-20181206-win32-dev\include\SDL\SDL_audio.h
     文件         986  2016-03-12 12:51  ffmpeg-20181206-win32-dev\include\SDL\SDL_byteorder.h
     文件        6048  2016-03-12 12:51  ffmpeg-20181206-win32-dev\include\SDL\SDL_cdrom.h
     文件        1474  2016-03-12 12:51  ffmpeg-20181206-win32-dev\include\SDL\SDL_config.h
     文件        2803  2016-03-12 12:51  ffmpeg-20181206-win32-dev\include\SDL\SDL_config_dreamcast.h
............此处省略170个文件信息

评论

共有 条评论