OS:Ubuntu: 16.04 / 18.04
OpencCV: 4.2
Cuda: 8.0 / 9.0
Nivida Driver: 418.56 (GTX 1050ti) / 435 (GTX 1060)
CPU: i3-8300 3.70Ghz
ffmpeg: 4.2 / 4.2.4
nv_codec_headers: 8.2 / 9.0
Install Nvidia Driver
-
list available Nvidia Driver
1
ubuntu-drivers devices
-
add repoitory
1 2
sudo add-apt-repository ppa:graphics-drivers/ppa sudo apt update
-
instll Nvidia Driver
1
sudo apt install nvidia-xxx
Install CUDA
-
Check CUDA and Nvidia driver compatibility https://docs.nvidia.com/deploy/cuda-compatibility/index.html
-
Download Cuda runfile which determines by your os https://developer.nvidia.com/cuda-downloads
please choose not to install nvidia graphics driver, or the driver will be updated**
-
install cuda (ex: cuda 8.0 for ubuntnu 16.04 )
1
sudo sh cuda_8.0.61_375.26_linux.run
-
Add environment variables to ~/.bashrc
1 2 3 4
sudo nano ~/.bashrc export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH source ~/.bashrc
Install NV_codec (optional for gpu )
-
download nv_codec_headers 8.2: https://github.com/FFmpeg/nv-codec-headers/tree/sdk/8.2
-
install nv_codec_headers 8.2
1
sudo make install
nv_codec_headers 9.0需要cuda 9.0以上
Install FFmpeg 4.2.4
-
install necessary tool
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
sudo apt-get update -qq && sudo apt-get -y install \ autoconf \ automake \ build-essential \ cmake \ git-core \ libass-dev \ libfreetype6-dev \ libgnutls28-dev \ libsdl2-dev \ libtool \ libva-dev \ libvdpau-dev \ libvorbis-dev \ libxcb1-dev \ libxcb-shm0-dev \ libxcb-xfixes0-dev \ pkg-config \ texinfo \ wget \ yasm \ zlib1g-dev
-
install libx264:
1 2 3 4
git clone https://code.videolan.org/videolan/x264.git cd x264 sudo ./configure --enable-shared --disable-asm sudo make && sudo make install
-
installl ffplay
1
sudo apt-get install libsdl2-dev
-
install libfdk_aac (optional)
https://sourceforge.net/projects/opencore-amr/files/fdk-aac/
1 2
./configure make && sudo make install
1 2 3
vi /etc/ld/so.conf.d/aac.conf /usr/local/lib sudo ldconfig
-
install mp3lame (optional)
1
sudo apt-get install yasm libmp3lame-dev
-
download ffmpeg:
1
git clone https://git.ffmpeg.org/ffmpeg.git
-
configure command (if you need cuvid)
1
sudo ./configure --enable-libmp3lame --enable-libfdk_aac --enable-libx264 --enable-gpl --enable-cuda --enable-cuvid --enable-nvenc --enable-nonfree --enable-pic --enable-rpath --enable-shared --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --enable-libnpp --enable-ffplay
-
configure command (if you don’t need cuvid and sound but want cuda )
1
sudo ./configure --enable-libx264 --enable-gpl --enable-cuda --enable-nvenc --enable-nonfree --enable-rpath --enable-pic --enable-shared --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --enable-ffplay
-
compile ffmpeg
1
sudo make && sudo make install
-
add to envirnment
1 2 3 4 5 6 7
sudo vi ~/.bashrc export PATH=/usr/local/bin:$PATH source ~/.bashrc sudo nano /etc/ld.so.conf # dertermine on your ffmpeg install location /usr/local/ffmpeg/lib sudo ldconfig
for more build options https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
Test ffmpeg
Hardware acceleration methods:
|
|
cpu:
|
|
gpu:
|
|
默認下ffmpeg抓rtsp使用UDP,這會lose大量packet,使用TCP避免。
Video.mp4 | CPU | GPU |
---|---|---|
Real Time | 16s | 0.5s |
Bit rate | 1497kbps | 1479kbps |
rtsp://admin:ai123456@192.168.0.111 (30幀) | CPU | GPU |
---|---|---|
Real Time | 1m49s | 1m41s |
Bit rate | 1911kbps | 2067kbps |
Install Opencv
-
install required and optional package
1
sudo add-apt-repository -y "deb http://security.ubuntu.com/ubuntu xenial-security main"
1
sudo apt-get install -y libjpeg8-dev libjasper-dev libpng12-dev
1
sudo apt install build-essential cmake git pkg-config libgtk-3-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev
1
sudo apt install libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev
-
download opencv 4.2 and extra library
1 2 3 4 5 6 7
mkdir opencv_base cd opencv_base git clone https://github.com/opencv/opencv.git --branch=4.2.0 git clone https://github.com/opencv/opencv_contrib.git --branch=4.2.0 cd opencv_base/opencv/ mkdir build cd build
with cuda and opencv_world
check your cuda_arch_bin:https://developer.nvidia.com/cuda-gpus
|
|
without cuda (Simple)
|
|
Compile cuda && ffmpeg with Opencv 開發指令
include及lib都為相對路徑 (if you want to pack code )
Two .so files need to be at system location
libx264.so
|
|
libswresample.so
|
|
CUDA compiles to shared file (.so): (NV12toBGR)
|
|
FFmpeg gpu server compiles to shared file (.so)
|
|
FFmpeg gpu client compiles
|
|