How to Set lag-in-frames in FFmpeg VP9

This article explains how to configure the lag-in-frames parameter when encoding video with the libvpx-vp9 encoder in FFmpeg. You will learn what this parameter does, how it impacts video quality and encoding latency, and how to apply it using practical FFmpeg command-line examples.

What is lag-in-frames?

The lag-in-frames parameter determines the number of frames the VP9 encoder looks ahead before encoding a given frame. By analyzing future frames (a process known as “lookahead”), the encoder can make more intelligent decisions regarding rate control, frame-type allocation (such as keyframes and golden frames), and overall compression efficiency.

By default, the libvpx-vp9 library sets this value to 25 for two-pass encoding, which is also the maximum supported value.

Technical Trade-offs

When adjusting this parameter, you must balance video quality against latency:

How to Configure lag-in-frames in FFmpeg

To set this parameter in FFmpeg, use the -lag-in-frames private option flag followed by an integer value between 0 and 25.

Example 1: High-Quality VOD Encoding (Maximum Lookahead)

For standard files where quality is the priority, set the parameter to its maximum value of 25.

ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -lag-in-frames 25 output.webm

Example 2: Low-Latency / Real-Time Streaming

For live streaming or interactive video applications where delay must be minimized, set the parameter to 0.

ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -lag-in-frames 0 -deadline realtime output.webm

(Note: When setting lag-in-frames to 0 for live streaming, it is recommended to also use the -deadline realtime speed setting to ensure the encoder runs fast enough for live playback).

Example 3: Balanced Mid-Range Lookahead

If you want to reduce the memory footprint of the encoder while retaining some compression benefits, you can choose a middle ground like 16.

ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 2M -lag-in-frames 16 output.webm