Docker环境配置ffmpeg nvidia加速

Docker环境是ubuntu 16.04 xenial,实际上是tensorflow/tensorflow:1.13.1-gpu-py3的image。
主机为ubuntu 19.04,,刚release就把笔记本系统升级,也是没谁了。。显卡为RTX2070,CUDA版本为10.0.130,nvidia driver版本为418.56。
(1)首先使用nvidia-docker run运行tensorflow的image,进入其bash:nvidia-docker run -it -e NVIDIA_DRIVER_CAPABILITIES=compute,utility,video --rm tensorflow/tensorflow:1.13.1-gpu-py3
(2)在容器中安装必要的软件包和库:

apt-get update -qq && apt-get -y install \
  autoconf \
  automake \
  build-essential \
  cmake \
  git-core \
  libass-dev \
  libfreetype6-dev \
  libsdl2-dev \
  libtool \
  libva-dev \
  libvdpau-dev \
  libvorbis-dev \
  libxcb1-dev \
  libxcb-shm0-dev \
  libxcb-xfixes0-dev \
  pkg-config \
  texinfo \
  wget \
  zlib1g-dev \
  cuda-npp-10-0 \
  cuda-npp-dev-10-0 

其中cuda-npp-<VERSION>是提供libnpp的库。
(3)在$HOME目录下建立两个文件夹,用于存放接下来的文件:mkdir -p ~/ffmpeg_sources ~/bin
(4)下载nasm,注意版本号不能太低,用于libx264的编译过程:

cd ~/ffmpeg_sources && \
wget https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/nasm-2.14.02.tar.bz2 && \
tar xjvf nasm-2.14.02.tar.bz2 && \
cd nasm-2.14.02 && \
./autogen.sh && \
PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" && \
make && \
make install

(5)下载yasm,同样用于编译:

cd ~/ffmpeg_sources && \
wget -O yasm-1.3.0.tar.gz https://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz && \
tar xzvf yasm-1.3.0.tar.gz && \
cd yasm-1.3.0 && \
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" && \
make && \
make install

(6)下载x264库并安装,用于h264编解码:

cd ~/ffmpeg_sources && \
git -C x264 pull 2> /dev/null || git clone --depth 1 https://git.videolan.org/git/x264 && \
cd x264 && \
PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static --enable-pic && \
PATH="$HOME/bin:$PATH" make && \
make install

(7)下载NVIDIA加速使用的一些头文件:

cd / && \
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git && \
cd /nv-codec-headers && \
make && \
make install

(8)最后下载ffmpeg源码并编译:

cd ~/ffmpeg_sources && \
wget -O ffmpeg-snapshot.tar.bz2 https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2 && \
tar xjvf ffmpeg-snapshot.tar.bz2 && \
cd ffmpeg

PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
  --prefix="$HOME/ffmpeg_build" \
  --pkg-config-flags="--static" \
  --extra-cflags="-I$HOME/ffmpeg_build/include -I/usr/local/cuda/include" \
  --extra-ldflags="-L$HOME/ffmpeg_build/lib -L/usr/local/cuda/compat -L/usr/local/cuda/lib64" \
  --extra-libs="-lpthread -lm" \
  --bindir="$HOME/bin" \
  --enable-gpl \
  --enable-libfreetype \
  --enable-libx264 \
  --enable-nonfree \
  --enable-cuda \
  --enable-cuvid \
  --enable-nvenc \
  --enable-libnpp && \
PATH="$HOME/bin:$PATH" make && \
make install

验证:

root@e4eed4755d62:/ffmpeg# ./ffmpeg -hwaccels    //查看硬件加速选项
ffmpeg version 4.1.git Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.11) 20160609
  configuration: 
  libavutil      56. 26.100 / 56. 26.100
  libavcodec     58. 52.100 / 58. 52.100
  libavformat    58. 27.103 / 58. 27.103
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 49.100 /  7. 49.100
  libswscale      5.  4.100 /  5.  4.100
  libswresample   3.  4.100 /  3.  4.100
Hardware acceleration methods:
cuda
cuvid

