前言,公司转码集群服务器资源有限,需要考虑GPU方案,本文记录下整个实现ffmpeg gpu 转码的过程。     
      该文章后续仍在不断的更新修改中, 请移步到原文地址 https://my.oschina.net/u/2950272/blog
  环境:
  DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_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++ 等编译依赖基础库
  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 播放无明显差别。