前言,公司轉碼集羣服務器資源有限,須要考慮GPU方案,本文記錄下整個實現ffmpeg gpu 轉碼的過程。 linux
該文章後續仍在不斷的更新修改中, 請移步到原文地址http://dmwan.ccgit
環境:github
DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"
注意,這裏機器啓動級別調低,不要加載桌面系統。ubuntu
本機是2核4G 普通硬盤,gpu 型號:GTX950M緩存
第一部分,安裝cuda 8:bash
1.1 查看是否有顯卡:服務器
lspci | grep -i nvidia
1.2 查看操做系統是否cuda 官方支持:app
uname -m && cat /etc/*release
1.3 安裝gcc g++ 等編譯依賴基礎庫ide
apt-get install gcc g++ build-essential
1.4 下載安裝cuda測試
下載cuda: wget --no-check-certificate https://developer.nvidia.com/compute/cuda/8.0/prod/local_installers/cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64-deb 安裝 cuda 源: dpkg -i cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64-deb 添加源: deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64 / 更新緩存: apt-get update 安裝cuda: apt-get install cuda
1.5 設置環境變量
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}} export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} source ~/.bashrc
1.6 安裝官方示例並驗證環境
查看驅動信息: cat /proc/driver/nvidia/version 安裝官方示例: cuda-install-samples-8.0.sh ./ 跑下示例: cd NVIDIA_CUDA-8.0_Samples/bin/x86_64/linux/release && ./deviceQuery 輸出下面內容 Pass爲安裝成功: CUDA Device Query (Runtime API) version (CUDART static linking) Detected 1 CUDA Capable device(s) Device 0: "GeForce GTX 1080" CUDA Driver Version / Runtime Version 8.0 / 8.0 CUDA Capability Major/Minor version number: 6.1 Total amount of global memory: 8112 MBytes (8506179584 bytes) (20) Multiprocessors, (128) CUDA Cores/MP: 2560 CUDA Cores GPU Max Clock rate: 1734 MHz (1.73 GHz) Memory Clock rate: 5005 Mhz Memory Bus Width: 256-bit L2 Cache Size: 2097152 bytes Maximum Texture Dimension Size (x,y,z) 1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384) Maximum Layered 1D Texture Size, (num) layers 1D=(32768), 2048 layers Maximum Layered 2D Texture Size, (num) layers 2D=(32768, 32768), 2048 layers Total amount of constant memory: 65536 bytes Total amount of shared memory per block: 49152 bytes Total number of registers available per block: 65536 Warp size: 32 Maximum number of threads per multiprocessor: 2048 Maximum number of threads per block: 1024 Max dimension size of a thread block (x,y,z): (1024, 1024, 64) Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535) Maximum memory pitch: 2147483647 bytes Texture alignment: 512 bytes Concurrent copy and kernel execution: Yes with 2 copy engine(s) Run time limit on kernels: No Integrated GPU sharing Host Memory: No Support host page-locked memory mapping: Yes Alignment requirement for Surfaces: Yes Device has ECC support: Disabled Device supports Unified Addressing (UVA): Yes Device PCI Domain ID / Bus ID / location ID: 0 / 3 / 0 Compute Mode: < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) > deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce GTX 1080 Result = PASS
第二部分,安裝ffmpeg
2.1 安裝基礎依賴:
apt-get update 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 zlib1g-dev
2.2 安裝yasm
apt-get install yasm #版本爲1.3
2.3 安裝lib264
apt-get install libx264-dev #版本爲148
2.4 安裝libx265(顯卡不必定支持265編碼)
apt-get install libx265-dev
2.5 安裝 libvpx
apt-get install libvpx-dev #版本爲1.5
2.6 安裝 安裝libfdk-aac
apt-get install libfdk-aac-dev # 無版本要求
2.7 安裝libmp3lam
apt-get install libmp3lame-dev
2.8 安裝libopus
apt-get install libopus-dev # 1.1.2
第三部分,安裝NVENC:
3.1 安裝依賴:
sudo apt-get -y install glew-utils libglew-dbg libglew-dev libglew1.13 \ libglewmx-dev libglewmx-dbg freeglut3 freeglut3-dev freeglut3-dbg libghc-glut-dev \ libghc-glut-doc libghc-glut-prof libalut-dev libxmu-dev libxmu-headers libxmu6 \ libxmu6-dbg libxmuu-dev libxmuu1 libxmuu1-dbg
3.2 下載ffmpeg
git clone https://github.com/FFmpeg/FFmpeg ffmpeg -b master
3.3 下載nvidia video sdk
下載地址:https://developer.nvidia.com/nvidia-video-codec-sdk#Download,這裏版本8.0, 解壓後命名爲 nv_sdk, 與ffmpeg 放於同文件夾。
3.4 移動頭文件
cp -r nv_sdk/LegacySamples/common/inc/ /usr/include/
第四部分,編譯ffmpeg
編譯命令以下:
export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig PATH="$HOME/bin:$PATH" ./configure \ --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 \ --extra-cflags=-I../nv_sdk \ --extra-ldflags=-L../nv_sdk \ --extra-cflags="-I/usr/local/cuda/include/" \ --extra-ldflags=-L/usr/local/cuda/lib64 \ --disable-shared \ --enable-nvenc \ --enable-cuda \ --enable-cuvid \ --enable-libnpp PATH="$HOME/bin:$PATH" make -j$(nproc) make -j$(nproc) install make -j$(nproc) distclean hash -r
第五部分,轉碼測試:
ffmpeg -i input.flv -c:v h264_nvenc -c:a aac output.mp4
倍速對比,一樣硬件條件下,gpu 提速在7-8倍左右。
frame=21022 fps=398 q=21.0 Lsize= 232698kB time=00:14:36.75 bitrate=2174.2kbits/s dup=137 drop=0 speed=16.6x
播放試了下播放效果,和cpu 播放無明顯差異。