1. 程式人生 > >Ubuntu ffmpeg編譯靜態庫和共享庫

Ubuntu ffmpeg編譯靜態庫和共享庫

Ubuntu16.04 環境下編譯ffmpeg生成靜態庫和共享庫

基本上按照這個步驟編

Compile FFmpeg on Ubuntu,Debian,or Mint

Get the Dependencies

sudo apt-get update
sudo apt-get -y install autoconf automake build-essential libass-dev libfreetype6-dev \
  libsdl2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev
libxcb-shm0-dev \ libxcb-xfixes0-dev pkg-config texinfo wget zlib1g-dev

Compilation & Installation

mkdir ~/ffmpeg_sources

任意指定一個目錄即可

Yasm

強烈推薦使用,不然會編得很慢
An assembler for x86 optimizations used by x264 and FFmpeg. Highly recommended or your resulting build may be very slow.

If your repository provides yasm version ≥ 1.2.0 then you can install that instead of compiling:

sudo apt-get install yasm

cd ~/ffmpeg_sources
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install

nasm

使用x264的話就需要
NASM assembler. Required for compilation of x264 and other tools.

cd ~/ffmpeg_sources
wget http://www.nasm.us/pub/nasm/releasebuilds/2.13.01/nasm-2.13.01.tar.bz2
tar xjvf nasm-2.13.01.tar.bz2
cd nasm-2.13.01
./autogen.sh
PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
PATH="$HOME/bin:$PATH" make
make install

libx264

H.264 video encoder. See the H.264 Encoding Guide for more information and usage examples.

Requires ffmpeg to be configured with –enable-gpl –enable-libx264.

If your repository provides libx264-dev version ≥ 118 then you can install that instead of compiling:

sudo apt-get install libx264-dev

cd ~/ffmpeg_sources
wget http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
tar xjvf last_x264.tar.bz2
cd x264-snapshot*
PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static --disable-opencl
PATH="$HOME/bin:$PATH" make
make install

以下的庫有選擇地安裝吧,有些音訊的庫用不到,都安裝也可以,注意看英文說明,哪個需要在執行configure檔案的時候,enable一下

libx265

H.265/HEVC video encoder. See the H.265 Encoding Guide for more information and usage examples.

If your repository provides libx265-dev version ≥ 68 then you can install that instead of compiling:

sudo apt-get install libx265-dev

sudo apt-get install cmake mercurial
cd ~/ffmpeg_sources
hg clone https://bitbucket.org/multicoreware/x265
cd ~/ffmpeg_sources/x265/build/linux
PATH="$HOME/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make
make install

libfdk-aac

AAC audio encoder. See the AAC Audio Encoding Guide for more information and usage examples.

Requires ffmpeg to be configured with –enable-libfdk-aac (and –enable-nonfree if you also included –enable-gpl).

If your repository provides libfdk-aac-dev then you can install that instead of compiling:

sudo apt-get install libfdk-aac-dev

cd ~/ffmpeg_sources
wget -O fdk-aac.tar.gz https://github.com/mstorsjo/fdk-aac/tarball/master
tar xzvf fdk-aac.tar.gz
cd mstorsjo-fdk-aac*
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install

libmp3lame

MP3 audio encoder.

Requires ffmpeg to be configured with –enable-libmp3lame.

If your repository provides libmp3lame-dev version ≥ 3.98.3 then you can install that instead of compiling:

sudo apt-get install libmp3lame-dev

cd ~/ffmpeg_sources
wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
tar xzvf lame-3.99.5.tar.gz
cd lame-3.99.5
./configure --prefix="$HOME/ffmpeg_build" --enable-nasm --disable-shared
make
make install

libopus

Opus audio decoder and encoder.

Requires ffmpeg to be configured with –enable-libopus.

If your repository provides libopus-dev version ≥ 1.1 then you can install that instead of compiling:

sudo apt-get install libopus-dev

