Ch. 19 · Diagnosing Video Quality Problems

한국어日本語
10 min

Pulling Out Suspect Segments: ffmpeg and Per-Frame VMAF

Find the timestamp of the lowest point in per-frame VMAF, pull that segment out with -ss, select, and tile, and check it by eye. A low score doesn't guarantee a person will find it distracting, so always finish by looking.

Most of the symptoms in the earlier lessons appear in some segments, not the whole video. So this last lesson is a procedure for deciding 'where to look'. Narrow down suspect segments with numbers, pull those segments out with ffmpeg, and check them by eye.

What this site's measurements tell us

In the vmaf-worst-frames measurement, looking at the 12 points whose mean VMAF is closest to 90, the gap between mean and minimum (mean − min) ranged from 2.275 to 16.717. The result of encoding riverbed with x264 at CRF 26 has a mean of 95.238 and a 5th percentile of 93.478, yet the minimum was 78.521. That means even a video that passes on the mean alone can contain frames where the score drops a lot.

Clip (encoder, CRF)Mean5th percentileMinMean − min
riverbed (x264, 26)95.23893.47878.52116.717
riverbed (SVT-AV1, 42)89.76283.40774.09315.669
rush_hour (x265, 24)91.63589.71989.3602.275

Source: vmaf-worst-frames-v1 (part of the table in the original article). Four 1080p25 10-second clips, 8-bit, software encoders, measured on one PC.

We don't know 'which artifact' that low frame is

The original article stated in its limitations that it did not check which frame the minimum score came from or what shape of distortion it was. This article also doesn't say whether a low-scoring frame is blocking or banding. VMAF only tells you 'where it is bad'; 'what is bad' has to be sorted out by eye with this chapter's methods. A special frame such as the first frame or a scene change could also be the minimum.

Step 1: Find the lowest points with per-frame VMAF

The command that saves VMAF as JSON is the same as in the libvmaf hands-on lesson. When comparing only part of a video, you must align the same segment by putting the same -ss and -t on both inputs (Measurement pitfalls lesson).

Save per-frame VMAF as JSON for only 10 seconds starting at 30 s (the first input is the output file, the second is the source)
ffmpeg -ss 30 -t 10 -i encoded.mkv -ss 30 -t 10 -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 -
Find the timestamps of the 10 lowest frames (standard library only)
import json

fps = 25.0          # video frame rate
start = 30.0        # the -ss value (seconds) from the command above. 0 if you measured the whole thing

d = json.load(open("vmaf.json"))
rows = [(f["frameNum"], f["metrics"]["vmaf"]) for f in d["frames"]]
worst = sorted(rows, key=lambda r: r[1])[:10]
for n, s in sorted(worst):
    print(f"frame {n:5d}  source time {start + n / fps:8.2f}s  VMAF {s:6.2f}")

If the low frames are clustered in one place, that scene is hard; if they are scattered, it may be a particular frame type or a brief event (such as a scene change). The temporal behavior from the earlier lessons (is it periodic, or concentrated in one scene) becomes a clue here. The frame numbers in this script count from 0 within the segment cut by -ss, so you add start to convert them to source time.

Step 2: Pull out the frame at that time and the frames around it

One frame with -ss, a specific frame number with select, and a quick overview with tile
# (a) One frame by timestamp: putting -ss before -i seeks quickly
ffmpeg -ss 34.2 -i encoded.mkv -frames:v 1 enc_34.2.png

# (b) Exactly one frame by frame number (n counts from 0; escape the comma as \,)
ffmpeg -i encoded.mkv -vf "select=eq(n\,310)" -frames:v 1 -fps_mode passthrough enc_n310.png
# On older ffmpeg versions, use -vsync 0 instead of -fps_mode

# (c) Take 3 s of the suspect segment at 5 frames per second, 15 frames, into one 5x3 tile
ffmpeg -ss 34 -t 3 -i encoded.mkv -vf "fps=5,scale=384:-1,tile=5x3" -frames:v 1 contact.png

# (d) Pull out only scene-change timestamps as candidates (the 0.4 threshold is an example; adjust per video)
ffmpeg -i encoded.mkv -vf "select='gt(scene,0.4)',showinfo" -f null - 2>&1 | grep pts_time
Should -ss go before or after -i?

An -ss before the input seeks quickly to near a keyframe and then decodes up to the exact time, while an -ss after the input decodes from the start and works its way there, so it is slower. That is the general understanding, but behavior can differ with the version and option combination, so for precise comparisons, check that the same frame came out.

Step 3: Put it next to the source at the same frame and name it

Use the hstack command from Lesson 1 to view the source and output side by side, and name what you see with the symptom table. Once you have a name, pick the knobs from the matching lesson.

What you sawLesson to read nextFirst knob
Square blocks, high-motion sceneBlockingLower CRF / raise bitrate
Ripples around text and lines / shimmer around outlinesRinging and mosquito noiseCheck the filter stage, weaken sharpening, CRF
Bands in the sky or a dark backgroundBanding10-bit, AQ, light deband
Periodic surgingFlicker and pumpingCheck the source, VBV headroom, keyframe interval
Sharp color edges bleedColor bleeding4:4:4 (check compatibility), chroma quality

Step 4: After fixing it, measure the same segment again

If you changed a knob, re-encode the same segment and compare the score and the picture the same way. Record how much the file size changed too. You can continue with bitrate planning in the video size and bitrate calculator and command assembly in the FFmpeg command builder. To roughly gauge at which CRF you reach a target mean or minimum, use the CRF recommender, but remember that its values come from this site's four measured clips (10 seconds, 8-bit).

One-line summary of this chapter

Name the symptom (still or moving, location, period), narrow down the segment with per-frame scores, confirm by eye, and then turn only the knob that fits that symptom. Raising the bitrate is just one of several knobs.

Per-frame VMAFPer-frame VMAF

The score recorded for each frame in the libvmaf JSON log. Used to compute the mean, 5th percentile, and minimum, or to find the timestamps of low frames.

Contact sheetContact sheet

An image that tiles several frames into one. Made with ffmpeg's fps and tile filters.

Check yourself

In the vmaf-worst-frames measurement, riverbed x264 CRF 26 had a mean of 95.238 but a minimum of 78.521. What can correctly be said from this?

Check yourself

You found the timestamps of low frames in per-frame VMAF. What is the most appropriate next step?

Check yourself

In the vmaf.json for a segment cut with -ss 30 -t 10, frame number 100 (counting from 0, at 25 fps) was the lowest. What is its time in the source?