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

參考文獻:

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