How to Use the FFmpeg Realtime Filter

This article provides a quick overview and practical guide on how to use the realtime filter in FFmpeg. You will learn what the filter does, its basic syntax, how it compares to the traditional -re input option, and how to apply it to your video and audio processing workflows to emulate real-time streaming.

What is the Realtime Filter?

By default, FFmpeg processes media files as fast as your CPU and GPU hardware allow. While this is ideal for file transcoding, it causes issues when broadcasting a live stream from a pre-recorded file, as the data is sent too quickly for the ingestion server.

The realtime filter solves this by pausing the processing pipeline matching the actual playback speed of the media.

Basic Syntax

The realtime filter can be used for both video (-vf) and audio (-af) streams, or within a complex filtergraph (-filter_complex).

To apply the filter to a video stream, use the following basic command:

ffmpeg -i input.mp4 -vf realtime -f mpegts udp://127.0.0.1:1234

To apply it to both audio and video streams using a complex filtergraph:

ffmpeg -i input.mp4 -filter_complex "[0:v]realtime[v];[0:a]realtime[a]" -map "[v]" -map "[a]" -f flv rtmp://live.server.com/app/stream_key

Filter Options

The realtime filter accepts a limit parameter, which defines the maximum pause time in seconds.

Example using the limit parameter:

ffmpeg -i input.mp4 -vf "realtime=limit=1.5" -f mpegts udp://127.0.0.1:1234

Realtime Filter vs. the -re Option

Historically, the -re flag (inserted before the input file) was the standard way to read inputs at native frame rates. However, the realtime filter offers distinct advantages:

  1. Precision: The realtime filter is applied at the filtering stage of the pipeline rather than the demuxing stage. This results in more precise pacing, especially when dealing with complex filter chains.
  2. Selectivity: While -re applies globally to the entire input file, the realtime filter can be placed at specific points in your filtergraph, allowing you to speed up or slow down certain segments of your processing before throttling the output rate.