Limit FFmpeg CPU Threads for Specific Encoder

By default, FFmpeg utilizes as many CPU cores as possible to speed up the encoding process, which can lead to 100% CPU utilization and system slowdowns. This article explains how to restrict the number of CPU threads used by a specific video or audio encoder in FFmpeg using both generic stream specifiers and encoder-specific parameters.

Using the Generic Threads Option

The most straightforward way to limit CPU threads for a specific encoder is to use the -threads option combined with a stream specifier. Placing -threads directly after the codec selection ensures the limit applies only to that specific encoder.

To limit the video encoder to 4 threads, use the -threads:v option:

ffmpeg -i input.mp4 -c:v libx264 -threads:v 4 -c:a copy output.mp4

In this command: * -c:v libx264 selects the H.264 video encoder. * -threads:v 4 restricts the video encoder to exactly 4 CPU threads. * -c:a copy copies the audio stream without re-encoding, ensuring no CPU cycles are wasted on audio.

Encoder-Specific Threading Controls

Some external libraries used by FFmpeg, such as libx264 and libx265, have their own internal threading management. For the most reliable results, you can pass threading parameters directly to these encoders.

For H.264 (libx264)

You can pass the threads parameter directly to the libx264 encoder using the -x264-params flag:

ffmpeg -i input.mp4 -c:v libx264 -x264-params threads=4 output.mp4

For H.265 / HEVC (libx265)

The libx265 encoder handles threading differently, using “thread pools” to map to CPU sockets and cores. To limit libx265 to 4 threads, use the pools parameter within -x265-params:

ffmpeg -i input.mp4 -c:v libx265 -x265-params pools=4 output.mp4

Setting pools=4 restricts the encoder to a pool of 4 threads. If you want to restrict it to a specific CPU socket or core range, you can specify that as well (e.g., pools=none or pools=+4).

For VP9 (libvpx-vp9)

For Google’s VP9 encoder, you must specify the -threads option alongside the -row-mt (row-based multi-threading) option to ensure threads are utilized efficiently within the limit:

ffmpeg -i input.mp4 -c:v libvpx-vp9 -threads 4 -row-mt 1 output.webm

Verifying Thread Limitation

To confirm that the thread restriction is working: 1. Run your FFmpeg command. 2. Open your system’s activity monitor (e.g., htop or top on Linux/macOS, or Task Manager on Windows). 3. Observe the CPU utilization. If restricted correctly, the FFmpeg process should only consume a percentage of CPU equivalent to the number of threads allocated (for example, 400% CPU utilization on a multi-core system represents 4 fully utilized threads).