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:1234How it works:
-re: This must be placed before the-i(input) option. It acts as an input rate limiter, slowing down the reading of the file to match the real-world duration of the media.- Result: CPU usage drops significantly because the encoder only processes frames as they are read in real time.
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/streamHow it works:
-vf "realtime": Applies the video realtime filter, stalling the video stream processing to real-world time.-af "arealtime": Applies the audio realtime filter, keeping the audio stream synced in real time.
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:1234When to Use Which?
- Use
-reif you are streaming standard pre-recorded files (like MP4 or MKV). It is the most efficient method because it throttles the pipeline at the very beginning (the demuxer stage). - Use the
realtimefilter if you are generating live feeds programmatically within FFmpeg (e.g.,-f lavfi -i testsrc), or if you are applying heavy filtering where the processing speed needs to be throttled mid-pipeline.