Ch. 11 · Measuring Video Quality in Practice

한국어日本語
9 min

Hands-On: Running libvmaf in ffmpeg and Reading the JSON

Try it yourself, from the basic command through measuring several models at once to pulling the average, 5th percentile, and minimum out of the per-frame log.

In Chapter 10 you got a taste of the one-line VMAF command. Here we extend it into a form that takes care of the measurement conditions. First, check whether your ffmpeg includes libvmaf.

Check that the libvmaf filter exists and see its options
ffmpeg -hide_banner -filters | grep libvmaf
ffmpeg -hide_banner -h filter=libvmaf
Input order matters

In the libvmaf filter, the first input is the one under test (distorted) and the second is the original (reference). The examples in the FFmpeg documentation use this order too. Swapping them can change the score, so always write -i result -i original.

Step 1: the basics. Align start times and save a JSON log
ffmpeg -i encoded.mkv -i reference.y4m -lavfi \
  "[0:v]setpts=PTS-STARTPTS[d];[1:v]setpts=PTS-STARTPTS[r];[d][r]libvmaf=log_fmt=json:log_path=vmaf.json:n_threads=4" \
  -f null -

# Near the last line it prints 'VMAF score: ...',
# and vmaf.json stores the per-frame scores and a summary (pooled_metrics).
Step 2: when the resolutions differ, scale the result up to the original size before comparing (e.g., 720p result vs. 1080p original)
ffmpeg -i encoded_720p.mkv -i reference_1080p.y4m -lavfi \
  "[0:v]scale=1920:1080:flags=bicubic,setpts=PTS-STARTPTS[d];[1:v]setpts=PTS-STARTPTS[r];[d][r]libvmaf=log_fmt=json:log_path=vmaf.json:n_threads=4" \
  -f null -
Step 3: default and NEG models in one run (named so you can tell them apart)
ffmpeg -i encoded.mkv -i reference.y4m -lavfi \
  '[0:v]setpts=PTS-STARTPTS[d];[1:v]setpts=PTS-STARTPTS[r];[d][r]libvmaf=model=version=vmaf_v0.6.1\\:name=vmaf|version=vmaf_v0.6.1neg\\:name=vmaf_neg:log_fmt=json:log_path=vmaf.json:n_threads=4' \
  -f null -

# The above assumes a shell like bash, where backslashes inside single quotes are passed through as-is.
# Separate the models with |, and separate each model's options with \\: (the style used in the FFmpeg docs).
# For the 4K model or phone mode, change only the version/enable_transform value and specify it the same way.
# (Option syntax can vary by version, so check with ffmpeg -h filter=libvmaf)
A note on paths

Windows paths like C:\... are prone to errors inside a filter string, because the colon (:) collides with the option separator. The simplest fix is to write log_path as a path relative to the working folder (vmaf.json).

Pulling the average, 5th percentile, and minimum out of the JSON

The three numbers from lesson 3 can be computed straight from the per-frame scores in the JSON. The Python below is a minimal example that uses only the standard library. It takes the 5th percentile the simple way: sort from lowest and pick the value at the 5% position (the value can differ slightly depending on how you compute quantiles).

vmaf.json → average, 5th percentile, minimum
import json

d = json.load(open("vmaf.json"))
scores = sorted(f["metrics"]["vmaf"] for f in d["frames"])
n = len(scores)

print("frames  :", n)
print("average :", sum(scores) / n)
print("5th pct :", scores[int(n * 0.05)])
print("minimum :", scores[0])

# If you measured two models together with names like vmaf_neg,
# change the key ("vmaf", "vmaf_neg") and pull them out the same way.
In the field: what to put in your measurement record

The items this site's measurement articles always include are also the minimum list for a reproducible record: the FFmpeg version, the model used, the input format (resolution, fps, pix_fmt), how many frames were used, the exact command, and the clip source and license. If you keep only the numbers and drop these, you can't compare again later. To put together the encoding options, you can use the FFmpeg command builder.

libvmaflibvmaf

The VMAF computation library. If it is built into ffmpeg, you use it as the libvmaf filter.

pooled_metricsPooled metrics

The part of the libvmaf JSON log that summarizes the per-frame scores (the average, and so on).

n_threadsNumber of computation threads

A filter option that sets how many threads libvmaf uses for the computation.

Check yourself

Which is the correct input order for the libvmaf filter?