root@e4eed4755d62:/ffmpeg# ./ffmpeg -codecs | grep cuvid  //查看与cuvid有关的编解码器
ffmpeg version 4.1.git Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.11) 20160609
  configuration: 
  libavutil      56. 26.100 / 56. 26.100
  libavcodec     58. 52.100 / 58. 52.100
  libavformat    58. 27.103 / 58. 27.103
  libavdevice    58.  7.100 / 58.  7.100
  libavfilter     7. 49.100 /  7. 49.100
  libswscale      5.  4.100 /  5.  4.100
  libswresample   3.  4.100 /  3.  4.100
 DEV.LS h264                 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_v4l2m2m h264_cuvid ) (encoders: h264_nvenc h264_v4l2m2m nvenc nvenc_h264 )
 DEV.L. hevc                 H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_cuvid ) (encoders: nvenc_hevc hevc_nvenc )
 DEVIL. mjpeg                Motion JPEG (decoders: mjpeg mjpeg_cuvid )
 DEV.L. mpeg1video           MPEG-1 video (decoders: mpeg1video mpeg1_v4l2m2m mpeg1_cuvid )
 DEV.L. mpeg2video           MPEG-2 video (decoders: mpeg2video mpegvideo mpeg2_v4l2m2m mpeg2_cuvid )
 DEV.L. mpeg4                MPEG-4 part 2 (decoders: mpeg4 mpeg4_v4l2m2m mpeg4_cuvid ) (encoders: mpeg4 mpeg4_v4l2m2m )
 D.V.L. vc1                  SMPTE VC-1 (decoders: vc1 vc1_v4l2m2m vc1_cuvid )
 DEV.L. vp8                  On2 VP8 (decoders: vp8 vp8_v4l2m2m vp8_cuvid ) (encoders: vp8_v4l2m2m )
 D.V.L. vp9                  Google VP9 (decoders: vp9 vp9_cuvid )
root@e4eed4755d62:/ffmpeg# 

可能出现的错误:
(1) libnpp not found.需要安装cuda-npp-<VERSION>软件包,其中VERSION是与CUDA版本相关,可使用命令apt list | grep cudart查看cuda-cudart的VERSION,与之相同即可。另外安装之后在编译ffmpeg源码时需要在参数--extra-ldflags中针对性地加入放置npp等文件的目录。
(2) libnvcuvid.so.1 not found. 在运行Docker窗口时需要使用nvidia-docker命令,并且加上参数-e NVIDIA_DRIVER_CAPABILITIES=compute,utility,video
(3) libcuda.so.1 not found. 在机器上使用find / -name libcuda*命令找出放置库文件的位置,并在编译ffmpeg源码时在参数--extra-ldflags中加入该目录。
(4) 运行/root/bin/ffmpeg -hwaccel cuvid -loglevel debug -c:v h264_cuvid -an -i rtmp://***/**/*** -f flv a.flv时出错:

[format @ 0x2fb7a80] Setting 'pix_fmts' to value 'yuv420p'
[auto_scaler_0 @ 0x3c2ed00] Setting 'flags' to value 'bicubic'
[auto_scaler_0 @ 0x3c2ed00] w:iw h:ih flags:'bicubic' interl:0
[format @ 0x2fb7a80] auto-inserting filter 'auto_scaler_0' between the filter 'Parsed_null_0' and the filter 'format'
Impossible to convert between the formats supported by the filter 'Parsed_null_0' and the filter 'auto_scaler_0'
Error reinitializing filters!
Failed to inject frame into filter network: Function not implemented
Error while processing the decoded data for stream #0:2
[AVIOContext @ 0x2f210c0] Statistics: 0 seeks, 0 writeouts
[AVIOContext @ 0x2f13e80] Statistics: 111319 bytes read, 0 seeks
[rtmp @ 0x2f11900] Deleting stream...

在命令中去除所有的hwaccel参数即可,此时依然使用硬件加速,可以使用nvidia-smi命令查看验证。

楼主直接打包好的包含tensorflow 1.13和ffmpeg均使用CUDA加速的docker镜像可以使用以下命令获取:docker pull bluespace/tf-ffmpeg-base

参考文献:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章