How to Stream at Native Speed with FFmpeg -re

This article explains how to use the -re (real-time) option in FFmpeg to stream media files at their native playback speed. You will learn what the -re option does, why it is crucial for live streaming workflows, and how to correctly position it in your command-line interface to prevent server buffering and playback synchronization issues.

What is the -re Option?

By default, FFmpeg processes input files as fast as your computer’s CPU and disk speed allow. While this is ideal for transcoding a file to save to your hard drive, it is problematic for live streaming. If you feed video data to a live streaming server (like YouTube, Twitch, or an RTSP server) faster than real-time, the server will become overwhelmed, leading to dropped packets, buffering, or connection termination.

The -re option tells FFmpeg to read the input file at its native frame rate. For example, if your video is 30 frames per second, FFmpeg will read exactly 30 frames per second, mimicking a live camera feed.

How to Use -re in an FFmpeg Command

To use the -re option correctly, you must place it before the input file flag (-i). If you place it after the input, FFmpeg may ignore it or apply it incorrectly.

Here is the basic syntax for streaming a local file to an RTMP destination at native speed:

ffmpeg -re -i input.mp4 -c:v libx264 -c:a aac -f flv rtmp://your-streaming-url/live/stream_key

Breakdown of the Command:

Important Considerations

Loop Streaming

If you want to stream a file in an infinite loop at native speed, combine the -re option with the -stream_loop -1 option. The loop option must also be placed before the input:

ffmpeg -stream_loop -1 -re -i input.mp4 -c:v libx264 -c:a aac -f flv rtmp://your-streaming-url/live/stream_key

When NOT to Use -re

Do not use the -re option in the following scenarios: * Live Hardware Inputs: If your input is a physical device like a webcam, capture card, or microphone, the input is already real-time. Adding -re will cause latency and lag. * File-to-File Transcoding: If you are simply converting a file from one format to another (e.g., MP4 to MKV) and saving it to your hard drive, using -re will unnecessarily slow down the conversion process to real-time speed.