How to Configure FFmpeg libx264 Threading

This article explains how to configure and optimize the frame-threading and sliced-threading options in FFmpeg’s libx264 encoder. You will learn the differences between these two threading methods, how they impact encoding latency and performance, and the specific command-line arguments needed to control them.


Understanding Frame-Threading vs. Sliced-Threading

By default, the libx264 encoder in FFmpeg automatically manages threading based on your system’s CPU cores. However, you can manually control how work is distributed using two primary methods:


Configuring Frame-Threading

Frame-threading is the default behavior when you specify multiple threads in FFmpeg without enabling slice-based processing.

To set a specific number of threads for frame-threading, use the -threads option:

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

In this command: * -threads 4 instructs libx264 to use 4 threads to encode 4 frames in parallel. * Setting -threads 0 (the default) allows the encoder to automatically select the optimal number of threads based on your CPU.


Configuring Sliced-Threading

To enable sliced-threading, you must explicitly tell libx264 to use slice-based parallelization instead of frame-based parallelization. This can be achieved in two ways: using FFmpeg’s native options or passing parameters directly to libx264.

Method 1: Using native FFmpeg flags

You can enable sliced-threading by setting the -sliced_threads flag to 1 along with your desired thread count:

ffmpeg -i input.mp4 -c:v libx264 -threads 4 -sliced_threads 1 output.mp4

Method 2: Using libx264 private parameters

You can pass the sliced-threads=1 option directly to the encoder using the -x264-params or -x264opts flag. This is the most reliable way to force sliced-threading:

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

Optimizing Sliced-Threading with Slices

For sliced-threading to work effectively, the video frame must be split into slices. If you do not specify the number of slices, the encoder will automatically determine them based on your thread count. You can manually control this using the -slices option to match your thread count for optimal resource utilization:

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

Disabling Threading (Single-Threaded Encoding)

If you need to disable all parallel processing for debugging, compatibility, or strict resource limitation, set the thread count to 1:

ffmpeg -i input.mp4 -c:v libx264 -threads 1 output.mp4