How to Set Thread Count in FFmpeg libx265
Controlling the number of threads used by the H.265/HEVC encoder
(libx265) in FFmpeg is essential for optimizing CPU
utilization, encoding speed, and system performance. This guide provides
a straightforward explanation of how to configure thread settings in
libx265 using both standard FFmpeg options and specific
x265 parameters.
By default, libx265 automatically detects your system’s
CPU cores and allocates resources to maximize encoding speed. However,
if you need to limit CPU usage or optimize multi-threading efficiency,
you can manually configure the thread count using two different
methods.
Method 1: Using the
-threads Option
The simplest way to limit threads in FFmpeg is by using the generic
-threads flag. This tells FFmpeg to limit the thread count
for the encoder.
ffmpeg -i input.mp4 -c:v libx265 -threads 4 output.mp4In this example, FFmpeg instructs the libx265 encoder to
restrict its processing to 4 threads. While this method is simple, it is
a global FFmpeg setting and may not offer the precise control that
libx265’s internal threading engine allows.
Method 2: Using
-x265-params (Recommended)
For precise control over how libx265 manages system
resources, you should pass threading arguments directly to the encoder
using the -x265-params option. The two main parameters for
thread management are pools and
frame-threads.
1. Controlling the Thread
Pool (pools)
The pools parameter determines the total number of
threads allocated to the encoder’s thread pool.
To limit the encoder to a specific number of threads (e.g., 6 threads):
ffmpeg -i input.mp4 -c:v libx265 -x265-params pools=6 output.mp4To restrict the encoder to a specific CPU socket or core list on multi-socket systems, you can specify cores directly (e.g.,
pools=0,2,4,6).To disable multi-threading entirely and use a single thread:
ffmpeg -i input.mp4 -c:v libx265 -x265-params pools=none output.mp4
2. Controlling Frame
Threads (frame-threads)
The frame-threads parameter dictates how many frames are
encoded in parallel. By default, libx265 decides this based
on your CPU core count. Manually adjusting this can reduce memory usage
or improve threading efficiency.
To specify the number of concurrent frames to encode:
ffmpeg -i input.mp4 -c:v libx265 -x265-params frame-threads=2 output.mp4
Combining Threading Parameters
For optimal results, you can combine these parameters inside the
-x265-params argument, separating them with a colon
(:).
ffmpeg -i input.mp4 -c:v libx265 -x265-params pools=8:frame-threads=3 output.mp4In this command, the encoder is restricted to a pool of 8 threads,
and it is instructed to encode up to 3 frames in parallel. This approach
provides the most reliable way to throttle or maximize the performance
of libx265 during video encoding.