1. 程式人生 > >交叉編譯 ffmpeg x264

交叉編譯 ffmpeg x264

arm linux 平臺
1、 交叉編譯x264
我用的是:x264-snapshot-20121212-2245-stable.tar.bz

開始是使用這個命令
./configure --enable-pthread --enable-static --disable-shared --host=arm-linux --disable-asm

然後make
make CC=arm-linux-gcc AR=arm-linux-ar LD=arm-linux-gcc RANLIB=arm-linux-ranlib STRIP=arm-linux-strip
 報出錯誤:

x264.c:50:34: error: libavformat/avformat.h: No such file or directory
x264.c:51:30: error: libavutil/pixfmt.h: No such file or directory
x264.c:52:31: error: libavutil/pixdesc.h: No such file or directory
x264.c:57:32: error: libswscale/swscale.h: No such file or directory
x264.c: In function 'print_version_info':
x264.c:236: error: 'LIBSWSCALE_VERSION_MAJOR' undeclared (first use in this function)
x264.c:236: error: (Each undeclared identifier is reported only once
x264.c:236: error: for each function it appears in.)
x264.c:236: error: 'LIBSWSCALE_VERSION_MINOR' undeclared (first use in this function)
x264.c:236: error: 'LIBSWSCALE_VERSION_MICRO' undeclared (first use in this function)
x264.c:239: error: 'LIBAVFORMAT_VERSION_MAJOR' undeclared (first use in this function)
x264.c:239: error: 'LIBAVFORMAT_VERSION_MINOR' undeclared (first use in this function)
x264.c:239: error: 'LIBAVFORMAT_VERSION_MICRO' undeclared (first use in this function)
x264.c:260: warning: implicit declaration of function 'swscale_license'
x264.c:260: warning: initialization makes pointer from integer without a cast
x264.c: In function 'print_csp_names':
x264.c:351: error: variable 'i' has initializer but incomplete type
x264.c:351: error: 'PIX_FMT_NONE' undeclared (first use in this function)
x264.c:351: error: storage size of 'i' isn't known
x264.c:351: error: 'enum PixelFormat' declared in 'for' loop initial declaration
x264.c:351: error: 'PIX_FMT_NB' undeclared (first use in this function)
x264.c:353: warning: implicit declaration of function 'av_get_pix_fmt_name'
x264.c:353: warning: initialization makes pointer from integer without a cast
x264.c:351: warning: unused variable 'i'
make: *** [x264.o] 錯誤 1

這幾個檔案都是在ffmpeg資料夾中,

解決辦法:

先按後面的方法配置一下ffmpeg,原因是libavutil/avconfig.h是生成的,配置過還沒有就編譯一下,沒有那個檔案編譯的時候會報出找不到這個檔案,

然後修改config.mak檔案的第十行,加上 -I/home/liuyan/h.264/for_X86/ffmpeg-1.0 就可以找到了

再次執行make命令後,編譯通過,但連結時出錯。編譯libx264.a庫時,顯示的命令為:

arm-linux-arlibx264.a ....

make檔案將連結命令和庫名字間沒有空格,導致系統無法識別此命令。我嘗試修改Makefile檔案也不行,乾脆手動執行下面的命令:

arm-linux-ar rc libx264.a common/mc.o common/predict.o common/pixel.o common/macroblock.o common/frame.o common/dct.o common/cpu.o common/cabac.o common/common.o common/osdep.o common/rectangle.o common/set.o common/quant.o common/deblock.o common/vlc.o common/mvpred.o common/bitstream.o encoder/analyse.o encoder/me.o encoder/ratecontrol.o encoder/set.o encoder/macroblock.o encoder/cabac.o encoder/cavlc.o encoder/encoder.o encoder/lookahead.o common/threadpool.o

arm-linux-ranlib libx264.a (用於產生x264-config.h檔案)

由於我電腦上的交叉編譯器是從以下目錄去查詢標頭檔案和庫的,所以把x264.h和libx264.a拷貝到相應目錄:(我是通過在/opt/EmbedSky/4.3.3下查詢pthread.h和libpthread.a來獲取這個目錄的)
cp x264.h x264-config.h /opt/EmbedSky/4.3.3/arm-none-linux-gnueabi/libc/usr/include/
cp libx264.a /opt/EmbedSky/4.3.3/arm-none-linux-gnueabi/libc/armv4t/usr/lib/
 
2、 編譯ffmpeg
我用的是:ffmpeg-1.0.tar.gz
./configure --enable-cross-compile --arch=arm --target-os=linux --enable-static --disable-shared --cc=arm-linux-gcc --enable-libx264 --enable-gpl --disable-network --enable-pthreads --enable-small --disable-parsers --disable-debug

修改config.mak 中:

