FFMPEG with NVidia Hardware Acceleration

Erich Izdepski
3 min readSep 1, 2023

Here is how to use your Nvidia GPU to hardware accelerate video encoding with ffmpeg. First off, it is much faster — about 5x faster in my case, although that varies with quality settings. My GPU is an Nvidia 2060 with 12GB of RAM and my CPU is an AMD Ryzen 5 5600X. While your setup may differ, your GPU should perform the job faster than your CPU always.

General ffmpeg Commands

  • y — say ‘yes’ to prompts automatically, like overwrite existing video
  • i — the input file (with wildcards for decimal numbering)
  • vf — video filter
  • fps=30 — the video filter to use; this sets the frame rate to 30 fps
  • c:v — the video codec for encoding
  • pix_fmt — input image format
  • qp — this also affects quality (and bitrate) for both the CPU and GPU encoding. Lower values are better.
  • start_number — specify which frame to start on
  • frames:v — specify how many frames to render (in the output)
  • the last value is the output file name

CPU Encoding Command

ffmpeg -y -i “C:\Users\eizde\content\tmpSuperSloMo\output\%d.png” -vf fps=30 -c:v libx264 -pix_fmt yuv420p -qp 0 C:\Users\eizde\content\slomoplanetary-30fps.mp4

Your input file and output files will need to be changed, of course.

CPU encoding output

GPU Encoding Command (Nvidia Encoder)

ffmpeg -y -i “C:\Users\eizde\content\tmpSuperSloMo\output\%d.png” -vf fps=30 -c:v h264_nvenc -pix_fmt yuv420p -qp 0 C:\Users\eizde\content\slomoplanetary-30fps.mp4

GPU encoding output

ffmpeg Output Details

The ffmpeg output window shown is very interesting. This is what the data in the last line means.

  • frame — current frame number in the batch of input frames
  • fps — the encoding rate
  • q — a quality value, tied to the qp parameter. Lower q (0 is really good) is better. Low qp sets a low q.
  • size — how much of the video has been encoded, measured in bytes
  • time — how much of the video has been encoded, measured in hours:minutes:seconds
  • bitrate — the bitrate in kbps of the encoded video
  • speed — a factor indicating how much faster than real-time the encoder is operating at; will be equal to fps divided by the desired frame rate

I encoded over 22,000 frames into a video for this comparison. The only difference in the commands is for the encode, shown in bold (libx264 versus h264_nvenc).

Video Quality

ffmpeg lets you control quality in a variety of ways. One is the crf setting. Here is a sample of BAD quality CPU encoding using libx264 and a crf = 63. This is super low quality but fast and provides a small (but useless) file full of pixelation.

Another setting is qp. A value of 0 or 1 produces very good video.

crf=63; low quality output

Higher quality encode with a crf = 5, showing the video at the same time. Default crf is 17, by the way.

crf=5; high quality output

For the sake of completeness, here is the hardware encoded frame.

High quality GPU output

--

--

Erich Izdepski
Erich Izdepski

Written by Erich Izdepski

Software engineer and architect who’s built web, mobile and desktop apps in multiple industries over a span of more than 25 years. CTO @ BTS Software Solutions.