Forward

If transcoding a video with a high bit rate and a long duration, the transcoding suppression will take a long time and take up a lot of CPU resources. Therefore, it is particularly important to use GPU acceleration.

What is Soft & Hard codec

Generally, the soft codec uses the CPU which is easy to use while the hard codec uses dedicated chips like GPU which is faster.

In the past two years, Apple has repeatedly emphasized the codec capabilities of their M-series chips.

Apple’s Next-Generation Custom Technologies

M2 brings Apple’s latest custom technologies to the Mac, enabling new capabilities, better security, and more:

  • The media engine includes a higher-bandwidth video decoder, supporting 8K H.264 and HEVC video.

  • Apple’s powerful ProRes video engine enables playback of multiple streams of both 4K and 8K video.

Apple unveils M2 with breakthrough performance and capabilities - Apple

Install

Basic environment

  • ffmpeg version 2022-10-27-git-00b03331a0-full_build-www.gyan.dev Copyright © 2000-2022 the FFmpeg developers

  • Geforce RTX 2060 12GB

    Video Encode and Decode GPU Support Matrix

  • Intel i5-12400

  • Windows 10 Version 21H2

Processes

  • First, install CUDA. If you have already installed CUDA, input nvcc --version in your terminal once again.

  • Second, install FFmpeg (Windows). If you have already installed ffmpeg, input ffmpeg -version in your terminal once again.

  • Finally, input ffmpeg -hwaccels in your terminal and you could find cuda or cuvid.Here is CUDA

  • Or, input ffmpeg -codecs | findstr cuvid in your terminal and you could find many cuvid decoders. (My computer is Windows system if yours is Linux, you should use the command ffmpeg -codecs | grep cuvid)CODECS: cuvid

FFmpeg fast guide

The document is your best teacher.

Synopsis

ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...

Description

  • To set the video bitrate of the output file to 64 kbit/s:

    ffmpeg -i input.mp4 -c:a copy -c:v h264 -b:v 5M output.mp4
    
  • To force the frame rate of the output file to 24 fps:

    ffmpeg -i input.avi -r 24 output.avi
    
  • To force the frame rate of the input file (valid for raw formats only) to 1 fps and the frame rate of the output file to 24 fps:

    ffmpeg -r 1 -i input.m2v -r 24 output.avi
    

Hardware-accelerated transcoding based on FFmpeg

The transcoding process in ffmpeg for each output can be described by the following diagram:

 _______              ______________
|       |            |              |
| input |  demuxer   | encoded data |   decoder
| file  | ---------> | packets      | -----+
|_______|            |______________|      |
                                           v
                                       _________
                                      |         |
                                      | decoded |
                                      | frames  |
                                      |_________|
 ________             ______________       |
|        |           |              |      |
| output | <-------- | encoded data | <----+
| file   |   muxer   | packets      |   encoder
|________|           |______________|

FFmpeg pipeline transcoding using NVIDIA hardware acceleration:

FFmpeg pipeline transcoding using NVIDIA hardware acceleration

Common commands

  • -hwaccel: Specify Hardware Acceleration

  • -c:v: codec: video

  • -c:a: codec: audio

  • -b:v: bit rate: video

  • -b:a: bit rate: audio

  • -s: resolution

  • -bufsize: buffer size

  • -maxrate: max data rate

  • -preset slow: Slow is the best preset generally.

  • -pix_fmt p010le: The output pixel format is p010le which is 10bit.

Command example

ffmpeg –hwaccel cuvid -c:v h264_cuvid -i <input> -c:v h264_nvenc -b:v 2048k -vf scale_npp=1280:-1 -y <output>
  • -hwaccel cuvid: Specify to use cuvid hardware acceleration

  • -c:v h264_cuvid:Use h264_cuvid to decode video

  • -c:v h264_nvenc:Use h264_nvenc to encode video

  • -vf scale_npp=1280👎 Specify the width and height of the output video, note that this is different from the -vf scale=x:x used in soft decoding

Test

Source File

Source File

Result

Soft transcoding

(Measure-Command { ffmpeg -c:v h264 -i C0080.mp4 -c:v h264 -b:v 20000k -s 3840x2160 C0080_CPU.mp4 | Out-Default }).ToString()

Soft transcoding takes 00:00:38.5304951

Hard transcoding

(Measure-Command { ffmpeg -vsync 0 -hwaccel cuda -c:v h264_cuvid -i C0080.mp4 -c:v h264_nvenc -c:a copy -b:v 20000k -s 3840x2160 C0080_GPU.mp4 | Out-Default }).ToString()

Hard transcoding takes 00:00:08.5457092

Conclusion

Hard transcoding is 4.5 times faster than soft transcoding.

Some other useful articles

[FFmpeg-user] what is the difference between hwaccels cuda and cuvid?

wiki: HWAsselIntro

How to install FFmpeg with NVIDIA GPU acceleration on Linux - nixCraft

NVIDIA FFmpeg 转码指南; NVIDIA 技术博客

Using FFmpeg with NVIDIA GPU Hardware Acceleration :: NVIDIA Video Codec SDK Documentation

Referring

GPU-accelerated video processing with ffmpeg - Stack Overflow

FFMPEG 使用显卡加速转码 - 掘金

Postscript

I’m still learning English. If there are any problems related to language expression, please tell me. And of course, if there are any issues with the article itself, please contact me too.

Be a Neutral Listener, Dialectical Thinker, and Practitioner of Knowledge