FFmpeg + NVIDIA AV1 Hardcoded Video Transcoding and Compression Getting Started Tutorial

FFmpeg + NVIDIA AV1 硬编码 视频转码压制入门教程

foreword

Recently, I got an RTX4080. I heard that it supports AV1 hard codec, so I started recording most of my videos in AV1 format.

There is no problem when using OBS to record AV1 hard-coded video (should be v28.1+)

However, when transcoding and suppressing video, I encountered a lot of problems, such as slow speed, not running GPU and other problems frequently appeared (note that SVT-AV1 does not support N card hard solution, it will run CPU), various well-known and non-famous I tried several transcoding programs, but all failed (including Format Factory, AdobePR, AdobeMediaEncoder, ShanaEncoder, NotEnoughAV1Encodes, Xiaomaru Compression Tool, Bicut, Quick Edit, etc.) to fully utilize the hardware encoding and decoding capabilities of the N card.

So after a lot of tossing, I can only start playing the ultimate king FFmpeg (it should be noted that most of the above-mentioned software and other video transcoding software are nested in FFmpeg. Stupid software does not directly support AV1) (but I heard that the professional version of Clipping supports AV1 hard coding)

It should be noted that this article refers to N card-related commands, and non-N cards may not be available. In addition, the AV1 encoder requires RTX40 series and other graphics cards.

FFmpeg installation

At the beginning, I followed the tutorials on the N card official website, but I downloaded the CUDA toolkit and other software packages. After compiling for more than an hour, I was told that a stupid thing called librtmp failed and could not continue. After checking, there was no solution, so I had to give up temporarily.

Painful compilation process ==> expand/shrink

QQ%E5%9B%BE%E7%89%8720230107094829.png

QQ%E5%9B%BE%E7%89%8720230107094907.png

QQ%E5%9B%BE%E7%89%8720230107094946.png

QQ%E5%9B%BE%E7%89%8720230107095109.png

QQ%E5%9B%BE%E7%89%8720230107095619.png

QQ%E5%9B%BE%E7%89%8720230107095750.png

QQ%E5%9B%BE%E7%89%8720230107100003.pngQQ%E5%9B%BE%E7%89%8720230107100251.png

QQ%E5%9B%BE%E7%89%8720230107114012.png

Then the compilation is broken, so I can only find someone else to compile it. At first, I thought that the official website did not have it. I went to Baidu and Google to check the results but found no usable one.

Then I looked at the WIN download method on the official website, and provided me with 2 third-party websites. At first, I thought that the first one was not compiled, it was just a warehouse mirror, so I opened the second download to see https:// github.com/BtbN/FFmpeg-Builds/releases

As a result, the second automatic compilation date of this stupid thing looks quite new. As a result, I found that the CUDA AV1 encoder that does not support the N card was used, relying on the reply

Then go back and look at the first one, and after a careful look, you find that there is the latest compilation, but the release version below is old.

Then download the latest compiled version directly, and finally test the CUDA AV1 encoder that does support N cards https://www.gyan.dev/ffmpeg/builds/

After downloading, it is directly a binary file, just run it with CMD.

initial use

The method of opening CMD, for example, my FFmpeg.exe is in D:\Program Files\ffmpeg\bin

The first method is to open this directory directly through the file manager, enter cmd in the address bar and press Enter, so that you can directly open this directory in cmd

The second method is to open cmd through WIN+R or search. At this time, the default directory is the user folder of the C drive. You need to switch the disk first and then open the directory with cd

Enter first (note the English symbols)

 D:

re-enter

 cd D:\Program Files\ffmpeg\bin

Interpretation of common parameter configuration

-y Do not prompt, directly overwrite the output file.

-n Do not overwrite the output file, exit directly if the output file already exists.

-vsync 0 video synchronization method (0: straight through, 1: CFR, 2: VFR, drop, -1: auto) [this should be directly unnecessary]

//passthrough each frame from the decoder to the encoder, the timestamp remains the same

cfr If an output frame rate is specified, input frames are copied as necessary (if the output frame rate is greater than the input frame rate) or dropped (if the output frame rate is less than the input frame rate)

vfr input frame from decoder to encoder, the timestamp remains the same; if a frame with the same timestamp appears, it is discarded

drop Same as passthrough, but clear the timestamps of all frames

According to NV’s official website instructions and examples (Use -vsync 0 option with decode to prevent FFmpeg from creating output YUV with duplicate and extra frames.) It is better to follow the selection of 0 through

However ffmpeg mentioned in the prompt

-vsync is deprecated. Use -fps_mode

Passing a number to -vsync is deprecated, use a string argument as described in the manual.

It seems that it has been deprecated, and now uses -fps_mode (but there will be no error when entering vsync)

-fps_mode 0 [This can be used directly]

//Set the video synchronization method/frame rate mode. vsync applies to all output video streams, but can be overridden by setting fps_mode to the stream. vsync is deprecated and will be removed in the future.

For compatibility reasons, some values ​​of vsync can be specified as numbers (shown in parentheses in the table below).

Straight through(0)

Each frame is passed from the demultiplexer to the multiplexer along with its timestamp.

Constant Frame Rate CFR (1)