CC=arm-linux-gcc 

AR=arm-linux-ar 

LD=arm-linux-gcc 

RANLIB=arm-linux-ranlib 

STRIP=arm-linux-strip


make CC=arm-linux-gcc AR=arm-linux-ar LD=arm-linux-gcc RANLIB=arm-linux-ranlib STRIP=arm-linux-strip

下面是轉載別人在PC機上安裝ffmpeg和x264的文件

ffmpeg是一款多媒體視訊和音訊檔案解碼和編碼的工具,用來提供眾多的多媒體(主要指視訊和音訊)檔案的播放和轉換支援。x264是一個編解碼H264視訊格式的庫檔案,相比較xvid或者其它流行的編解碼器轉換效率和轉換後的畫面質量都更加優異。Avidemux, VLC Player, MEncoder, 和Handbrake中都可利用x264庫檔案。

    儘管ffmpeg和x264存在於Ubuntu的軟體倉庫中,但是實際上通過Uuntu軟體倉庫apt-get方式安裝的ffmpeg是不完整的的版本,仍需要我們通過自行編譯才能提供對更多視訊、音訊格式(例如3gp)轉換支援。

    獲取相應的依賴軟體

    1. 首先請確認在軟體源中已經添加了相應的Universe和Multiverse軟體源,目的是我們要通過軟體倉庫來解決x264和ffmpeg的依賴檔案。

    2. 接下來如果您之前安裝了x264,libx264-dev和ffmpeg請先把它解除安裝,開啟X終端或控制檯執行下面的命令:

 sudo apt-get purge ffmpeg x264 libx264-dev

    下面的步驟我們會安裝依賴軟體及其它編譯所需要的軟體

    對於Ubuntu的版本:Intrepid Ibex 8.10

    命令:

 sudo apt-get update
sudo apt-get install build-essential subversion git-core checkinstall yasm texi2html libfaad-dev libfaac-dev libmp3lame-dev libtheora-dev

    對於Ubuntu的版本:Hardy Heron 8.04:

    命令:

 sudo apt-get update
sudo apt-get install build-essential subversion git-core checkinstall texi2html libfaad-dev libfaac-dev liblame-dev libtheora-dev

    Intrepid Ibex 8.10 和 Hardy Heron 8.04都用到的依賴包:

    libsdl1.2-dev: 執行 ffplay需要

    libfaad-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore4-dev libschroedinger-dev libspeex-dev libgsm1-dev: ffmpeg開發應用到的一些檔案,其中一些已經在ffmpeg的原始碼中包含,如果系統中包含且版本太低,可能會導致安裝失敗(不常見)。

    3. 使用Ubuntu版本為Hardy Heron 8.04請注意下。請最好先安裝yasm(一個彙編編譯軟器,和gcc類似)。它可以優化x264的程式碼。當然即便沒有yasm,也可以在編譯過程中加入–disable-asm引數來編譯,不過這樣得到的x264執行效率會稍差一些。

    好像在Ubuntu Hardy Heron 8.04軟體倉庫中的Yasm的版本是0.5.0,而最新版本的x264的彙編編譯部分需要yasm的版本至少為0.6.1,否則在執行編譯命令時會提示:Minimum version is yasm-0.6.1。這時我們可以通過執行:

    命令:

 cd ~/
wget http://www.tortall.net/projects/yasm/releases/yasm-0.7.2.tar.gz
tar xzvf yasm-0.7.2.tar.gz
cd yasm-0.7.2
./configure
make
sudo checkinstall

    來自行下載並編譯相應的軟體安裝x264

    4. 獲取最新版本的x264並編譯安裝,我們在這裡通過git命令獲取:

 cd ~/
git clone git://git.videolan.org/x264.git

    開始編譯並安裝

 cd x264
./configure --enable-shared
make
sudo checkinstall --fstrans=no --install=yes --pkgname=x264 --pkgversion "1:0.svn`date +%Y%m%d`-0.0ubuntu1"
sudo ldconfig

    更多編譯引數請執行./configure –help檢視。。。

    安裝ffmpeg

    5. 通過svn命令獲取最新版本的ffmpeg:

 svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg

    開始編譯並安裝:

 svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg
