Bind FFmpeg to CPU Cores Using Taskset in Linux

Binding an FFmpeg process to specific CPU cores or sockets allows you to control system resource utilization, prevent high-load encoding jobs from starving other processes, and improve cache efficiency on multi-socket NUMA systems. This guide explains how to use the Linux taskset utility to set CPU affinity for FFmpeg, covering how to launch new encoding commands on designated cores, bind processes to entire CPU sockets, and modify the affinity of an already running process.

Identify Your CPU Topology

Before assigning cores, identify your system's layout using lscpu:

lscpu

Look for the following fields:

  • CPU(s): Total logical processors (e.g., 0–15).
  • Socket(s): Number of physical processor sockets.
  • NUMA node(s) or Core(s) per socket: Shows how logical cores map to physical sockets.

To view which specific logical CPUs belong to which socket, run:

lscpu -e=CPU,CORE,SOCKET,NODE

Launch FFmpeg on Specific CPU Cores

Use the taskset -c command followed by a comma-separated list or a range of CPU core IDs to launch your FFmpeg job.

Bind to a Single Core

To run FFmpeg strictly on CPU core 0:

taskset -c 0 ffmpeg -i input.mp4 -c:v libx264 output.mp4

Bind to Multiple Specific Cores

To run FFmpeg on cores 0, 2, and 4:

taskset -c 0,2,4 ffmpeg -i input.mp4 -c:v libx264 output.mp4

Bind to a Range of Cores

To allocate cores 0 through 3 (a total of 4 cores) to FFmpeg:

taskset -c 0-3 ffmpeg -i input.mp4 -c:v libx264 output.mp4

Note: You can also pass the -threads flag to FFmpeg to match the number of allocated cores (e.g., -threads 4), ensuring the internal thread pool aligns with your affinity mask.

Bind FFmpeg to a Specific CPU Socket

On multi-socket servers, pinning a process to a single socket keeps memory access local to that socket's NUMA node, reducing latency.

  1. Determine the cores assigned to your target socket (for example, Socket 1 manages cores 8 through 15):
lscpu | grep "NUMA node"
  1. Pass that entire core range to taskset:
taskset -c 8-15 ffmpeg -i input.mp4 -c:v libx264 -threads 8 output.mp4

Alternatively, if numactl is installed, you can bind directly by physical node or socket:

numactl --cpunodebind=1 --membind=1 ffmpeg -i input.mp4 -c:v libx264 output.mp4

Change Affinity for an Already Running FFmpeg Process

If FFmpeg is already executing, retrieve its Process ID (PID) and reassign its CPU affinity without stopping the encoding job.

  1. Locate the PID:
pgrep -a ffmpeg
  1. Apply the new core mask using taskset -cp:
taskset -cp 0-3 <PID>
  1. Verify that the affinity has updated:
taskset -cp <PID>

The output will confirm the current list of allowed CPU cores for that process.