📜 ⬆️ ⬇️

Effective video encoding in Linux with Nvidia NVENC: part 1, general


This article contains practical information useful for organizing efficient video coding on Linux using the latest Nvidia video processors.
What this article is not:

About NVENC


Nvidia NVENC is a technology that allows encoding video in H.264 (and H.265 for the latest Maxwell processors) using GPU speeds of several hundred frames per second (depending on the profile and resolution). The vendor provides the ability to use this technology in the form of the NVENC SDK developer kit.

Implementation


There are many utilities for various platforms, which in some form give access to the SDK functionality. In March 2015, the release of ffmpeg was released , which included support for NVENC. ffmpeg occupies a central position among the entire set of free and not very software for working with video, so from a practical point of view, it is most interesting. This tool can and process video files ( 1 , 2 ), and stream streaming ( 1 , 2 ). Since the March release, another has been released, which included even more options for the nvenc codec and H.265 (HEVC) support.

Build FFmpeg with support for NVENC we will do.
')
Deployment

To create a working configuration we need:


CUDA drivers and runtime

Drivers and libraries install at once in one package. From the repository is better not to use anything.
sudo apt-get update sudo apt-get -y install axel build-essential dkms axel 'http://developer.download.nvidia.com/compute/cuda/7_0/Prod/local_installers/cuda_7.0.28_linux.run' chmod +x cuda_7.0.28_linux.run sudo ./cuda_7.0.28_linux.run 

All questions of a reputable computer should be answered in the affirmative. After installation - restart.

Nvenc header files

To build applications with nvenc sdk, some header files are required. Previously, they needed to be pulled from the Windows SDK version. But progress is taking place by leaps and bounds and now this file can be pulled from examples in the Linux SDK. Find in the examples (the files were installed with runtime and drivers in the previous step)
nvCPUOPSys.h
nvEncodeAPI.h
nvFileIO.h
NvHWEncoder.h
nvUtils.h

and put them in / usr / include. They will be needed only once, when building ffmpeg. On all machines they are not needed.

FFMPEG

It is better to start assembling it with installation of all assembly dependencies.
 sudo apt-get build-dep libav sudo apt-get install -y libfdk-aac-dev libopencv-dev 

I could forget something, something could change. To find the missing file to build, feel free to use apt-file:
 $ sudo apt-get install apt-file -y $ sudo apt-file update $ apt-file search libxcb-shm.so.0 libxcb-shm0: /usr/lib/x86_64-linux-gnu/libxcb-shm.so.0 libxcb-shm0: /usr/lib/x86_64-linux-gnu/libxcb-shm.so.0.0.0 


We download the latest version of ffmpeg from the site, unpack, configure, build and install:
 axel 'http://ffmpeg.org/releases/ffmpeg-2.7.1.tar.bz2' tar xf ffmpeg-2.7.1.tar.bz2 cd ffmpeg-2.7.1 ./configure --cpu=native --enable-pthreads --extra-version=habrahabr.ru --enable-bzlib --enable-libdc1394 --enable-libfreetype --enable-frei0r --enable-gnutls --enable-libgsm --enable-libmp3lame --enable-librtmp --enable-libopencv --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-vaapi --enable-vdpau --enable-libvorbis --enable-libvpx --enable-zlib --enable-libfdk-aac --enable-nonfree --enable-gpl --enable-swscale --enable-libcdio --enable-x11grab --enable-libx264 --enable-libxvid --enable-libopencore-amrnb --enable-version3 --enable-libopencore-amrwb --enable-version3 --enable-libvo-aacenc --enable-version3 --enable-libvo-amrwbenc --enable-version3 --enable-nvenc make -j5 sudo make install 

We compile with libfdk-aac, it is definitely preferable to the existing aac codec. Among other things, you can notice the option --enable-nvenc. Otherwise, the parameters are chosen based on the builds of the libav package in the ubuntu distribution itself. After all the manipulations, a working ffmpeg should appear in / usr / local / bin / ffmpeg.

Application

Launch

The launch options are exactly the same as in software coding, but instead of libx264, the video codec will be called nvenc (aka nvenc_h264) for h.264 and nvenc_hevc for h.265.
 /usr/local/bin/ffmpeg -i input.mov -vcodec nvenc -b:v 5000k -r 30 ... output.mp4 


Performance and quality

To produce 1 hour of video in 4 qualities with good transcoding accuracy, you need to either spend about 1 hour of computer time on two six-core xeons, or a little more than half an hour of desktop with such a video card. The quality of the results is indistinguishable from the x264 software codec with similar settings.

Restrictions

Alas, Nvidia has imposed a restriction: you cannot encode more than two video streams simultaneously on regular desktop video cards. The removal of this restriction will be devoted to my next article, which is likely to be of interest to a slightly different circle of readers.

Source: https://habr.com/ru/post/262507/


All Articles