How to Bind FFmpeg to a Specific CPU Core on Linux

Running resource-intensive FFmpeg video transcodes can easily consume 100% of your CPU, slowing down other essential services on your Linux system. This article provides a straightforward guide on how to use the taskset command to restrict FFmpeg to specific CPU cores or sockets (CPU affinity). By isolating FFmpeg to designated cores, you can optimize system performance and ensure other applications remain highly responsive.

Step 1: Identify Your CPU Topology

Before binding processes, you need to know which CPU cores are available. Run the following command in your terminal to view your system’s CPU layout:

lscpu

Look for the CPU(s): line to see the total number of cores (indexed starting from 0), and the NUMA node(s) or Socket(s) section if you want to bind FFmpeg to a specific physical CPU socket.

Step 2: Bind a New FFmpeg Process to Specific Cores

To launch a new FFmpeg process and immediately restrict it to specific CPU cores, use taskset with the -c (or --cpu-list) option, followed by the core numbers and your FFmpeg command.

Example 1: Bind to a single core (Core 0)

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

Example 2: Bind to a range of cores (Cores 0, 1, 2, and 3)

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

Example 3: Bind to specific, non-consecutive cores (Cores 1 and 3)

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

Step 3: Bind an Already Running FFmpeg Process

If FFmpeg is already running, you can change its CPU affinity on the fly using its Process ID (PID).

  1. Find the PID of the running FFmpeg process:

    pgrep ffmpeg
  2. Use taskset with the -p (pid) and -c options to assign the process to specific cores (e.g., cores 4 to 7 for PID 12345):

    taskset -cp 4-7 12345

Step 4: Bind FFmpeg to a Specific CPU Socket (NUMA node)

On multi-socket motherboards, you can bind FFmpeg to all cores belonging to a single CPU socket to maximize memory bandwidth and avoid inter-socket latency.

  1. Find which cores belong to which socket using lscpu:

    lscpu | grep "NUMA node"

    Output example: NUMA node0 CPU(s): 0-7,16-23

  2. Bind FFmpeg to the cores mapped to that specific socket:

    taskset -c 0-7,16-23 ffmpeg -i input.mp4 -c:v libx264 output.mp4