前言,公司转码集群服务器资源有限,需要考虑GPU方案,本文记录下整个实现ffmpeg gpu 转码的过程。
该文章后续仍在不断的更新修改中, 请移步到原文地址http://dmwan.cc
环境:
1DISTRIB_ID=Ubuntu 2DISTRIB_RELEASE=16.04 3DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"
注意,这里机器启动级别调低,不要加载桌面系统。
本机是2核4G 普通硬盘,gpu 型号:GTX950M
第一部分,安装cuda 8:
1.1 查看是否有显卡:
lspci | grep -i nvidia
1.2 查看操作系统是否cuda 官方支持:
uname -m && cat /etc/*release
1.3 安装gcc g++ 等编译依赖基础库
1apt-get install gcc g++ build-essential 2
1.4 下载安装cuda
1下载cuda: 2wget --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 3 4安装 cuda 源: 5dpkg -i cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64-deb 6 7添加源: 8deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64 / 9 10更新缓存: 11apt-get update 12 13安装cuda: 14apt-get install cuda 15
1.5 设置环境变量
1export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}} 2export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} 3source ~/.bashrc
1.6 安装官方示例并验证环境
1查看驱动信息: 2cat /proc/driver/nvidia/version 3 4安装官方示例: 5cuda-install-samples-8.0.sh ./ 6 7跑下示例: 8cd NVIDIA_CUDA-8.0_Samples/bin/x86_64/linux/release && ./deviceQuery 9 10输出下面内容 Pass为安装成功: 11 12CUDA Device Query (Runtime API) version (CUDART static linking) 13 14Detected 1 CUDA Capable device(s) 15 16Device 0: "GeForce GTX 1080" 17 CUDA Driver Version / Runtime Version 8.0 / 8.0 18 CUDA Capability Major/Minor version number: 6.1 19 Total amount of global memory: 8112 MBytes (8506179584 bytes) 20 (20) Multiprocessors, (128) CUDA Cores/MP: 2560 CUDA Cores 21 GPU Max Clock rate: 1734 MHz (1.73 GHz) 22 Memory Clock rate: 5005 Mhz 23 Memory Bus Width: 256-bit 24 L2 Cache Size: 2097152 bytes 25 Maximum Texture Dimension Size (x,y,z) 1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384) 26 Maximum Layered 1D Texture Size, (num) layers 1D=(32768), 2048 layers 27 Maximum Layered 2D Texture Size, (num) layers 2D=(32768, 32768), 2048 layers 28 Total amount of constant memory: 65536 bytes 29 Total amount of shared memory per block: 49152 bytes 30 Total number of registers available per block: 65536 31 Warp size: 32 32 Maximum number of threads per multiprocessor: 2048 33 Maximum number of threads per block: 1024 34 Max dimension size of a thread block (x,y,z): (1024, 1024, 64) 35 Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535) 36 Maximum memory pitch: 2147483647 bytes 37 Texture alignment: 512 bytes 38 Concurrent copy and kernel execution: Yes with 2 copy engine(s) 39 Run time limit on kernels: No 40 Integrated GPU sharing Host Memory: No 41 Support host page-locked memory mapping: Yes 42 Alignment requirement for Surfaces: Yes 43 Device has ECC support: Disabled 44 Device supports Unified Addressing (UVA): Yes 45 Device PCI Domain ID / Bus ID / location ID: 0 / 3 / 0 46 Compute Mode: 47 < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) > 48 49deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce GTX 1080 50Result = PASS 51 52
第二部分,安装ffmpeg
2.1 安装基础依赖:
1apt-get update 2apt-get -y install autoconf automake build-essential libass-dev libfreetype6-dev \ 3 libsdl2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \ 4 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 安装依赖:
1sudo apt-get -y install glew-utils libglew-dbg libglew-dev libglew1.13 \ 2libglewmx-dev libglewmx-dbg freeglut3 freeglut3-dev freeglut3-dbg libghc-glut-dev \ 3libghc-glut-doc libghc-glut-prof libalut-dev libxmu-dev libxmu-headers libxmu6 \ 4libxmu6-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
编译命令如下:
1export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig 2PATH="$HOME/bin:$PATH" ./configure \ 3 --bindir="$HOME/bin" \ 4 --enable-gpl \ 5 --enable-libass \ 6 --enable-libfdk-aac \ 7 --enable-libfreetype \ 8 --enable-libmp3lame \ 9 --enable-libopus \ 10 --enable-libtheora \ 11 --enable-libvorbis \ 12 --enable-libvpx \ 13 --enable-libx264 \ 14 --enable-libx265 \ 15 --enable-nonfree \ 16 --extra-cflags=-I../nv_sdk \ 17 --extra-ldflags=-L../nv_sdk \ 18 --extra-cflags="-I/usr/local/cuda/include/" \ 19 --extra-ldflags=-L/usr/local/cuda/lib64 \ 20 --disable-shared \ 21 --enable-nvenc \ 22 --enable-cuda \ 23 --enable-cuvid \ 24 --enable-libnpp 25 26PATH="$HOME/bin:$PATH" make -j$(nproc) 27make -j$(nproc) install 28make -j$(nproc) distclean 29hash -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 播放无明显差别。