本文為作者原創,轉載請注明出處:https://www.cnblogs.com/leisure_chn/p/10035365.html
1. 相關資源介紹
本文主要講述 linux 平台 x86(及x86-64) 架構下的 ffmpeg 編譯安裝過程。
其他嵌入式平台需要交叉編譯,過程類似,不詳述。
本實驗在 opensuse 和 ubuntu 兩個平台作了驗證。使用lsb_release -a
命令查看系統發行版版本:
opensuse 平台版本:openSUSE Leap 15.1。
ubuntu 平台版本:Ubuntu 16.04.5 LTS。
1.1 ffmpeg
ffmpeg官網:https://www.ffmpeg.org/
1.2 SDL
SDL(Simple DirectMedia Layer) 是一套開源的跨平台多媒體開發庫。SDL提供了數種控制圖像、聲音、輸出輸入的函數,封裝了復雜的視音頻底層操作,簡化了視音頻處理的難度。目前SDL多用於開發游戲、模擬器、媒體播放器等多媒體應用領域。
SDL 官網:https://www.libsdl.org/
1.3 yasm/nasm
舊版 ffmpeg 及 x264/x265 使用 yasm 匯編器
Yasm 是英特爾x86架構下的一個匯編器和反匯編器。Yasm 是一個完全重寫的 Netwide 匯編器(NASM)。Yasm 通常可以與 NASM 互換使用,並支持 x86 和 x86-64 架構。其許可協議為修訂過的 BSD 許可證。
此處 Yasm 用來編譯 x86 平台下 ffmpeg 中部分匯編代碼。
注意,Yasm 是 x86 平台匯編器,不需要交叉編譯。若是 arm 等其他平台,交叉編譯工具鏈中包含有對應的匯編器,則交叉編譯 ffmpeg 時需要 --disable-yasm 選項。
Yasm 官網:http://yasm.tortall.net/
新版 ffmpeg 及 x264/x265 改用 nasm 匯編器
Netwide Assembler (簡稱NASM) 是一款基於英特爾 x86 架構的匯編與反匯編工具。NASM 被認為是 Linux 平台上最受歡迎的匯編工具之一。
注意,NASM 是 x86 平台匯編器,不需要交叉編譯。若是 arm 等其他平台,交叉編譯工具鏈中包含有對應的匯編器,則交叉編譯 ffmpeg 時需要 --disable-x86asm 選項。
NASM 官網:https://www.nasm.us/
1.4 x264
x264 是開源的 h264 編碼器,使用非常廣泛,綜合性能不比商業編解碼器差。
x264 官網:https://www.videolan.org/developers/x264.html
ffmpeg 工程中實現了 h264 解碼器,但無 h264 編碼器。因此需要安裝第三方編碼器 x264
1.5 x265
x265 是開源的 h265 編碼器。
x265 官網:http://www.x265.org/
下載地址一:https://bitbucket.org/multicoreware/x265/downloads/
下載地下二:https://www.videolan.org/developers/x265.html
ffmpeg 工程中實現了 h265 解碼器,但無 h265 編碼器。因此需要安裝第三方編碼器 x265
1.6 libmp3lame
libmp3lame 是開源的 mp3 編碼器。
libmp3lame 官網:http://lame.sourceforge.net/
1.7 librtmp
librtmp: RTMPDump Real-Time Messaging Protocol API。
librtmp 又稱 rtmpdump,是用於處理 RTMP 流的工具。支持所有形式的 RTMP,包括 rtmp://, rtmpt://, rtmpe://, rtmpte://, 和 rtmps://。
librtmp 文檔:http://rtmpdump.mplayerhq.hu/librtmp.3.html
librtmp 官網:http://rtmpdump.mplayerhq.hu/
2. 編譯安裝過程
2.1 規划安裝路徑
將編譯源碼得到的程序資源安裝到用戶目錄 /home/think 下。則安裝后,/home/think 目錄下會多出 bin、include、lib、share 等目錄
配置環境變量
編輯 /etc/profile,添加如下幾行:
export PATH=/home/think/bin:$PATH
export LIBRARY_PATH=/home/think/lib:/home/think/lib64:$LIBRARY_PATH
export PKG_CONFIG_PATH=/home/think/lib/pkgconfig:$PKG_CONFIG_PATH
export C_INCLUDE_PATH=/home/think/include:$C_INCLUDE_PATH
上述幾個環境變量是程序編譯時需要用到的庫文件、頭文件路徑,以及可執行程序所在路徑。
在命令行中運行如下命令,使新設置的環境變量立即生效:
source /etc/profile
配置動態庫路徑
編輯 /etc/ld.so.conf,添加如下兩行:
/home/think/lib
/home/think/lib64
ld.so.conf 中的內容是程序運行時需要搜索的動態庫路徑。
在命令行中運行如下命令,使新設置的動態庫路徑立即生效:
ldconfig
2.2 SDL
注意:應先安裝 SDL,再安裝 ffmpeg,否則 ffmpeg 編譯時不會生成 ffplay
原因如參考資料[3]所述。
兩種安裝方式,推薦第二種方式,可能遇到的問題比較少
編譯源碼安裝(不推薦)
在 SDL 官網 https://www.libsdl.org/ 下載最新源碼包 SDL2-2.0.10.tar.gz
tar -zxvf SDL2-2.0.10.tar.gz
cd SDL2-2.0.10/
./configure --prefix=/home/think
make
make install
通過編譯源碼安裝的方式,編譯安裝成功后運行 ffplay 可能會遇到挺多問題,參 “4. 問題描述”
通過軟件源在線安裝(推薦)
opensuse 平台:
zypper install libSDL2-devel
zypper install libSDL2_image-devel
zypper install libSDL2_mixer-devel
zypper install libSDL2_ttf-devel
zypper install libSDL2_gfx-devel
ubuntu 平台:
apt-get install libsdl2-dev
apt-get install libsdl2-image-dev
apt-get install libsdl2-mixer-dev
apt-get install libsdl2-ttf-dev
apt-get install libsdl2-gfx-dev
2.3 yasm/nasm
舊版 ffmpeg 及 x264/x265 使用 yasm 匯編器
在官網下載頁面 http://yasm.tortall.net/Download.html 下載最新版源碼 yasm-1.3.0.tar.gz
tar -zxvf yasm-1.3.0.tar.gz
cd yasm-1.3.0/
./configure --prefix=/home/think
make
make install
新版 ffmpeg 及 x264/x265 改用 nasm 匯編器
具體從哪一版本開始改用nasm不太清楚。在官網 https://www.nasm.us/ 下載最新版源碼 nasm-2.14.02.tar.bz2
tar -zxvf nasm-2.14.02.tar.gz
cd nasm-2.14/
./configure --prefix=/home/think
make
make install
2.4 x264
在網址 https://www.videolan.org/developers/x264.html 下載源碼包 last_x264.tar.bz2,這是 git 倉庫的 master 分支源碼。我們輸入以下地址:http://download.videolan.org/x264/snapshots/,可以選擇最新的 stable 版本源碼包進行下載,當前最新版本為 x264-snapshot-20191217-2245-stable.tar.bz2。
tar -jxvf x264-snapshot-20191217-2245-stable.tar.bz2
cd x264-snapshot-20191217-2245-stable/
./configure --prefix=/home/think --enable-shared --enable-static
make
make install
注意第 4 行配置選項中,未給出 --disable-asm 選項,則表示啟用匯編選項
2.5 x265
在網址 https://bitbucket.org/multicoreware/x265/downloads/ 下載源碼包 x265_3.0.tar.gz
編譯說明參照 https://bitbucket.org/multicoreware/x265/wiki/Home
tar -zxvf x265_3.3.tar.gz
cd x265_3.3/build/linux/
./make-Makefiles.bash
在上一行命令運行快結束時,出現 cmake 配置信息編輯界面,將 CMAKE_INSTALL_PREFIX 的值改成 /home/think
make
make install
2.6 libmp3lame
在官網 http://lame.sourceforge.net/ 下載最新源碼 lame-3.100.tar.gz
tar -zxvf lame-3.100.tar.gz
cd lame-3.100
./configure --prefix=/home/think
make
make install
2.7 librtmp
根據官網 http://rtmpdump.mplayerhq.hu/ 說明,通過 git 下載源碼。
編譯安裝方法參考源碼目錄下 README 文件和 Makefile 文件。
git clone git://git.ffmpeg.org/rtmpdump
cd rtmpdump
make CRYPTO=GNUTLS
make install prefix=/home/think
2.8 ffmpeg
在官網 https://www.ffmpeg.org/ 下載最新版源碼包。或者使用 git 克隆 ffmpeg 源碼倉庫。
ffmpeg 源碼倉庫地址 https://git.ffmpeg.org/ffmpeg.git,在 github 上鏡像地址 https://github.com/FFmpeg/FFmpeg.git。
git clone https://git.ffmpeg.org/ffmpeg.git
cd ffmpeg
git tag
git checkout -b n4.2.2 n4.2.2
./configure --prefix=/home/think \
--enable-shared --enable-static --enable-gpl --enable-pthreads \
--enable-libx264 --enable-libx265 --enable-libmp3lame --enable-librtmp \
--extra-cflags=-I/home/think/include --extra-ldflags=-L/home/think/lib
make
make install
"./configure" 選項中 "--extra-cflags=-I/home/think/include --extra-ldflags=-L/home/think/lib" 是指定 ffmpeg 編譯時需要的第三方庫 (libx264 等) 的頭文件目錄和庫文件目錄。因為我們前面將所有第三方庫的安裝路徑都設置為 "/home/think/" 目錄。
如果機器上已經編譯安裝過 ffmpeg,需要再次編譯安裝時,需要先卸載舊版本的頭文件和庫文件,否則編譯時可能優先使用已安裝的舊的頭文件或庫文件,導致編譯失敗。
./configure --prefix=/home/think
make uninstall
如果是全新安裝,不必執行上述卸載命令。
3. 測試
測試文件下載(右鍵另存為):huangh.flv
在命令行中運行如下測試命令:
ffmpeg -i huangh.flv -c copy huangh.ts
ffplay huangh.flv
ffprobe huangh.flv
注意:
遠程終端處於控制台命令行模式(運行級別3),無權限調用 SDL,因此無法測試 ffplay,但可以測試 ffmpeg 和 ffprobe。測試 ffplay 需要 X11 控制台模式(運行級別5,即 GUI 圖形模式)。
4. 問題記錄
4.1 No available video device
錯誤提示:
Could not initialize SDL - No available video device
(Did you set the DISPLAY variable?)
原因分析:
參考資料[4]https://blog.csdn.net/huanghai381/article/details/51777446
解決方法:
a) 安裝x11的庫文件:
opensuse平台:
zypper install libX11-devel
zypper install xorg-x11-devel
ubuntu平台:
apt-get install libx11-dev
apt-get install xorg-dev
b) 重新編譯安裝SDL
4.2 Audio target 'pulse' not available
錯誤提示:
Could not initialize SDL - Audio target 'pulse' not available
(Did you set the DISPLAY variable?)
原因分析:
參考資料[5]http://forums.libsdl.org/viewtopic.php?t=7609
解決方法:
a) 安裝缺少的庫
opensuse 平台:
zypper install libpulse-devel
zypper install libasound2
zypper install libasound2-devel // 實測不安裝此包也無問題,若軟件源中無此包則不必安裝
ubuntu 平台:
apt-get install libpulse-dev
apt-get install libasound2
apt-get install libasound2-dev
b) 重新編譯安裝 SDL
4.3 x265 not found using pkg-config
錯誤提示:
編譯ffmpeg,運行./configure --enable-libx265 ...
出現如下錯誤提示:
x265 not found using pkg-config
原因分析:
參考資料[6]https://stackoverflow.com/questions/51918409/compiling-ffmpeg-x265-not-found-using-pkg-config
解決方法:
export PKG_CONFIG_PATH=/home/think/lib/pkgconfig:$PKG_CONFIG_PATH
5. 參考資料
[1] “ffmpeg 編譯”, https://blog.csdn.net/season_hangzhou/article/details/24399371
[2] “ffmpeg 編譯”,https://www.cnblogs.com/HongyunL/p/5243096.html
[3] “ffmpeg 編譯未生成ffplay”, http://blog.chinaunix.net/uid-11344913-id-3936227.html
[4] “SDL 失敗:無有效視頻設備”,https://blog.csdn.net/huanghai381/article/details/51777446
[5] “SDL 失敗:無有效音頻設備”,http://forums.libsdl.org/viewtopic.php?t=7609
[6] “x265 not found using pkg-config”,https://stackoverflow.com/questions/51918409/compiling-ffmpeg-x265-not-found-using-pkg-config
[7] “configure, pkg-config”,https://www.cnblogs.com/wliangde/p/3807532.html
[8] “Yasm”, https://zh.wikipedia.org/wiki/Yasm
[9] “NASM”, https://zh.wikipedia.org/wiki/Netwide_Assembler
6. 修改記錄
2018-11-20 1.0 初稿
2019-03-26 1.1 增加 librtmp 庫。ffmpeg 版本 4.1 升級至 4.1.2
2019-04-04 1.2 增加 libmp3lame 庫
2020-02-22 1.3 ffmpeg 版本 4.1.2 升級至 4.2.2