加班次數頻繁,已經快一年沒有寫博了。由於此次在項目中使用到了 FFmpeg 來處理音視頻,顧記錄下在 Linux 系統中源碼安裝 FFmpeg 以便不時之需。
1. 安裝 yasm
yasm 是一個匯編編譯器。FFmpeg 編譯中為了提高編譯速度,使用了匯編命令,如果不想安裝此功能,則只需在后續安裝 FFmpeg 中使用 ./configure –disable-yasm 選項。
下載地址:http://yasm.tortall.net/Download.html
安裝命令:
(1)上傳下載的 yasm 文件 yasm-1.3.0.tar.gz,本人上傳至 /home/ffmpeg/ 文件目錄中。
(2)解壓文件並安裝
-bash-4.2# cd /home/ffmpeg/ -bash-4.2# tar zxvf yasm-1.3.0.tar.gz
-bash-4.2# cd yasm-1.3.0
-bash-4.2# ./configure
-bash-4.2# make
-bash-4.2# make install
2. 安裝 FFmpeg
下載地址:https://ffmpeg.org/download.html
安裝命令:
(1)解壓安裝包並進入該目錄
-bash-4.2# tar -jxvf ffmpeg-4.1.tar.bz2 -bash-4.2# cd ffmpeg-4.1
(2)配置項,具體配置項見文末
-bash-4.2# ./configure --enable-shared --enable-swscale --enable-gpl --enable-nonfree --enable-pic --prefix=/usr/local/ffmpeg --enable-version3 --enable-postproc --enable-pthreads --enable-avisynth
–enable-shared 表示生成動態鏈接庫,可以供以后編程使用,同時生成的可執行程序也依賴這些動態庫。如果不加上 –enable-shared 選項則使用靜態鏈接的方式編譯,此時不會生成動態庫,同時生成的 FFmpeg 等的可執行文件也比較大,但他們不需要動態庫就可以直接運行。
–prefix 表示程序安裝的目錄,這里設為 /usr/local/ffmpeg。
(3)編譯安裝
編譯需要等待一段的時間,但編譯成功后安裝也就很快完成。
-bash-4.2# make -bash-4.2# make install
3. 路徑配置
為了方便在各個路徑下使用 ffmpeg 命令,需配置一些路徑,包括加入系統環境變量。
安裝完成后將會在 /usr/local/ffmpeg 目錄下出現三個文件目錄:
-bash-4.2# cd /usr/local/ffmpeg/ -bash-4.2# ls bin include lib share
- bin:可執行文件目錄
- lib:動態鏈接庫目錄
- include:編程用到的頭文件目錄
(1)添加動態庫路徑
通過打開 /etc/ld.so.conf 文件發現只有 include ld.so.conf.d/*.conf 一句。
-bash-4.2# vi /etc/ld.so.conf include ld.so.conf.d/*.conf
表明其包含了 ld.so.conf.d 下所有的 conf 文件,於是可以在 /etc/ld.so.conf.d/ 創建一個新的文件 ffmpeg.conf,其中包含一句話,即為 ffmpeg 的 lib 目錄。
-bash-4.2# cd /etc/ld.so.conf.d/ -bash-4.2# vi ffmpeg.conf /usr/local/ffmpeg/lib
路徑 /usr/local/ffmpeg/lib 為新加的配置文件 ffmpeg.conf 中的內容。
執行 ldconfig -v 命令,更新 ld.so.cache,使修改立即生效:
-bash-4.2# ldconfig -v
(2)創建軟連接
相當於 windows 中的創建快捷方式:
-bash-4.2# ln -s /usr/local/ffmpeg/bin/ffmpeg /usr/local/bin/ -bash-4.2# ln -s /usr/local/ffmpeg/bin/ffprobe /usr/local/bin/ -bash-4.2# ln -s /usr/local/ffmpeg/bin/ffserver /usr/local/bin/
(3)頭文件拷貝
路徑 /usr/local/ffmpeg/include/ 包含的文件信息如下:
-bash-4.2# cd /usr/local/ffmpeg/include/ -bash-4.2# ls libavcodec libavfilter libavutil libswresample libavdevice libavformat libpostproc libswscale
/usr/local/ffmpeg/include 目錄下創建 ffmpeg 目錄:
-bash-4.2# mkdir ffmpeg
將 /usr/local/ffmpeg/include 路徑下所有目錄復制到 ffmpeg 目錄中:
-bash-4.2# cp -r libswscale/ /usr/local/ffmpeg/include/ffmpeg/ -bash-4.2# cp -r libswresample/ /usr/local/ffmpeg/include/ffmpeg/ -bash-4.2# cp -r libpostproc/ /usr/local/ffmpeg/include/ffmpeg/ -bash-4.2# cp -r libavutil/ /usr/local/ffmpeg/include/ffmpeg/ -bash-4.2# cp -r libavformat/ /usr/local/ffmpeg/include/ffmpeg/ -bash-4.2# cp -r libavfilter/ /usr/local/ffmpeg/include/ffmpeg/ -bash-4.2# cp -r libavcodec/ /usr/local/ffmpeg/include/ffmpeg/ -bash-4.2# cp -r libavdevice/ /usr/local/ffmpeg/include/ffmpeg/
將 /usr/local/ffmpeg/include 路徑下 ffmpeg/ 目錄復制到 /usr/include/:
-bash-4.2# cp -r ffmpeg/ /usr/include/
(4)PKG_CONFIG_PATH 變量設置
ffmpeg 安裝程序會在 /usr/local/ffmpeg/lib/pkgconfig 目錄下提供 libavcodec.pc libswscale.pc 等5個.pc文件,分別對應安裝的五個庫文件。必須要讓 pkg-config 能找到 ffmpeg 的 *.pc 文件,通過 PKG_CONFIG_PATH 來設置 pkg-config 來更新。
-bash-4.2# cd /usr/local/ffmpeg/lib/pkgconfig/ -bash-4.2# vim /etc/profile
添加如下配置:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/local/ffmpeg/lib/pkgconfig export FFMPEG_HOME=/usr/local/ffmpeg export PATH=$PATH:$FFMPEG_HOME
使配置立即生效:
-bash-4.2# source /etc/profile
(5)檢查配置是否成功
在命令行輸入 ffmpeg 查看是否安裝成功,若出現如下界面則安裝成功:
-bash-4.2# ffmpeg ffmpeg version 4.1 Copyright (c) 2000-2018 the FFmpeg developers built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-16) configuration: --enable-shared --enable-swscale --enable-gpl --enable-nonfree --enable-pic --prefix=/usr/local/ffmpeg --enable-version3 --enable-postproc --enable-pthreads --enable-avisynth libavutil 56. 22.100 / 56. 22.100 libavcodec 58. 35.100 / 58. 35.100 libavformat 58. 20.100 / 58. 20.100 libavdevice 58. 5.100 / 58. 5.100 libavfilter 7. 40.101 / 7. 40.101 libswscale 5. 3.100 / 5. 3.100 libswresample 3. 3.100 / 3. 3.100 libpostproc 55. 3.100 / 55. 3.100 Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}... Use -h to get full help or, even better, run 'man ffmpeg'
附:ffmpeg configure 參數配置
進入目錄 /home/ffmpeg/ffmpeg-4.1 輸入命令 ./configure --help 即可查看各個參數信息:
Standard options: 基本選項參數 --help 顯示此幫助信息|print this message --log[=FILE|yes|no] 記錄測試並輸出到config.err文件|log tests and output to FILE [config.err] --prefix=PREFIX 安裝程序到指定目錄(默認/usr/local)|install in PREFIX [/usr/local] --libdir=DIR 安裝庫到指定目錄(默認prefix/lib)|install libs in DIR [PREFIX/lib] --shlibdir=DIR 指定共享庫路徑(默認prefix/lib)|install shared libs in DIR [PREFIX/lib] --incdir=DIR 指定includes路徑(默認prefix/include/ffmpeg)|install includes in DIR[PREFIX/include/ffmpeg] --mandir=DIR 指定man page路徑(默認prefix/man)install man page in DIR [PREFIX/man] --enable-mp3lame 啟用mp3編碼libmp3lame(默認關閉)enable MP3 encoding via libmp3lame[default=no] --enable-libogg 啟用ogg支持libogg(默認關閉)enable Ogg support via libogg [default=no] --enable-vorbis 啟用Vorbis支持libvorbis(默認關閉)enable Vorbis support via libvorbis [default=no] --enable-faad 啟用faad支持libfaad(默認關閉)enable FAAD support via libfaad [default=no] --enable-faadbin 啟用faad運行時鏈接支持(默認關閉)build FAAD support with runtime linking[default=no] --enable-faac 啟用faac支持libfaac(默認關閉)enable FAAC support via libfaac [default=no] --enable-libgsm 啟用GSM支持libgsm(默認關閉)enable GSM support via libgsm [default=no] --enable-xvid 啟用xvid支持xvidcore(默認關閉)enable XviD support via xvidcore [default=no] --enable-libx264 啟用H.264編碼(默認關閉)enable H.264 encoding via x264 [default=no] --enable-mingw32 啟用MinGW本地/交叉win環境編譯|enable MinGW native/cross Windows compile --enable-mingwce 啟用MinGW本地/交叉winCE環境編譯enable MinGW native/cross WinCE compile --enable-a52 啟用A52支持(默認關閉)enable GPLed A52 support [default=no] --enable-a52bin 啟用運行時打開liba52.so.0(默認關閉)open liba52.so.0 at runtime [default=no] --enable-dts 啟用DTS支持(默認關閉)enable GPLed DTS support [default=no] --enable-pp 啟用后加工支持(默認關閉)enable GPLed postprocessing support [default=no] --enable-static 構建靜態庫(默認啟用)build static libraries [default=yes] --disable-static 禁止構建靜態庫(默認關閉)do not build static libraries [default=no] --enable-shared 構建共享庫(默認關閉)build shared libraries [default=no] --disable-shared 禁止構建共享庫(默認啟用)do not build shared libraries [default=yes] --enable-amr_nb 啟用amr_nb float音頻編解碼器|enable amr_nb float audio codec --enable-amr_nb-fixed 啟用fixed amr_nb codec | use fixed point for amr-nb codec --enable-amr_wb 啟用amr_wb float音頻編解碼器|enable amr_wb float audio codec --enable-amr_if2 啟用amr_wb IF2音頻編解碼器|enable amr_wb IF2 audio codec --enable-sunmlib 啟用Sun medialib(默認關閉) | use Sun medialib [default=no] --enable-pthreads 啟用pthreads(多線程)(默認關閉)use pthreads [default=no] --enable-dc1394 啟用libdc1394、libraw1394抓取IIDC-1394(默認關閉)enable IIDC-1394 grabbing using libdc1394 and libraw1394 [default=no] --enable-swscaler 啟用計數器支持?(默認關閉)software scaler support [default=no] --enable-avisynth 允許讀取AVISynth腳本本件(默認關閉)allow reading AVISynth script files [default=no] --enable-gpl 允許使用GPL(默認關閉)allow use of GPL code, the resulting libav* and ffmpeg will be under GPL [default=no] Advanced options (experts only): 高級選項參數(供專業人員使用) --source-path=PATH 源碼的路徑(當前為/root/flv/ffmpeg)| path to source code [/root/flv/ffmpeg] --cross-prefix=PREFIX 為編譯工具指定路徑 | use PREFIX for compilation tools [] --cross-compile 假定使用了交叉編譯 | assume a cross-compiler is used --cc=CC 指定使用何種C編譯器(默認gcc)use C compiler CC [gcc] --make=MAKE 使用特定的make | use specified make [make] --extra-cflags=ECFLAGS 添加ECFLAGS到CFLAGS | add ECFLAGS to CFLAGS [] --extra-ldflags=ELDFLAGS 添加ELDFLAGS到LDFLAGS(默認-Wl,--as-needed)| add ELDFLAGS to LDFLAGS [ -Wl,--as-needed] --extra-libs=ELIBS 添加ELIBS | add ELIBS [] --build-suffix=SUFFIX 為專用程序添加后綴 | suffix for application specific build [] --arch=ARCH 選擇機器架構(默認x86)select architecture [x86] --cpu=CPU 選用最低的cpu(影響指令的選擇,可以在老CPU上出錯) | selects the minimum cpu required (affects instruction selection, may crash on older CPUs) --powerpc-perf-enable 啟用PPC上面的性能報告(需要啟用PMC)enable performance report on PPC (requires enabling PMC) --disable-mmx 禁用MMX | disable MMX usage --disable-armv5te 禁用armv5te | disable armv5te usage --disable-iwmmxt 禁用iwmmxt | disable iwmmxt usage --disable-altivec 禁用AltiVec | disable AltiVec usage --disable-audio-oss 禁用OSS音頻支持(默認啟用)disable OSS audio support [default=no] --disable-audio-beos 禁用BeOS音頻支持(默認啟用)disable BeOS audio support [default=no] --disable-v4l 禁用video4linux提取(默認啟用)disable video4linux grabbing [default=no] --disable-v4l2 禁用video4linux2提取(默認啟用)disable video4linux2 grabbing [default=no] --disable-bktr 禁用bktr視頻提取(默認啟用)disable bktr video grabbing [default=no] --disable-dv1394 禁用DV1394提取(默認啟用)disable DV1394 grabbing [default=no] --disable-network 禁用網絡支持(默認支持)disable network support [default=no] --disable-ipv6 禁用ipv6支持(默認支持)disable ipv6 support [default=no] --disable-zlib 禁用zlib(默認支持)disable zlib [default=no] --disable-simple_idct 禁用simple IDCT例程(默認啟用)disable simple IDCT routines [default=no] --disable-vhook 禁用video hooking支持 | disable video hooking support --enable-gprof enable profiling with gprof [no] --disable-debug 禁用調試符號 | disable debugging symbols --disable-opts 禁用編譯器最優化 | disable compiler optimizations --disable-mpegaudio-hp 啟用更快的解碼MPEG音頻(但精確度較低)(默認禁用)faster (but less accurate) MPEG audio decoding [default=no] --disable-protocols 禁用 I/O 協議支持(默認啟用)disable I/O protocols support [default=no] --disable-ffserver 禁用生成ffserver | disable ffserver build --disable-ffplay 禁用生成ffplay | disable ffplay build --enable-small 啟用優化文件尺寸大小(犧牲速度)optimize for size instead of speed --enable-memalign-hack 啟用模擬內存排列,由內存調試器干涉? | emulate memalign, interferes with memory debuggers --disable-strip 禁用剝離可執行程序和共享庫 | disable stripping of executables and shared libraries --disable-encoder=NAME 禁用XX編碼器 | disables encoder NAME --enable-encoder=NAME 啟用XX編碼器 | enables encoder NAME --disable-decoder=NAME 禁用XX解碼器 | disables decoder NAME --enable-decoder=NAME 啟用XX解碼器 | enables decoder NAME --disable-encoders 禁用所有編碼器 | disables all encoders --disable-decoders 禁用所有解碼器 | disables all decoders --disable-muxer=NAME 禁用XX混音器 | disables muxer NAME --enable-muxer=NAME 啟用XX混音器 | enables muxer NAME --disable-muxers 禁用所有混音器 | disables all muxers --disable-demuxer=NAME 禁用XX解軌器 | disables demuxer NAME --enable-demuxer=NAME 啟用XX解軌器 | enables demuxer NAME --disable-demuxers 禁用所有解軌器 | disables all demuxers --enable-parser=NAME 啟用XX剖析器 | enables parser NAME --disable-parser=NAME 禁用XX剖析器 | disables parser NAME --disable-parsers 禁用所有剖析器 | disables all parsers