Ubuntu16.04编译ffmpeg以及cuda加速配置

Preliminary(参考此博客)

  1. 安装依赖关系
$ sudo apt-get update
$ sudo 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 wget zlib1g-dev
  1. 源码编译yasm
    x264和FFmpeg使用的x86优化的汇编器。非常推荐不然编译极慢!
    下载链接:http://yasm.tortall.net/Download.html
    下载源码版本:yasm-1.3.0.tar.gz
$ wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
$ tar xzvf yasm-*.tar.gz
$ cd yasm-*
$ ./configure --prefix="$HOME/your_ffmpeg_build_dir" --bindir="$HOME/bin"
$ sudo make
$ sudo make install
  1. 源码编译NASM(此部分本博没试)
$ sudo apt install nasm
$ wget http://www.nasm.us/pub/nasm/releasebuilds/2.13.01/nasm-2.13.01.tar.bz2
$ tar xjvf nasm-*.tar.bz2
$ cd nasm-*
$ ./autogen.sh
$ PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
$ PATH="$HOME/bin:$PATH" make
$ sudo make install
  1. 源码编译libx264
$ wget http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
$ tar xjvf last_x264.tar.bz2
$ cd x264-*
$ PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-shared --disable-opencl
$ PATH="$HOME/bin:$PATH" make
$ sudo make install
  1. 源码编译libx265(首先要安装cmake)
#源码编译cmake
wget https://cmake.org/files/v3.3/cmake-3.3.2.tar.gz
tar xzvf cmake-*.tar.gz
cd cmake-*
./bootstrap
sudo make
sudo make install
cmake --version #test
#下面开始编译x265
wget http://ftp.videolan.org/pub/videolan/x265/x265_2.6.tar.gz
cd x265_2.6/build/linux
sh make-Makefiles.bash
#c键config;g键generate并退出
sudo make
sudo make install

将x265整合到ffmpeg时的config以及可能报错
./configure --prefix=/usr/local/ffmpeg_3/ --enable-shared --enable-yasm --enable-libx264 --enable-libx265 --enable-gpl --enable-pthreads --extra-cflags=-I/usr/local/x264/include --extra-ldflags=-L/usr/local/x264/lib --extra-cflags=-I/usr/local/include/ --extra-ldflags=-L/usr/local/lib
执行时可能报错:ERROR: x265 not found using pkg-config
——————————————————————————————————————————————————————————————————
原因为pkg-config找不到
解决方法是改变PKG_CONFIG_PATH:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
——————————————————————————————————————————————————————————————————
pkg-config --modversion x265
1.9

  1. 源码编译安装libmp3lame(没试)
$ wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
$ tar xzvf lame-*.tar.gz
$ cd lame-*
$ ./configure --prefix="$HOME/ffmpeg_build" --enable-nasm --enable-shared
$ sudo make
$ sudo make install
  1. 源码安装 libopus(没试)
$ wget https://archive.mozilla.org/pub/opus/opus-1.1.5.tar.gz
$ tar xzvf opus-*.tar.gz
$ cd opus-*
$ ./configure --prefix="$HOME/ffmpeg_build" --enable-shared
$ sudo make
$ sudo make install
  1. 源码编译pkg-config
$ wget https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz
$ tar -xzf pkg-config-*.tar.gz
$ cd  pkg-config-*
$ ./configure  --with-internal-glib
$ make
$ sudo make install
  1. 源码编译nv-codec-headers

 - 问题1:“ERROR: cuvid requested, but not all dependencies are satisfied:
   cuda/ffnvcodec”
   原因: FFmpeg编译时没有使用nv-codec-headers
	#下载nv-codec-headers
	$ git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
	#编译
	$ cd nv-codec-headers 
	$ make 
	$ sudo make install
	# ffmpeg config设置方式
	./configure --enable-shared --disable-static --disable-yasm --disable-doc --enable-gpl --enable-pthreads --disable-w32threads --disable-os2threads --enable-debug --enable-nvenc --enable-cuda --enable-cuvid --extra-cflags="-Invidia_sdk -I/usr/local/cuda-10.0/include" --extra-ldflags="-Lnvidia_sdk -L/usr/local/cuda-10.0/lib64" --enable-nonfree --enable-libfreetype --enable-libfontconfig --enable-libnpp --enable-gpl
	make -j10 &&sudo make install
 - 衍生问题:对ffmpeg进行config设置时报错:
	"sed 's#@@PREFIX@@#/usr/local#' ffnvcodec.pc.in > ffnvcodec.pc"
   解决:./config前输入命令
   PKG_CONFIG_PATH="/home/ubuntu/ffmpeg/nv-codec-headers "./configure --***
   -------------------------------------------------------------------------------------------------------------------
 - 问题2:“Cannot load cuvidGetDecodeStatus Failed loading nvcuvid.”
   原因: nv-codec-headers 版本与所安装驱动不符
   解决办法:使用与显卡驱动相匹配的nv-codec-headers 即可
-------------------------------------------------------------------------------------------------------------------
   问题3:“Codec h264_cuvid is not supported”
   原因: 显卡不支持硬件加速
   解决办法:更换显卡
   此部分引自[此博客](https://www.jianshu.com/p/532e502aeab8)

Ffmpeg compile from source

  1. 下载ffmpeg源码
    source code download
    Ubuntu16.04编译ffmpeg以及cuda加速配置_第1张图片
  2. 编译ffmpeg源码
    编译config参数设置:
PKG_CONFIG_PATH="/home/ubuntu/ffmpeg/nv-codec-headers" ./configure --prefix=/home/ubuntu/ffmpeg/ffmpeg-4.2.1 --enable-shared --enable-ffplay --enable-cuda --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --enable-gpl --enable-libx264 --enable-libx265 --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --disable-x86asm

环境变量设置以及动态链接调用失败问题

./ffmpeg: error while loading shared libraries: libavdevice.so.58: cannot op(包括其他各种动态链接库找不到的情况)

首先输入命令查看哪些动态链接无法找到:

$ sudo ldd ffmpeg

再输入命令寻找这些库在什么位置:

$ find / -name"libavdevice.so.58" 

添加这些库到/etc/ld.so.conf:

$ sudo vim /etc/ld.so.conf
添加下面三个路径到文档最后
/usr/local/lib
/home/dir/to/your/ffmpeg/lib
/usr/local/cuda-10.0/lib64
$ ldconfig

环境变量配置:

$ sudo vim /etc/profile
最后一行加入:
export PATH="/home/dir/to/your/ffmpeg/bin:$PATH"

你可能感兴趣的:(Ffmpeg)