The easiest way for ffmpeg to support nvidia hard codec

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

Because of the needs of the work content, I wrote an article about ffmpeg supporting Nvidia's hard codec. That method is more suitable for customized ffmpeg compilation. If you only use ffmpeg for hardware codec, it doesn't need to be so troublesome.
ffmpeg customized compilation supports cuda codec


1. What is nvidia hard codec?

This has already been said in the previous article, so I won't make more descriptions here. It can be understood as a specialized hardware that handles video decoding and encoding.

2. Use steps

首先你需要一张nvidia的卡,最好是GTX以上级别的,因为有些显卡没有硬件编解码器,比如GT920M这种早期笔记本显卡。

AV1 has obtained hardware support on the RTX30 series graphics card, because there is no 30 series graphics card in hand, so today we only talk about H264 and H265.

Today's is relatively simple, thanks to ffmpeg-3.4the function, we can install this version directly without compiling.

1. Install

Make sure your distribution's default ffmpeg version supports versions above 3.4, Ubuntu needs >= 18.04, Debian needs >= 9. Check out other distributions yourself.

The installation method is also relatively simple, just use the official source to install.
Ubuntu&Debian:

sudo apt update
sudo apt install ffmpeg

2. Confirm

Confirmation needs to use two options, one is -decoders, one is-encoders

The output here is too long, I only intercept a part.

1. Hardware decoder

ffmpeg -decoders
 V..... h264_cuvid           Nvidia CUVID H264 decoder (codec h264)
 VFS..D hap                  Vidvox Hap
 VFS..D hevc                 HEVC (High Efficiency Video Coding)
 V..... hevc_v4l2m2m         V4L2 mem2mem HEVC decoder wrapper (codec hevc)
 V..... hevc_cuvid           Nvidia CUVID HEVC decoder (codec hevc)
 V....D hnm4video            HNM 4 video
 V....D hq_hqa               Canopus HQ/HQA
 VFS..D hqx                  Canopus HQX
 VF..BD huffyuv              Huffyuv / HuffYUV
 VF..BD hymt                 HuffYUV MT
 V....D idcinvideo           id Quake II CIN video (codec idcin)
 V....D idf                  iCEDraw text
 V....D iff                  IFF ACBM/ANIM/DEEP/ILBM/PBM/RGB8/RGBN (codec iff_ilbm)
 V....D imm4                 Infinity IMM4
 V....D indeo2               Intel Indeo 2
 V....D indeo3               Intel Indeo 3
 V....D indeo4               Intel Indeo Video Interactive 4
 V....D indeo5               Intel Indeo Video Interactive 5
 V....D interplayvideo       Interplay MVE video
 VFS..D jpeg2000             JPEG 2000
 VF...D libopenjpeg          OpenJPEG JPEG 2000 (codec jpeg2000)
 V....D jpegls               JPEG-LS
 V....D jv                   Bitmap Brothers JV video
 V....D kgv1                 Kega Game Video
 V....D kmvc                 Karl Morton's video codec
 VF...D lagarith             Lagarith lossless
 V....D loco                 LOCO
 V....D lscr                 LEAD Screen Capture
 V....D m101                 Matrox Uncompressed SD
 V....D eamad                Electronic Arts Madcow Video (codec mad)
 VFS..D magicyuv             MagicYUV video
 VF...D mdec                 Sony PlayStation MDEC (Motion DECoder)
 VF...D mimic                Mimic
 V....D mjpeg                MJPEG (Motion JPEG)
 V..... mjpeg_cuvid          Nvidia CUVID MJPEG decoder (codec mjpeg)
 V....D mjpegb               Apple MJPEG-B
 V....D mmvideo              American Laser Games MM Video
 V....D motionpixels         Motion Pixels video
 V.S.BD mpeg1video           MPEG-1 video
 V..... mpeg1_v4l2m2m        V4L2 mem2mem MPEG1 decoder wrapper (codec mpeg1video)
 V..... mpeg1_cuvid          Nvidia CUVID MPEG1VIDEO decoder (codec mpeg1video)
 V.S.BD mpeg2video           MPEG-2 video
 V.S.BD mpegvideo            MPEG-1 video (codec mpeg2video)
 V..... mpeg2_v4l2m2m        V4L2 mem2mem MPEG2 decoder wrapper (codec mpeg2video)
 V..... mpeg2_cuvid          Nvidia CUVID MPEG2VIDEO decoder (codec mpeg2video)
 VF..BD mpeg4                MPEG-4 part 2
 V..... mpeg4_v4l2m2m        V4L2 mem2mem MPEG4 decoder wrapper (codec mpeg4)
 V..... mpeg4_cuvid          Nvidia CUVID MPEG4 decoder (codec mpeg4)

All supported decoders with the suffix _cuvid are NVIDIA hardware decoding formats, and you can choose according to your needs.

2. Hardware encoder

General hardware is almost more capable of decoding than encoding, and Nvidia is no exception. It may not support as many encoding formats as decoding.

ffmpeg -encoders
 V..... h264_nvenc           NVIDIA NVENC H.264 encoder (codec h264)
 V..... h264_omx             OpenMAX IL H.264 video encoder (codec h264)
 V..... h264_v4l2m2m         V4L2 mem2mem H.264 encoder wrapper (codec h264)
 V..... h264_vaapi           H.264/AVC (VAAPI) (codec h264)
 V..... nvenc                NVIDIA NVENC H.264 encoder (codec h264)
 V..... nvenc_h264           NVIDIA NVENC H.264 encoder (codec h264)
 V..... hap                  Vidvox Hap
 V..... libx265              libx265 H.265 / HEVC (codec hevc)
 V..... nvenc_hevc           NVIDIA NVENC hevc encoder (codec hevc)
 V..... hevc_nvenc           NVIDIA NVENC hevc encoder (codec hevc)

Those with the suffix or prefix nvenc are hardware encoders. My graphics card only supports h264 and hevc, which are commonly known as H264 and H265, but different organizations call them differently.

3. Test

The test is also very simple, just use h264 decoding + h265 encoding, if it is normal, no error will be reported.

ffmpeg -rtsp_transport tcp -vcodec h264_cuvid -an -i <H264视频文件> -vcodec hevc_nvenc -an -t 100 -f mp4 -y output.mp4

特别说明:-an的意思是不要音频,因为只测试视频,要不然还要单独指定音频编码器

You can also use H265 file decoding + H264 file encoding, you need to reverse the operation.


Summarize

1. Both ffmpeg and nvidia support are required, and different versions may have different effects.
2. It is also possible that the early version of ffmpeg already supports nvidia, but the minimum Ubuntu version I use is 18.04, so I can't confirm it.

Guess you like

Origin blog.csdn.net/jiexijihe945/article/details/131912403