cd ~/ffmpeg_sources
wget https://archive.mozilla.org/pub/opus/opus-1.1.5.tar.gz
tar xzvf opus-1.1.5.tar.gz
cd opus-1.1.5
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install

libvpx

VP8/VP9 video encoder and decoder. See the VP8 Video Encoding Guide for more information and usage examples.

Requires ffmpeg to be configured with –enable-libvpx.

If your repository provides libvpx-dev version ≥ 0.9.7 then you can install that instead of compiling:

sudo apt-get install libvpx-dev

sudo apt-get install git
cd ~/ffmpeg_sources
git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git
cd libvpx
PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --disable-examples --disable-unit-tests
PATH="$HOME/bin:$PATH" make
make install

最後,ffmpeg

cd ~/ffmpeg_sources
wget http://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
tar xjvf ffmpeg-snapshot.tar.bz2
cd ffmpeg
PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" 

在解壓後的ffmpeg目錄下面有configure檔案,開啟或者執行./configure –help可看到使用說明,以下是官方文件中的配置,其中–pkg-config-flags=”–static”一句表明只生成靜態庫.a,若想生成共享庫.so,或者都生成,參考文章最後的配置。

./configure \
  --prefix="$HOME/ffmpeg_build" \
  --pkg-config-flags="--static" \
  --extra-cflags="-I$HOME/ffmpeg_build/include" \
  --extra-ldflags="-L$HOME/ffmpeg_build/lib" \
  --bindir="$HOME/bin" \
  --enable-gpl \
  --enable-libass \
  --enable-libfdk-aac \
  --enable-libfreetype \
  --enable-libmp3lame \
  --enable-libopus \
  --enable-libtheora \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-libx265 \
  --enable-nonfree
PATH="$HOME/bin:$PATH" make
make install
hash -r

只生成靜態庫.a:使用以上配置
只生成共享庫.so:
刪去–pkg-config-flags=”–static”一句
使用–enable-shared –disable-static
靜態庫.a和共享庫.so都聲稱:
刪去–pkg-config-flags=”–static”一句
僅使用–enable-shared(兩條-)

最終,在~/ffmpeg-build/lib目錄下,可檢視到生成的相關庫。
這裡寫圖片描述
注:生成的.so有一些是.so.*形式的副檔名,如不想生成此種形式,需要修改configure檔案。
貼出網上的一種改法:
將該檔案中的如下四行:

SLIBNAME_WITH_MAJOR=’(SLIBNAME).(LIBMAJOR)’

LIB_INSTALL_EXTRA_CMD=’$$(RANLIB)”$(LIBDIR)/$(LIBNAME)”’

SLIB_INSTALL_NAME=’$(SLIBNAME_WITH_VERSION)’

SLIB_INSTALL_LINKS=’(SLIBNAMEWITHMAJOR)(SLIBNAME)’

替換為:

SLIBNAME_WITH_MAJOR=’(SLIBPREF)(FULLNAME)-(LIBMAJOR)(SLIBSUF)’

LIB_INSTALL_EXTRA_CMD=’$$(RANLIB)”$(LIBDIR)/$(LIBNAME)”’

SLIB_INSTALL_NAME=’$(SLIBNAME_WITH_MAJOR)’

SLIB_INSTALL_LINKS=’$(SLIBNAME)’

如果想撤銷以上所有操作對你的系統造成的改變,使用以下命令

 rm -rf ~/ffmpeg_build ~/ffmpeg_sources 
 ~/bin/{ffmpeg,ffprobe,ffplay,ffserver,x264,x265,nasm}
sudo apt-get autoremove autoconf automake build-essential cmake libass-dev libfreetype6-dev \
  libmp3lame-dev libopus-dev libsdl2-dev libtheora-dev libtool libva-dev libvdpau-dev \
  libvorbis-dev libvpx-dev libx264-dev libxcb1-dev libxcb-shm0-dev ibxcb-xfixes0-dev mercurial texinfo zlib1g-dev
sed -i '/ffmpeg_build/c\' ~/.manpath
hash -r

更多內容請參考文章一開始貼出的官方文件網站。