Use FFmpeg arealtime Filter for Audio Streaming
When streaming audio locally, FFmpeg processes files as fast as your
hardware allows, which can cause network congestion and playback
buffering. This guide demonstrates how to use the arealtime
filter to throttle audio processing to actual real-time speed, ensuring
a smooth and continuous local audio stream.
Understanding the arealtime Filter
The arealtime audio filter in FFmpeg slows down the
filtergraph processing to match the computer’s system clock (wallclock
time). While the global -re input flag serves a similar
purpose at the demuxer level, arealtime is applied directly
as an audio filter. This makes it ideal when you are generating audio
dynamically (such as with virtual audio sources) or when you need
precise pacing after other filters have been applied.
Basic Usage and Command Example
To apply the filter, use the -af (audio filter) flag
followed by arealtime.
Here is a basic command to stream a local audio file over UDP to a local destination at real-time speed:
ffmpeg -i input.mp3 -af arealtime -f mpegts udp://127.0.0.1:12345In this command: * -i input.mp3 specifies your source
audio file. * -af arealtime slows the output processing
down to 1x playback speed. * -f mpegts formats the output
stream as an MPEG-TS container. * udp://127.0.0.1:12345
sends the stream to your local port.
Streaming Generated Audio in Real Time
If you are generating audio on the fly, using -re might
not work correctly. The arealtime filter is the correct
tool for this scenario. For example, to stream a continuous 440Hz sine
wave:
ffmpeg -f lavfi -i "sine=frequency=440:sample_rate=48000" -af arealtime -f adts udp://127.0.0.1:12345Adjusting Playback Speed
The arealtime filter accepts a speed
parameter if you need to pace the stream at a rate other than 1x. For
example, to stream at 1.5x real-time speed, use:
ffmpeg -i input.mp3 -af "arealtime=speed=1.5" -f mpegts udp://127.0.0.1:12345Using arealtime prevents the recipient media player from
receiving too much data at once, eliminating buffering delays and
ensuring a stable local stream.