天天看點

Windows編譯x264、ffmpeg及vmaf

Windows編譯x264、ffmpeg及vmaf

    • 文章介紹
    • 環境準備
    • 編譯x264
    • 編譯ffmpeg
    • 編譯vmaf

文章介紹

本文主要介紹在windows上編譯音視訊所需要的開源庫,包括x264,ffmpeg和vmaf。

本文編譯所使用配置:

os : win10 x64

vs : 2019

其他版本可以參考。

環境準備

ffmpeg是基于linux開發的,需要gc++來編譯,windows平台下是MSVC++(Microsoft Visual C++),是以暫不支援直接編譯,需要搭建類Linux的環境,用gc++來編譯,環境搭建需要下載下傳MSYS2。

下載下傳安裝MSYS2:

  • 官網下載下傳64位安裝包
  • 安裝目錄自己選擇,本文安裝目錄為:D:\soft_install\msys2 (注意後邊路徑設定需要用自己的安裝目錄)
  • 編輯安裝目錄下的msys2_shell.cmd 檔案,将rem MSYS2_PATH_TYPE=inherit 這一行的 rem 移除。
    Windows編譯x264、ffmpeg及vmaf
  • 運作 msys2_shell.cmd
  • 在shell中依次輸入指令:
pacman -Syu
#(中間會退出一次,重新開機繼續執行這條指令)
pacman -S make
pacman -S diffutils
pacman -S nasm
mv /usr/bin/link.exe /usr/bin/link.exe.bak 
#(上邊這句是重命名link.exe,目的是避免與MSVC的link.exe沖突)

#編譯vmaf需要
pacman -S --noconfirm --needed mingw-w64-x86_64-nasm  mingw-w64-x86_64-meson


           

這篇文章中提到有兩種方式可以編譯:msys或者MinGW-w64,本文使用msys版本(親測并沒有題主所說的依賴msys-2.0.dll)進行編譯。

編譯x264

  1. 獲得h264源碼:

    獲得源碼有兩種方式:1)直接下載下傳壓縮包;2)用git下載下傳

    1.1)H264官網下載下傳,下載下傳之後解壓就可以:

    Windows編譯x264、ffmpeg及vmaf
    1.2)使用git下載下傳源碼
#安裝git
pacman -S git

#源碼路徑,更改為自己的路徑
cd f:/encode_test_project

#clone代碼
git clone https://code.videolan.org/videolan/x264.git

cd x264
#切換到stable分支,也可以直接用master分支
git checkout -b stable remotes/origin/stable
           

使用git的一個好處是當源碼有新的送出時可以用指令行更新代碼及切換分支

  1. 配置腳本檔案:
curl "http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD" > config.guess
sed -i 's/host_os = mingw/host_os = msys/' configure
           
  1. 編譯X264:

    3.1 打開 x64 Native Tools Command Prompt for VS2019(适用于 VS 2017 的 x64 本機工具指令提示)

    Windows編譯x264、ffmpeg及vmaf

    3.2 進入到MSYS的安裝目錄

    3.3 運作msys2_shell.cmd

    Windows編譯x264、ffmpeg及vmaf

    注意跨盤需要添加 /d 選項

    然後在MSYS中運作:

cd f:/encode_test_project
mkdir -p build/x264
cd build/x264
#注意下面配置路徑與自己相符
CC=cl ./../../x264/configure --prefix=./../../installed --enable-shared --enable-static
make -j 8
make install
mv ./../../installed/lib/libx264.dll.lib ./../../installed/lib/libx264.lib
           

編譯ffmpeg

  1. 獲得ffmpeg源碼:

    同x264

    1.1)ffmpeg官網下載下傳,下載下傳之後解壓就可以:

    Windows編譯x264、ffmpeg及vmaf
    1.2)使用git下載下傳源碼
#源碼路徑,更改為自己的路徑
cd f:/encode_test_project
#clone代碼
git clone https://git.ffmpeg.org/ffmpeg.git

cd ffmpeg
#切換到4.4分支,也可以直接用master分支
git checkout -b release/4.4  remotes/origin/release/4.4
           
  1. 編譯ffmpeg

    同x264

    2.1打開 x64 Native Tools Command Prompt for VS2019(适用于 VS 2017 的 x64 本機工具指令提示)

    2.2 進入到MSYS的安裝目錄

    2.3 運作msys2_shell.cmd

    在打開的shell中運作下列:

cd f:/encode_test_project
mkdir -p build/ffmpeg
cd build/ffmpeg
#注意路徑
CC=cl ./../../ffmpeg/configure \
             --prefix=./../../installed \
             --toolchain=msvc \
             --arch=x86_64 \
             --enable-x86asm  \
             --enable-asm \
             --enable-shared \
             --enable-static \
             --disable-programs \
             --enable-avresample \
             --enable-libx264 \
             --enable-w32threads \
             --enable-gpl \
             --extra-ldflags="-LIBPATH:./../../installed/lib/" \
             --extra-cflags="-I./../../installed/include/"

make V=1 -j 8
make install
           

最後生成的lib和dll 都在installed/lib 裡面了

編譯vmaf

  1. 獲得vmaf源碼:

    vmaf 的源碼在github上: https://github.com/Netflix/vmaf,

    1.1)vmaf官網下載下傳,下載下傳之後解壓就可以:

    Windows編譯x264、ffmpeg及vmaf
    1.2)使用git下載下傳源碼
#源碼路徑,更改為自己的路徑
cd f:/encode_test_project
#clone代碼
git clone https://github.com/Netflix/vmaf.git

cd vmaf
#切換到2.1.1分支,也可以直接用master分支
git checkout -b branch_v2.1.1  v2.1.1
           
  1. 編譯vmaf

    2.1 進入到MSYS的安裝目錄

    2.2 運作mingw64.exe

    在打開的shell中運作下列:

cd f:/encode_test_project
cd vmaf
#注意路徑
meson setup libvmaf libvmaf/build --buildtype release --default-library both --prefix  f:/encode_test_project/installed

meson install -C libvmaf/build
           

這種方式是官方推薦的方式,但是隻能生成dll檔案,沒有生成lib檔案,也許是設定不對,歡迎在評論區讨論。自定義開發的話隻能動态調用dll檔案了。

到這一步就完成了x264,ffmpeg和vmaf了,頭檔案在installed/include裡面,生成的lib和dll在installed/lib 裡面。

繼續閱讀