FFMPEG uses graphics card to accelerate transcoding

Foreword

When transcoding a video, if the video is relatively large and takes a long time, the transcoding suppression will take a long time and occupy the cpu, so I wonder if there is any way to speed up the transcoding progress, so this article records how to call FFMPEG The graphics card accelerates the transcoding or compression.

The distinction between soft and hard codecs:

soft coding: use the CPU for coding

Hard coding: use non-CPU for coding, such as graphics card GPU, dedicated DSP, FPGA, ASIC chip, etc.

The difference between soft and hard codecs:

Soft coding: It is direct and simple to implement, easy to adjust parameters, and easy to upgrade, but the CPU load is heavy, and the performance is lower than hard coding. The quality at low bit rates is usually better than hard coding.

 Hard coding: high performance, and the quality is usually lower than that of soft coding at low bit rates, but some products have transplanted excellent soft coding algorithms (such as X264) on the GPU hardware platform, and the quality is basically equivalent to soft coding. 

Simply put, hard decoding is the process of memory – codec circuit – memory/display, so the speed will be faster than software codec
basic environment

  • FFMPEG BUILDS 4.4.1
    FFmpeg Builds
  • GeForce GTX 1060 (NVIDIA)
    Graphics card support list
  • windows 10

Environment construction

Install FFMPEG

Download

download link

Select the appropriate version to download according to the system

Select the version to download, I downloaded ffmpeg-release-essentials.zip

Environment configuration

Compress ffmpeg-release-essentials.zip to the specified directory, enter the bin directory, there are three files in it
ffmpeg.exe ffplay.exe ffprobe.exe

Among them, we often use ffmpeg.exe. At this time, ffmpeg version can be executed in the directory

PS D:\Software\ffmpeg\bin> ffmpeg -version
ffmpeg version 2021-05-05-git-7c451b609c-full_build-www.gyan.dev Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 10.2.0 (Rev6, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable -gmp --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable -libcaca --enable-sdl2 --enable-libdav1d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable -libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable -amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libglslang- -enable-vulkan --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable- libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
libavutil 57.0.100 / 57.0.100
libavcodec 59.0.100 / 59.0.100
libavformat 59.0.101 / 59.0.101
libavdevice 59.0.100 / 59.0.100
libavfilter 8.0.101 / 8.0.101
libswscale 6.0.100 / 6.0.100
libswresample 4.0.100 / 4.0.100
libpostproc 56.0.100 / 56.0.100

The above information proves that the installation is successful. If you do not want to enter the directory or use absolute paths every time, you can configure environment variables.

Install the CUDA driver

CUDA is a driver that allows the GPU to perform related operations, because we generally do not directly control the GPU when developing applications, but operate through the driver.

Front

First determine whether your computer supports CUDA

Video Encode and Decode GPU Support Matrix

download

Download place

Select system type to download

Install

Follow the prompts to install by default.

Command related

Hardware supported by ffmpeg

Hardware acceleration method

Use the command ffmpeg -hwaccels to query the hardware accelerators supported by the system.

ffmpeg -hwaccels
----------------------------------------------
cuda
dxva2
qsv
d3d11va
opencl
vulkan

PS: The above output represents the hardware acceleration method supported by the system, which will be used later

hardcoded suffix explained

  • qsv: Quick sync video for intel graphics cards
  • nvenc: nvidia graphics hardware video encoder (nvidia hardware video encoder)
  • cuvid: The old name of nvdec, only the decoder.
  • cuda: Same as above
  • amf: the amf hardware encoder of the amd graphics card (amd hardware encoder)

Basically all Intel computers support the qsv method.

codec support

ffmpeg-codecs

query encoder

ffmpeg -codecs

You can follow the sls command to query the required encoding, for example: ffmpeg -codecs | sls nvenc

ffmpeg -codecs | sls nvenc
-------------------------------------------------- -------------
DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_qsv h264_cuvid ) (encoders:
 libx264 libx264 libx264rgb h264_amf h264_nvenc h264_qsv )
 DEV.L. hevc H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_qsv hevc_cuvid ) (encoder
s: libx265 hevc_amf hevc_nvenc hevc_qsv )

To query the codec formats supported by the system, you need to use the two commands ffmpeg -decoders and ffmpeg -encoders.

Query codec options

Query options for encoder hevc_nvenc

ffmpeg -h encoder=hevc_nvenc

Query the detailed information of the decoder h264_qsv

ffmpeg -h decoder=h264_qsv

ffmpeg-decoders

The following uses h264 as a test

ffmpeg -decoders | sls h264
-------------------------------------------------- ----
VFS..D h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
V....D h264_qsv H264 video (Intel Quick Sync Video acceleration) (codec h264)
V.....h264_cuvid Nvidia CUVID H264 decoder (codec h264)

PS: h264_qsv , h264_cuvid, h264 : are the supported decoding methods

sls : equivalent to grep in linux

h264_qsv: Intel hard decoding

h264_cuvid: Nvidia hard decoding

ffmpeg-encoders

Query with the decoding format

ffmpeg -encoders | sls h264
 -------------------------------------------------- ------
 V..... libx264 libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (codec h264)
 V..... libx264 libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (codec h264)
 V..... libx264rgb libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB (codec h264)
 V....D h264_amf AMD AMF H.264 Encoder (codec h264)
 V....D h264_nvenc NVIDIA NVENC H.264 encoder (codec h264)
 V..... h264_qsv H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration) (codec h26)

sls : equivalent to grep in linux

If there is a similar output above, it proves that it supports hard decoding or hard coding.

Hardware accelerated commands

ffmpeg -hwaccel cuvid -c:v h264_cuvid -i input.mp4 -c:v h264_nvenc -b:v 2048k -s 1920x1080 output.mp4

Explanation of Common Command Parameters

  • -hwaccel cuvid: use cuvid for hardware acceleration
  • -c:v h264_cuvid: use h264_cuvid to decode
  • -c:v h264_nvenc: use h264_nvenc for encoding (nvidia hardware accelerated x265)
  • -b:v 2000k: bit rate, the higher the value is, the clearer it is.
  • -s 1920x1080: resolution
  • -b:v 3500K : The average video bitrate is 3500K
  • -bufsize 6000K: The size of the buffer area is 6000K, it is recommended to set it to the current code rate frame rate5, maybe the 2pass here is for the buffer, and it will be cached for the next 5s
  • -maxrate 5000K: The maximum bit rate is 5000K, and the maximum bit rate of each frame does not exceed this value
  • -preset slow: The preset scheme is slow, slow is already the best option
  • -pix_fmt p010le: The output pixel format is p010le, which is 10bit, only supported by new hardware, for example: 970m does not support, 1070 can support

Summarize
The above is the overall process of using ffmpeg hardware to accelerate transcoding. If you have any questions or comments, you are welcome to discuss.

Reference

NVIDIA FFmpeg Transcoding Guide

ffmpeg-with-nvidia-gpu

Author: Leaf1993
Original link: FFMPEG uses graphics card to accelerate transcoding – Nuggets

Business card at the end of the article can receive audio and video development learning materials for free, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmap, etc.

See below!↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