本文來詳細講解一下:如何在Mac、Windows環境下成功編譯FFmpeg。
目標
這里先提前說明一下,最后希望達到的效果:
- 編譯出ffmpeg、ffprobe、ffplay三個命令行工具
- 只產生動態庫,不產生靜態庫
- 將fdk-aac、x264、x265集成到FFmpeg中
- x264、x265會在以后講解的視頻模塊中用到
下載源碼
下載源碼ffmpeg-4.3.2.tar.xz,然后解壓。
Mac編譯
依賴項
- brew install yasm
- ffmpeg的編譯過程依賴yasm
- 若未安裝yasm會出現錯誤:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
- brew install sdl2
- ffplay依賴於sdl2
- 如果缺少sdl2,就無法編譯出ffplay
- brew install fdk-aac
- 不然會出現錯誤:ERROR: libfdk_aac not found
- brew install x264
- 不然會出現錯誤:ERROR: libx264 not found
- brew install x265
- 不然會出現錯誤:ERROR: libx265 not found
其實x264、x265、sdl2都在曾經執行brew install ffmpeg的時候安裝過了。
- 可以通過brew list的結果查看是否安裝過
- brew list | grep fdk
- brew list | grep x26
- brew list | grep -E 'fdk|x26'
- 如果已經安裝過,可以不用再執行brew install
configure
首先進入源碼目錄。
# 我的源碼放在了Downloads目錄下
cd ~/Downloads/ffmpeg-4.3.2
然后執行源碼目錄下的configure腳本,設置一些編譯參數,做一些編譯前的准備工作。
./configure --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
-
--prefix
- 用以指定編譯好的FFmpeg安裝到哪個目錄
- 一般放到/usr/local/ffmpeg中即可
-
--enable-shared
- 生成動態庫
-
--disable-static
- 不生成靜態庫
-
--enable-libfdk-aac
- 將fdk-aac內置到FFmpeg中
-
--enable-libx264
- 將x264內置到FFmpeg中
-
--enable-libx265
- 將x265內置到FFmpeg中
-
--enable-gpl
- x264、x265要求開啟GPL License
-
--enable-nonfree
- fdk-aac與GPL不兼容,需要通過開啟nonfree進行配置
你可以通過configure --help命令查看每一個配置項的作用。
./configure --help | grep static
# 結果如下所示
--disable-static do not build static libraries [no]
編譯
接下來開始解析源代碼目錄中的Makefile文件,進行編譯。-j8表示允許同時執行8個編譯任務。
make -j8
對於經常在類Unix系統下接觸C/C++開發的小伙伴來說,Makefile必然是不陌生的。這里給不了解Makefile的小伙伴簡單科普一下:
- Makefile描述了整個項目的編譯和鏈接等規則
- 比如哪些文件需要編譯?哪些文件不需要編譯?哪些文件需要先編譯?哪些文件需要后編譯?等等
- Makefile可以使項目的編譯變得自動化,不需要每次都手動輸入一堆源文件和參數
- 比如原來需要這么寫:gcc test1.c test2.c test3.c -o test
安裝
將編譯好的庫安裝到指定的位置:/usr/local/ffmpeg。
make install
安裝完畢后,/usr/local/ffmpeg的目錄結構如下所示。
配置PATH
為了讓bin目錄中的ffmpeg、ffprobe、ffplay在任意位置都能夠使用,需要先將bin目錄配置到環境變量PATH中。
# 編輯.zprofile
vim ~/.zprofile
# .zprofile文件中寫入以下內容
export PATH=/usr/local/ffmpeg/bin:$PATH
# 讓.zprofile生效
source ~/.zprofile
如果你用的是bash,而不是zsh,只需要將上面的.zprofile換成.bash_profile。
驗證
接下來,在命令行上進行驗證。
ffmpeg -version
# 結果如下所示
ffmpeg version 4.3.2 Copyright (c) 2000-2021 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
此時,你完全可以通過brew uninstall ffmpeg卸載以前安裝的FFmpeg。
Windows編譯
configure、Makefile這一套工具是用在類Unix系統上的(Linux、Mac等),默認無法直接用在Windows上。
這里介紹其中一種可行的解決方案:
- 使用MSYS2軟件在Windows上模擬出Linux環境
- 結合使用MinGW對FFmpeg進行編譯
下載安裝MSYS2
進入MSYS2官網下載安裝包(我這邊下載的是:msys2-x86_64-20210228.exe),然后進行安裝。
安裝完畢后打開命令行工具mingw64.exe。
安裝依賴
pacman(Package Manager)是一個包管理工具。
- pacman -Sl:搜索有哪些包可以安裝
- pacman -S:安裝
- pacman -R:卸載
# 查看是否有fdk、SDL2相關包(E表示跟一個正則表達式,i表示不區分大小寫)
pacman -Sl | grep -Ei 'fdk|sdl2'
# 結果如下所示
mingw32 mingw-w64-i686-SDL2 2.0.14-2
mingw32 mingw-w64-i686-SDL2_gfx 1.0.4-1
mingw32 mingw-w64-i686-SDL2_image 2.0.5-1
mingw32 mingw-w64-i686-SDL2_mixer 2.0.4-2
mingw32 mingw-w64-i686-SDL2_net 2.0.1-1
mingw32 mingw-w64-i686-SDL2_ttf 2.0.15-1
mingw32 mingw-w64-i686-fdk-aac 2.0.1-1
mingw64 mingw-w64-x86_64-SDL2 2.0.14-2
mingw64 mingw-w64-x86_64-SDL2_gfx 1.0.4-1
mingw64 mingw-w64-x86_64-SDL2_image 2.0.5-1
mingw64 mingw-w64-x86_64-SDL2_mixer 2.0.4-2
mingw64 mingw-w64-x86_64-SDL2_net 2.0.1-1
mingw64 mingw-w64-x86_64-SDL2_ttf 2.0.15-1
mingw64 mingw-w64-x86_64-fdk-aac 2.0.1-1
接下來,安裝各種依賴包。
# 編譯工具鏈
pacman -S mingw-w64-x86_64-toolchain
pacman -S mingw-w64-x86_64-yasm
pacman -S mingw-w64-x86_64-SDL2
pacman -S mingw-w64-x86_64-fdk-aac
pacman -S mingw-w64-x86_64-x264
pacman -S mingw-w64-x86_64-x265
# 需要單獨安裝make
pacman -S make
關於軟件包相關的默認路徑:
- 下載目錄:%MSYS2_HOME%/var/cache/pacman/pkg
- 安裝目錄:%MSYS2_HOME%/mingw64
- %MSYS2_HOME%是指MSYS2的安裝目錄
configure
我的源碼是放在F:/Dev/ffmpeg-4.3.1,輸入cd /f/dev/ffmpeg-4.3.1即可進入源碼目錄。然后執行configure。
./configure --prefix=/usr/local/ffmpeg --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-libfdk-aac --enable-libx264 --enable-libx265
編譯、安裝
make -j8 && make install
FFmpeg最終會被安裝到%MSYS2_HOME%/usr/local/ffmpeg目錄中。
bin
此時bin目錄中的ffmpeg、ffprobe、ffplay還是沒法使用的,因為缺少相關的dll,需要從%MSYS2_HOME%/mingw64/bin中拷貝,或者將%MSYS2_HOME%/mingw64/bin配置到環境變量Path中。
需要拷貝的dll有:libwinpthread-1、SDL2、zlib1.dll、liblzma-5、libbz2-1、libiconv-2、libgcc_s_seh-1、libstdc++-6、libx265、libx264-159、libfdk-aac-2。
Path
最后建議將%FFMPEG_HOME%/bin目錄配置到環境變量Path中。
在命令行輸入ffmpeg -version,一切大功告成!