Limit FFmpeg CPU Threads on Linux
By default, FFmpeg maximizes your system’s resources by utilizing all
available CPU cores to speed up video and audio processing. While this
speeds up encoding, it can completely freeze your system or starve other
applications of processing power. Fortunately, you can easily restrict
FFmpeg’s resource consumption by using its native -threads
flag, pinning it to specific cores with the taskset
command, or utilizing cgroups and nice values for advanced
system prioritization.
Method 1: Using
the Native FFmpeg -threads Flag
The simplest and most effective way to control CPU usage is by
telling FFmpeg directly how many threads to create. You do this by
passing the -threads option into your command.
To limit FFmpeg to a specific number of threads, place the
-threads flag after your input file but
before your output file:
ffmpeg -i input.mp4 -threads 4 output.mp4- How it works: In this example, FFmpeg is strictly limited to using 4 CPU threads.
- The Default Behavior: Setting
-threads 0tells FFmpeg to use its optimal default, which usually means filling up every available thread on your processor.
Note: Some specific video codecs (like
libx264orlibx265) have their own internal threading management. If you want to ensure the codec itself obeys your limits, you can pass codec-specific arguments like this:ffmpeg -i input.mp4 -c:v libx264 -x264opts threads=4 output.mp4
Method 2:
Hard-Limiting Cores with taskset
If you want to prevent FFmpeg from touching specific CPU cores
entirely, you can use the Linux utility taskset. This sets
the “CPU affinity” for the process, forcing Linux to run FFmpeg only on
the hardware cores you permit.
To run FFmpeg on specific CPU cores (for example, cores 0, 1, 2, and 3):
taskset -c 0-3 ffmpeg -i input.mp4 output.mp4Alternatively, if FFmpeg is already running and you want to throttle it mid-process, find its Process ID (PID) and restrict it on the fly:
taskset -cp 0-3 <PID>Method 3:
Managing Priority with nice and cpulimit
Sometimes, you don’t care how many threads FFmpeg uses, as long as it doesn’t make your desktop lag. In these scenarios, changing the process priority or capping the overall CPU percentage is a better approach.
Lowering Priority with
nice
The nice command tells Linux to give FFmpeg a lower
scheduling priority. Other tasks (like your web browser or code editor)
will get first dibs on the CPU.
nice -n 19 ffmpeg -i input.mp4 output.mp4A value of 19 is the “nicest” possible setting, meaning FFmpeg will quietly process in the background using only idle CPU cycles.
Capping Total CPU
Percentage with cpulimit
If you want to ensure FFmpeg never exceeds a strict total percentage
of your CPU capacity (e.g., maximum 200%, which equals 2 full cores),
use the cpulimit utility:
cpulimit -l 200 -- ffmpeg -i input.mp4 output.mp4Summary of Best Practices
| Goal | Recommended Tool | Command Example |
|---|---|---|
| Standard restriction | FFmpeg -threads |
ffmpeg -i in.mp4 -threads 2 out.mp4 |
| Strict hardware isolation | Linux taskset |
taskset -c 0-1 ffmpeg -i in.mp4 out.mp4 |
| Prevent system lag | Linux nice |
nice -n 19 ffmpeg -i in.mp4 out.mp4 |