Frames will be duplicated and dropped to achieve exactly the requested constant frame rate.

Variable Frame Rate VFR (2)

Frames are passed or dropped with their timestamps to prevent 2 frames from having the same timestamp.

reduce

Acts as a passthrough but destroys all timestamps, causing the muxer to generate new timestamps based on the frame rate.

automatic (-1)

Choose between cfr and vfr depending on the mux function. This is the default method.

-i input file path, such as “G:\EV\2022-12-29 10-10-07.mp4”

-hwaccel cuda -hwaccel_output_format cuda Additional parameters for hardware acceleration, using the CUDA tool of the N card

//According to the official website, to compile FFmpeg, the CUDA toolkit must be installed on the system, although the CUDA toolkit is not required to run FFmpeg to compile binary files.

-c:v av1_nvenc selects the av1 encoder (affects the encoding type of the output file)

//The NVENC plug-in in FFMPEG supports the following codecs [Note that some graphics cards support AV1 decoding but not AV1 encoding]

h264_nvenc: H264 (AVC) encoder

hevc_nvenc: H265 (HEVC) encoder

av1_nvenc: AV1 encoder [requires RTX40 series and above consumer graphics cards to support AV1 encoding]

-c:a Use the copy stream, and will not re-encode the input stream [it is only recommended to use this parameter in the case of the same encoding]

-b:v 15M Set the video bit rate of the output file to 15Mbps. Note that this is bit instead of byte. The ratio is 1 to 8. 15M is equivalent to 6.75G per hour

//The video code rate recommended by Bilibili is 4K greater than 20M, and 1080P is greater than 6M. Actually, after my video is transcoded by Bilibili, the 4K60 AV1 encoded video is at most about 17M (lower ones also have 4M, 9M, 12M )

–resize 1280×720 Specify the output file resolution (use the built-in adjuster in the cuvid decoder) [may need to be placed in front of the output file, but it doesn’t work in my actual test]

-vf scale=1280:720 Change the resolution of the output file (use the scale filter) [However, I reported an error directly after the actual measurement]

-vf scale_cuda=1280:720 Change the resolution of the output file (use the scale_cuda filter) [may need to be placed in front of the output file, behind the input file, this is available in actual measurement]

-ss 1:23.456 The start time is 1 minute 23 seconds 456 milliseconds

-t duration

-to Termination time [-to must be written before -i]

-bufsize 5M Set the rate control buffer size to 5M

-r frame rate

run command demo

Compress the high bit rate video to the specified bit rate, use AV1 encoding, do not change the parameters such as resolution and frame rate, and do not intercept the video

 ffmpeg -y -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -i "G:\EV\2023-01-06 10-31-56.mp4" -c:a copy -c:v av1_nvenc -b:v 15M "G:\EV\output.mp4"

Compress the high bit rate video to the specified bit rate, use AV1 encoding, do not change the parameters such as resolution and frame rate, and intercept the video (from 30s to 1 minute)

 ffmpeg -y -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -ss 0:30.000 -t 1:00.000 -i "G:\EV\2023-01-06 10-31-56.mp4" -c:a copy -c:v av1_nvenc -b:v 5M "G:\EV\output2.mp4"

Compress the high bit rate video to the specified bit rate, use AVC encoding, intercept the video (from 30s to 1min30s), and compress the video to 720P30FPS

 ffmpeg -y -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -ss 0:30.000 -to 1:30.000 -r 30 -i "G:\EV\2023-01-06 10-31-56.mp4" -vf scale_cuda=1280:720 -c:a copy -c:v h264_nvenc -b:v 2M "G:\EV\output3.mp4"

Demonstration renderings

The code rate of the original video is about 47M (19.8G), the video code rate after transcoding is 16M (6.74G), the encoding AV1 resolution 4K frame rate is 60, the video duration is 1 hour and 9 seconds, and the transcoding time is 28 minutes using RTX4080. Compared with CMD My 2.15X Speed ​​is estimated to be this time ratio, and then the copy stream may not be triggered when transcoding, because the decoder and encoder are running at the same time (but they are not enough to run, in the task manager, but the power consumption of the graphics card is not directly Take off, it is still a relatively low level, the CPU is basically not moving, and the memory and disk IO are not obviously abnormal)

QQ%E5%9B%BE%E7%89%8720230107123542.png

QQ%E5%9B%BE%E7%89%8720230107125210.png

QQ%E5%9B%BE%E7%89%8720230107130337.png

QQ%E5%9B%BE%E7%89%8720230107130414.png

References

https://docs.nvidia.com/video-technologies/video-codec-sdk/ffmpeg-with-nvidia-gpu/#video-encoding

https://docs.nvidia.cn/video-technologies/video-codec-sdk/ffmpeg-with-nvidia-gpu/#video-encoding

https://developer.nvidia.com/video-encode-and-decode-gpu-support-matrix-new

https://docs.nvidia.com/video-technologies/video-codec-sdk/pdf/Using_FFmpeg_with_NVIDIA_GPU_Hardware_Acceleration.pdf

https://ffmpeg.org/ffmpeg.html

This article is transferred from https://www.blueskyxn.com/202301/6785.html
This site is only for collection, and the copyright belongs to the original author.