Reduce FFmpeg CPU Usage During Screen Recording

Recording high-resolution screens in real-time can heavily tax your processor, leading to dropped frames and laggy videos. This article provides actionable techniques to minimize CPU usage during FFmpeg screen capture, including leveraging hardware acceleration, optimizing encoder presets, adjusting frame rates, and offloading heavy processing tasks.

Use Hardware Acceleration (GPU Encoding)

The most effective way to reduce CPU usage is to offload the video encoding process to your graphics card (GPU). Modern GPUs feature dedicated hardware chips designed specifically for video encoding, leaving your CPU free for other tasks.

  • NVIDIA GPUs: Use the NVENC encoder by replacing the standard -c:v libx264 with -c:v h264_nvenc or -c:v hevc_nvenc.
  • Intel CPUs with Integrated Graphics: Use Quick Sync Video (QSV) with -c:v h264_qsv.
  • AMD GPUs: Use the Advanced Media Framework with -c:v h264_amf.
  • Apple Silicon / macOS: Use Apple's hardware encoder via -c:v h264_videotoolbox.

Example command utilizing NVIDIA NVENC:

ffmpeg -f gdigrab -framerate 30 -i desktop -c:v h264_nvenc -preset p1 -qp 23 output.mp4

Apply Ultrafast x264 Presets

If you must use your CPU for encoding (via the libx264 encoder), you can drastically reduce its workload by changing the encoder preset. The preset determines how much computational effort FFmpeg spends compressing the video.

  • Add -preset ultrafast or -preset superfast to your command.
  • While this results in larger file sizes, it requires the minimum CPU processing power during real-time capture. You can always compress the output file to a smaller size later.

Example command:

ffmpeg -f gdigrab -framerate 30 -i desktop -c:v libx264 -preset ultrafast -crf 23 output.mp4

Adjust Frame Rate and Resolution

High-resolution screens generate massive amounts of data per second. Reducing the amount of data FFmpeg has to process will instantly lower CPU utilization.

  • Lower the Frame Rate: Drop the capture rate from 60 FPS to 30 FPS or 24 FPS using the -framerate 30 input option.
  • Avoid Real-Time Scaling: Do not use filters like -vf scale=... during the live recording. Record at your desktop's native resolution and scale the video during post-processing.

Use Lightweight Input Devices

The API FFmpeg uses to grab your screen impacts performance. Ensure you are using the most efficient capture interface for your operating system:

  • Windows: Use gdigrab for general capture, or dshow with a third-party screen capture filter for better performance.
  • Linux: Use kmsgrab (kernel-mode setting grab) for low-overhead hardware-level capture, or xcbgrab for X11 environments.
  • macOS: Use the native avfoundation input device.

Optimize Threading

Ensure FFmpeg is utilizing your CPU cores efficiently. You can explicitly tell the encoder how many threads to use during the process.

  • Add the -threads 0 flag to your command to allow FFmpeg to automatically select the optimal number of threads based on your processor's architecture.