Ffmpeg realizes GPU (hard-coded) accelerated transcoding
# Project scene:
Tip: ubuntu16 transcodes video stream (monitoring stream) data, and uses GPU to achieve accelerated transcoding:
For example: convert h264 video stream data in one path to another path
Ready to work
Make sure the server has a GPUlspci | grep NVIDIA # 查看NVIDIA显卡
ubuntu-drivers devices
1. Dependent library installation:
nasmassembly compiler, required when compiling certain dependent libraries
cd ~/ffmpeg_sources
curl -O -L http://www.nasm.us/pub/nasm/releasebuilds/2.13.02/nasm-2.13.02.tar.bz2
tar xjvf nasm-2.13.02.tar.bz2
cd nasm-2.13.02
./autogen.sh
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin"
make
make install
yasm
assembly compiler, required when compiling certain dependent libraries
cd ~/ffmpeg_sources
curl -O -L http://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
libx264
H.264 video encoder, if you need to output H.264 encoded video, you need this library, so it can be said to be a must
cd ~/ffmpeg_sources
git clone --depth 1 http://git.videolan.org/git/x264
cd x264
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static
make
make install
libx265
H.265/HEVC video encoder.
If you don’t need this encoder, you can skip it and remove it in the configure command of ffmpeg. –enable-libx265
This path is very slow to download. If conditions permit, you can go directly to https://bitbucket.org/multicoreware to download to the window , And then move to that path
cd ~/ffmpeg_sources
hg clone https://bitbucket.org/multicoreware/x265_git
cd ~/ffmpeg_sources/x265/build/linux
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
make
make install
libfdk_acc
AAC audio encoder, necessary
cd ~/ffmpeg_sources
git clone --depth 1 --branch v0.1.6 https://github.com/mstorsjo/fdk-aac.git
cd fdk-aac
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
libmp3lame
MP3 audio encoder, essential
cd ~/ffmpeg_sources
curl -O -L http://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
tar xzvf lame-3.100.tar.gz
cd lame-3.100
./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --disable-shared --enable-nasm
make
make install
libops
OPUS Audio Encoder
If you don’t need this encoder, you can skip it and remove –enable-libopus in the configure command of ffmpeg
cd ~/ffmpeg_sources
curl -O -L https://archive.mozilla.org/pub/opus/opus-1.2.1.tar.gz
tar xzvf opus-1.2.1.tar.gz
cd opus-1.2.1
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
libogg
is dependent on libvorbis
cd ~/ffmpeg_sources
curl -O -L http://downloads.xiph.org/releases/ogg/libogg-1.3.3.tar.gz
tar xzvf libogg-1.3.3.tar.gz
cd libogg-1.3.3
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
make install
libvorbis
Vorbis audio encoder
If you don't need this encoder, you can skip it and remove it in the configure command of ffmpeg –enable-libvorbis
cd ~/ffmpeg_sources
curl -O -L http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.5.tar.gz
tar xzvf libvorbis-1.3.5.tar.gz
cd libvorbis-1.3.5
./configure --prefix="$HOME/ffmpeg_build" --with-ogg="$HOME/ffmpeg_build" --disable-shared
make
make install
libvpx
VP8/VP9 video codec
If you don’t need this codec, you can skip it and remove it in the configure command of ffmpeg –enable-libvpx
cd ~/ffmpeg_sources
git clone --depth 1 https://github.com/webmproject/libvpx.git
cd libvpx
./configure --prefix="$HOME/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm
make
make install
2. Compile and install ffmpeg 3.3.8
If you want to install other versions of ffmpeg, you can download it from this path http://ffmpeg.org/releases/, then move it to the virtual machine, and extract and execute it from the third stepcd ~/ffmpeg_sources
curl -O -L https://ffmpeg.org/releases/ffmpeg-3.3.8.tar.bz2
tar xjvf ffmpeg-3.3.8.tar.bz2
cd ffmpeg-3.3.8
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" \
--extra-ldflags="-L$HOME/ffmpeg_build/lib" \
--extra-libs=-lpthread \
--extra-libs=-lm \
--bindir="$HOME/bin" \
--enable-gpl \
--enable-libfdk_aac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libopus \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
--enable-libx265 \
--enable-nonfree
make
make install
hash -r
Verify installation
ffmpeg -h
At this point, we can use ffmpeg to transcode the video, but it is a soft coding method transcoding performed by the cpu, which is low in efficiency and takes up high cpu resources.
Implement GPU transcoding (hard coding):
1. Install cuda
First make sure that NVIDIA driver (graphics driver) is installednvidia-smi
watch -n 10 nvidia-smi #每10s查看一次
If the output is as follows, it means that the graphics card driver has been installed.
If the output is,
it means that there is a problem with the driver. When you need to install cuda, install the graphics card driver together (when you install cuda, it will remind you whether to install the graphics card driver, if not, then Choose y, after all, this method is the most convenient way to install the graphics driver)
Go to the CUDA official website to download the CUDA installation package https://developer.nvidia.com/cuda-downloads, select the corresponding version
For example: After
entering the installation command, you will enter this interface (for the previous protocol, keep pressing the enter key to skip)
After the download is complete, after the installation is complete, you can view the
output of nvcc -V through the command.
If it shows, try it through /usr/local/cuda-9.2/bin/nvcc -V. If it does not, then cuda has not been installed successfully. If there is the output of the above figure, it means that the installation is successful. You only need to configure the cuda environment variable.
Configuration method:
1、 sudo vim ~/.bashrc
2、 在文件末尾加入下面两行
export PATH=/usr/local/cuda-10.1/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64$LD_LIBRARY_PATH
3、 保存退出
4、 source ~/.bashrc
2. Recompile and install ffmpeg 3.3.8 to support GPU transcoding
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/lib64" \
--extra-libs=-lpthread \
--extra-libs=-lm \
--bindir="$HOME/bin" \
--enable-gpl \
--enable-libfdk_aac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libopus \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
--enable-libx265 \
--enable-nonfree \
--enable-cuda \
--enable-cuvid \
--enable-nvenc \
--enable-libnpp
make
make install
hash -r
After the installation is complete, enter the command ffmpeg -hwaccels to view the supported hardware acceleration methods.
Command ffmpeg -codecs | grep cuvid to view the provided encoder
example: Transcode the h265 encoded video (in my case, the monitoring stream) data through the GPU method h264 encoding
ffmpeg -y -vsync 0 -hwaccel cuvid -c:v hevc_cuvid -i rtsp://admin:mm852456@192.168.0.209:554/h264/ch1/main/av_stream -c:v h264_nvenc -b:v 1024k -f flv -y rtmp://192.168.0.98:11937/live/h265_1
-hwaccl cuvid specifies the use of cuvid hardware acceleration (vdpau, cuvid)
-c:v hevc_cuvid decoding rules
-i rtsp://admin:[email protected]:554/h264/ch1/main/av_stream input file (stream data)
- c:v h264_nvenc encoding rules
-f flv file output type designation
-y rtmp://192.168.0.98:11937/live/h265_1 where to output the specified path