Prevent FFmpeg CPU Overload with Realtime Filter

When streaming local media files, FFmpeg defaults to processing the data as fast as your CPU allows. This behavior causes massive CPU usage spikes and can crash local streaming destinations that cannot handle the rapid influx of data. This article explains how to use FFmpeg’s -re option and the realtime filter to throttle processing to the media’s native frame rate, ensuring low CPU usage and stable local streams.

The Problem: Fast-As-Possible Processing

By default, FFmpeg treats input files as static data to be transcoded as quickly as possible. If you attempt to stream a 1080p video file to a local loopback address or RTMP server without throttling, FFmpeg will consume 100% of your CPU to encode the video at 300+ frames per second (fps) instead of the actual 24 or 30 fps playback speed.

To prevent this CPU overload, you must force FFmpeg to read or process the input in real time.

Method 1: Using the -re Input Option

The most common and efficient way to stream local files in real time is using the -re input option. This option tells FFmpeg to read the input file at its native frame rate.

Command Example:

ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -c:a aac -f mpegts udp://127.0.0.1:1234

How it works:

Method 2: Using the realtime Filter

In some scenarios—such as when generating synthetic video sources (like testsrc), using complex filtergraphs, or when -re does not work correctly with certain input formats—you should use the explicit realtime video and audio filters instead.

The realtime filter pauses the filtering pipeline to match the actual elapsed time, preventing downstream encoders from being overloaded.

Command Example:

ffmpeg -i input.mp4 -vf "realtime" -af "arealtime" -c:v libx264 -c:a aac -f flv rtmp://localhost/live/stream

How it works:

You can also configure the filter to speed up or slow down the playback by using the speed parameter. For example, to stream at 1.5x speed:

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

When to Use Which?