1

I'm compressing some videos for archiving, and I came to this configuration which gives me a nice overall quality on a small size:

ffmpeg -threads 4 -y -hide_banner -i input.mp4 -vf scale=-2:480 -c:v libx264 -b:v 200k -preset slow -tune animation -movflags faststart -pix_fmt yuv420p -an -f mp4 -pass 1 NUL
ffmpeg -threads 4 -y -hide_banner -i input.mp4 -vf scale=-2:480 -c:v libx264 -b:v 200k -preset slow -tune animation -movflags faststart -pix_fmt yuv420p -pass 2 2-pass-x264.mp4

This output quality is acceptable for my needs:

x264 encoded frame

But I would like to enjoy hardware acceleration, it is way much faster.

So I tried:

ffmpeg -hwaccel nvdec -y -hide_banner -i input.mp4 -vf scale=-2:480 -movflags faststart -pix_fmt yuv420p -c:v h264_nvenc -b:v 200k -maxrate 2M -bufsize 1M -rc-lookahead:v 30 -rc:v vbr_hq -preset slow -profile:v high -level 5.1 -an h264_nvenc.mp4

But this gives me an output with worse overall quality, and in complex parts, things gets worse:

h264_nvenc encded frame

Is it possible to achieve near same overall quality (for low bitrate) using h264_nvenc encoder?

0

Not really no. Newer Turing based nvidia cards will produce better quality than previous generations. But since nvenc is fixed function, and not software running on the GPU it’s not upgradable, or modifiable except for a few options like presets.

2
  • I tried every possible option, this is really a shame.
    – user27704
    Mar 7 '20 at 18:40
  • It’s just the nature of hardware accelerated video encoding. There is no such thing as a free lunch.
    – SlimSCSI
    Mar 7 '20 at 18:41
2

h264_nvenc is able to encode in constant quality, at least from Pascal onwards. I had to do quite some digging around the internet to figure out how to properly use it, but finally found a working formula.

A command like below will record in constant quality, and the CQ value 26 will produce about the same but slightly better quality, measured with VMAF, as a libx264 encode with the following options: -vcodec libx264 -level 4.1 -profile:v high -tune film -preset medium -crf 23. The H264_Nvenc bitrate was about 19% higher than the libx264 encode, but it had a slightly better VMAF score. I made these tests on some 1080p60 game recording, but also on some 1080p movie trailers.

ffmpeg -i input.mkv -c:v h264_nvenc -preset hq -profile:v high -tune hq -2pass 0 -rc-lookahead 8 -bf 2 -rc vbr_hq -cq 26 -b:v 0 -maxrate 120M -bufsize 240M output.mkv

Important:

  • Unless you set the Max rate and buffer size via -maxrate 120M -bufsize 240M, the CQ mode doesn't behave accordingly and lowering its value won't yield better results. The Constant Quality mode is activated by specifying Variable Bitrate rate control together with a CQ parameter, a bitrate set to zero, and the above two parameters set to some large value which won't limit the output. Don't be afraid of those values, as bitrate will be quite good.
  • The limits above will force the encode to produce a Level 5.1 encoding. Even higher limits like 300/600 produced a video with Level labeled 6.2, which had trouble playing correctly in NLE's as Vegas Pro. If you require a certain maximum Level, ensure the maxrate and buffer size ar within the level's limits and check the resulted output.
1

Specify bitrate as -b:v 0 made the trick forme, the overall bitrate bumped from 2m to 16m.

ffmpeg -i in.mp4 -c:v h264_nvenc -b:v 0 out.mp4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy