準備工作
在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。