Stream Video and Audio to RTSP Server Using FFmpeg

Streaming live audio and video to an RTSP (Real-Time Streaming Protocol) server is a highly efficient way to distribute media for IP cameras, live broadcasts, and security monitoring. This article provides a practical, step-by-step guide on how to use FFmpeg to encode and transmit synchronized media streams to an RTSP target, covering essential command structures, hardware input capture, and latency optimization techniques.

Prerequisites

Before streaming, you need: 1. FFmpeg installed on your system. 2. An active RTSP server (such as MediaMTX, Nimble Streamer, or Live555) ready to accept publishing connections.

Basic Command to Stream a Media File

To stream a pre-recorded video file with audio to an RTSP server, use the following basic command structure:

ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -c:a aac -f rtsp rtsp://localhost:8554/live/stream

Streaming Live Hardware Inputs (Camera and Microphone)

If you want to stream a live webcam and microphone feed instead of a file, you must target your system’s hardware capture devices.

On Windows (using DirectShow):

ffmpeg -f dshow -i video="Integrated Camera":audio="Microphone" -c:v libx264 -preset ultrafast -c:a aac -f rtsp rtsp://localhost:8554/live/stream

On Linux (using Video4Linux2 and ALSA):

ffmpeg -f v4l2 -i /dev/video0 -f alsa -i hw:0 -c:v libx264 -preset ultrafast -c:a aac -f rtsp rtsp://localhost:8554/live/stream

On macOS (using AVFoundation):

ffmpeg -f avfoundation -framerate 30 -i "0:0" -c:v libx264 -preset ultrafast -c:a aac -f rtsp rtsp://localhost:8554/live/stream

Optimizing for Low Latency

RTSP streams can suffer from lag if not configured correctly. To minimize latency, add the following parameters to your FFmpeg command:

Optimized Low-Latency Command:

ffmpeg -re -i input.mp4 -c:v libx264 -preset ultrafast -tune zerolatency -g 60 -c:a aac -rtsp_transport tcp -f rtsp rtsp://localhost:8554/live/stream