cd ffmpeg
./configure --enable-gpl --enable-postproc --enable-pthreads --enable-libfaac --enable-libfaad --enable-libmp3lame --enable-libtheora --enable-libx264
make
sudo checkinstall --fstrans=no --install=yes --pkgname=ffmpeg --pkgversion "3:0.svn`date +%Y%m%d`-12ubuntu3"

    更多編譯引數請執行./configure –help檢視。。。,例如美麗星點用到的引數是(命令中加–prefix=/usr引數目的是規定安裝位置,如非必要,請不要加這個引數,以免造成不必要的麻煩):
    ./configure –prefix=/usr –enable-shared –enable-gpl –enable-postproc –enable-swscale –enable-pthreads –enable-nonfree –enable-libdc1394 –enable-libfaac –enable-libfaad –enable-libfaadbin –enable-libgsm –enable-libmp3lame –enable-libnut –enable-libtheora –enable-libvorbis –enable-libx264 –enable-libxvid –enable-avfilter –enable-avfilter-lavf –enable-libamr-nb –enable-libamr-wb
    注意:–enable-libamr-nb –enable-libamr-wb –enable-libfaac這三個引數如果想讓ffmpeg可以轉換3gp,請在編譯過程中加上,其它引數請根據需要新增。

    6. 鎖定x264和ffmpeg這兩個軟體包阻止其升級。這部分也很重要,由於我們自行編譯的軟體包可能會在操作作業系統更新時被替換掉。

    如果您習慣使用的是圖形介面的apt-get命令或者是新利得軟體包管理器,請在管理器的介面執行:
    System(系統) -> Administration (系統管理)-> Synaptic Package Manager(新得利軟體包管理器) -> 在包瀏覽器中選擇安裝好的264-> Package(軟體包) -> Lock Version(鎖定版本)

    使用aptitude命令的話,請通過下面的命令來鎖定版本:
    sudo aptitude hold x264 ffmpeg
    Using ffmpeg and x264使用ffmpeg和x264

    使用ffmpeg最簡便的轉換視訊和音訊的方法是用它的預設檔案(ffmpeg presets),這些檔案在ffmpeg的原始碼中就有。我們需要把它複製到當前使用者目錄的.ffmpeg資料夾中:
    命令:
    新建資料夾

 mkdir ~/.ffmpeg

    複製預設檔案到上面的目錄中

 cp ~/ffmpeg/ffpresets/* ~/.ffmpeg

    現在就可以使用預設檔案了(libx264-default.ffpreset,libx264-hq.ffpreset,libx264-normal.ffpreset,libx264-fastfirstpass.ffpreset,libx264-max.ffpreset),下面的兩個例子會指導你如何使用這些預設:

    命令:

 ffmpeg -i infile -vcodec libx264 -vpre hq -b 1M -bt 1M -threads 0 outfile.mp4

    改變-vpre 後面的引數即可更改不同的預設,很容易理解這幾個引數的意思

    備註(點選檢視對應檔案):

 » default - 預設引數(x264 CLI default values)
» fastfirstpass - 快速轉換(disables options unnecessary for first pass)
» normal - 正常(Dark Shikari’s Q55 preset)
» hq - 高質量轉換(Dark Shikari’s Q75 preset)
» max - 最大檔案(all options maxed out)

    您確實可以不使用預設檔案,不過那樣的話你轉換視訊或音訊檔案時要照顧到很多細節,程式碼會很長,使用預設檔案省去了我們很多麻煩!升級ffmpeg和x264

    理所當然我們即使我們鎖定了版本,我們還是會想到升級我們的ffmpeg和x264到最新的版本,我們可以使用下面的命令(假定您沒有刪除相應的svn和git同步檔案):

 sudo apt-get purge ffmpeg x264
cd ~/x264
make distclean
git pull
./configure
make
sudo checkinstall --fstrans=no --install=yes --pkgname=x264 --pkgversion "1:0.svn`date +%Y%m%d`-0.0ubuntu1"
cd ~/ffmpeg
make distclean
svn update
./configure --enable-gpl --enable-postproc --enable-pthreads --enable-libfaac --enable-libfaad --enable-libmp3lame --enable-libtheora --enable-libx264
make
sudo checkinstall --fstrans=no --install=yes --pkgname=ffmpeg --pkgversion "3:0.svn`date +%Y%m%d`-12ubuntu3"

    解除安裝ffmpeg和x264

    Ubuntu發行版Intrepid Ibex 8.10的操作:
    命令:
    sudo apt-get purge x264 ffmpeg build-essential yasm subversion git-core checkinstall texi2html libfaad-dev libfaac-dev libmp3lame-dev libtheora-dev
    Ubuntu發行版Hardy Heron 8.04:
    命令:

 sudo apt-get purge x264 ffmpeg build-essential yasm subversion git-core checkinstall texi2html libfaad-dev libfaac-dev liblame-dev libtheora-dev

    引用和相關網站:
    本文英文原文: HOWTO: Compile the latest ffmpeg and x264 from source
    ffmpeg主頁
    FFmpeg libx264 presets預設檔案下載:ffpresets.zip (20)
    ffmpeg原始碼configure幫助檔案中文譯文(編譯說明)
    FFmpeg x264 encoding guide(FFmpeg x264編碼指南英文版)