How to Use FFmpeg -re Option with Live Camera Input

This article explains how the -re (real-time) option in FFmpeg interacts with live camera inputs. You will learn what the -re flag does, why it is generally unnecessary and counterproductive for live hardware devices like webcams or capture cards, and the correct way to configure your FFmpeg commands for real-time streaming.

What is the -re Option?

By default, FFmpeg processes input files as fast as your CPU and disk speed allow. The -re option tells FFmpeg to read the input at its native frame rate.

This flag is crucial when you are streaming a pre-recorded file (like an MP4) to a live destination (like YouTube or Twitch via RTMP). Without -re, FFmpeg would read and upload the file too fast, resulting in a broken stream or a rejected connection.

Why You Should Not Use -re with Live Cameras

When your input source is a live camera (such as a USB webcam, an RTSP IP camera, or a PCIe capture card), the input is already generated in real-time. The hardware clock of the camera dictates the frame rate.

Using the -re option on a live camera input is redundant and can cause performance issues: 1. Double Throttling: FFmpeg will attempt to apply an artificial software delay on top of an input that is already arriving in real-time. 2. Buffer Overflows: If the camera’s hardware clock and FFmpeg’s software clock drift slightly out of sync, FFmpeg may fall behind, leading to buffer overflow errors and dropped frames. 3. Increased Latency: It introduces unnecessary processing delay to the live feed.

Correct Implementation Examples

1. The Incorrect Way (Using -re with Live Camera)

Avoid using -re when capturing from a hardware device:

# AVOID THIS:
ffmpeg -re -f v4l2 -i /dev/video0 -c:v libx264 -f flv rtmp://live.twitch.tv/app/stream_key

2. The Correct Way (Omitting -re for Live Camera)

Simply omit the -re flag. Let the camera hardware drive the input timing naturally:

# Linux (V4L2 Webcam):
ffmpeg -f v4l2 -framerate 30 -video_size 1280x720 -i /dev/video0 -c:v libx264 -f flv rtmp://live.twitch.tv/app/stream_key

# Windows (dshow Webcam):
ffmpeg -f dshow -framerate 30 -video_size 1280x720 -i video="Integrated Camera" -c:v libx264 -f flv rtmp://live.twitch.tv/app/stream_key

# macOS (AVFoundation):
ffmpeg -f avfoundation -framerate 30 -video_size 1280x720 -i "0" -c:v libx264 -f flv rtmp://live.twitch.tv/app/stream_key

Best Practices for Live Camera Inputs

Instead of using -re, use the following flags to optimize live camera performance and prevent frame drops:

# Recommended command structure for Windows:
ffmpeg -f dshow -rtbufsize 100M -thread_queue_size 1024 -framerate 30 -video_size 1280x720 -i video="Integrated Camera" -c:v libx264 -preset veryfast -f flv rtmp://live.twitch.tv/app/stream_key