Speed Up FFmpeg VMAF with Multi-Threading
This article explains how to significantly accelerate video quality assessment by running the VMAF (Video Multi-Method Assessment Fusion) filter in FFmpeg using multi-threading. You will learn the exact command-line parameters required to leverage multiple CPU cores, optimize resource usage, and drastically reduce the time it takes to compute VMAF scores.
Understanding VMAF Bottlenecks
Calculating VMAF scores is a highly CPU-intensive process because it
analyzes spatial and temporal features across every single frame of a
reference and distorted video. By default, FFmpeg’s libvmaf
filter may only run on a single thread or fail to utilize your CPU’s
full capacity, leading to slow analysis times. To overcome this, you
must explicitly enable multi-threading using the n_threads
option within the filter parameters.
The Multi-Threaded FFmpeg Command
To run VMAF with multi-threading, pass the n_threads
option inside the -filter_complex flag.
Here is the standard syntax:
ffmpeg -i distorted.mp4 -i reference.mp4 -filter_complex "[0:v][1:v]libvmaf=n_threads=4" -f null -Parameter Breakdown:
-i distorted.mp4: The compressed or altered video you want to test.-i reference.mp4: The original, uncompressed source video.-filter_complex "[0:v][1:v]libvmaf=n_threads=4": This maps both video inputs to thelibvmaffilter. Then_threads=4argument tells the VMAF library to allocate 4 CPU threads to the calculation.-f null -: Since you only need the VMAF score output (which prints in the terminal or saves to a log), this prevents FFmpeg from wasting resources rendering a new output video file.
Determining the Optimal Thread Count
Assigning too many threads can lead to diminishing returns due to thread synchronization overhead, while too few will underutilize your system.
- Physical Cores: As a general rule, set
n_threadsto match the number of physical cores (not virtual/hyperthreaded cores) available on your system. For an 8-core CPU, usen_threads=8. - RAM Constraints: Multi-threading VMAF increases memory consumption. Ensure your system has enough RAM (at least 2GB to 4GB per thread for 4K video analysis).
Advanced Speed Optimization: Subsampling
If multi-threading alone is not fast enough, you can combine it with
frame subsampling. The n_subsample parameter tells FFmpeg
to only calculate VMAF for every \(N\text{-th}\) frame.
ffmpeg -i distorted.mp4 -i reference.mp4 -filter_complex "[0:v][1:v]libvmaf=n_threads=8:n_subsample=5" -f null -In this example, the filter uses 8 threads and
processes every 5th frame (n_subsample=5).
This speed optimization maintains a high correlation with the actual
VMAF score of the entire video while cutting processing time by up to
80%.