准备工作
在Windows平台上最强大的IDE非Visual Studio莫属了,虽然本人也非常喜欢并经常用Clion写一些小程序,鉴于VS的通用性还是选择使用VS来学习FFmpeg开发,可以使用免费的VS2017 Comminity社区版。
然后要下载FFmpeg Windows平台的开发工具,可以点这里下载 Dev版本。
把下载下来的incldue和lib目录放到VS工程目录下。开发需要.h头文件和.lib静态库文件。
配置VS2017
打开工程属性页面(不是解决方案属性)
1. 【属性】 - 【C/C++】 - 【常规】 - 【附加包含目录】添加头文件目录../include;
2. 【属性】 - 【链接器】 - 【常规】 - 【附加库目录】添加静态库目录../lib;
3. 【属性】 - 【链接器】 - 【输入】 - 【附加依赖项】添加要使用的静态库文件,方便起见可以全部加上,avcodec.lib; avformat.lib; avutil.lib; swscale.lib; swresample.lib; postproc.lib; avfilter.lib; avdevice.lib;
。
英文版对应目录为:
- Properties - C/C++ - General - Additional Include Directories
../include;%(AdditionalIncludeDirectories)
- Properties - Linker - General - Additional Library Directories
../include;%(AdditionalIncludeDirectories)
- Properties - Linker - Input - Additional Dependencies
avcodec.lib;avformat.lib;avutil.lib;swscale.lib;swresample.lib;postproc.lib;avfilter.lib;avdevice.lib;%(AdditionalDependencies)
测试工程
新建main.cpp源文件,添加头文件应该使用extern "C" {}
这种方式把#include
包起来,注意main函数的参数int main(int argc, char *argv[])
。
main.cpp
extern "C" {
#include <libavformat/avformat.h>
};
int main(int argc, char *argv[])
{
AVFormatContext *fmtCtx = NULL;
char *inputFile = "../assets/Sample.mkv";
// Register all components
av_register_all();
// Open an input stream and read the header.
if (avformat_open_input(&fmtCtx, inputFile, NULL, NULL) < 0) {
printf("Failed to open an input file");
return -1;
}
// Read packets of a media file to get stream information.
if (avformat_find_stream_info(fmtCtx, NULL) < 0) {
printf("Failed to retrieve input stream information");
return -1;
}
// Print detailed information about the input or output format
av_dump_format(fmtCtx, 0, 0, 0);
avformat_close_input(&fmtCtx);
return 0;
}
编译执行时会提示缺少相应的.dll动态库文件,直接去上面的网站下载Shared版本的dll文件放到程序执行目录就可以了。
具体工程可以参考: https://github.com/lmshao/FFmpeg-Basic。