How to Stream Desktop Live Over RTSP Using FFmpeg

This guide explains how to stream your live desktop activity over the Real-Time Streaming Protocol (RTSP) using the powerful, open-source command-line tool FFmpeg. You will learn how to capture your screen on Windows, macOS, and Linux, configure the FFmpeg encoding parameters for low latency, and broadcast the feed to an RTSP server so it can be viewed on other devices.

Prerequisite: Setting Up an RTSP Server

FFmpeg does not act as an RTSP server by default; instead, it captures your screen and pushes (publishes) the stream to an existing RTSP server.

Before running the FFmpeg command, you need an active RTSP server. A popular, lightweight, and free option is MediaMTX (formerly rtsp-simple-server). 1. Download and run MediaMTX on your local machine or a remote server. 2. It will start listening for RTSP publishers on port 8554 by default. 3. Your publishing URL will look like: rtsp://localhost:8554/mystream.


FFmpeg Commands by Operating System

Once your RTSP server is running, use the appropriate command for your operating system to capture your screen and start streaming.

1. Windows (using gdigrab)

Windows uses the gdigrab device to capture desktop input. Open Command Prompt or PowerShell and run:

ffmpeg -f gdigrab -framerate 30 -i desktop -c:v libx264 -pix_fmt yuv420p -preset ultrafast -tune zerolatency -f rtsp rtsp://localhost:8554/mystream

2. Linux (using x11grab)

Linux systems running an X11 display server use x11grab. Open your terminal and run:

ffmpeg -f x11grab -framerate 30 -video_size 1920x1080 -i :0.0 -c:v libx264 -pix_fmt yuv420p -preset ultrafast -tune zerolatency -f rtsp rtsp://localhost:8554/mystream

(Note: Replace 1920x1080 with your actual monitor resolution, and :0.0 with your display identifier if different).

3. macOS (using avfoundation)

macOS utilizes the avfoundation framework. First, list your available input devices to find your screen index:

ffmpeg -f avfoundation -list_devices true -i ""

Identify the index number for your screen (usually 1 or 2), then run:

ffmpeg -f avfoundation -framerate 30 -i "1" -c:v libx264 -pix_fmt yuv420p -preset ultrafast -tune zerolatency -f rtsp rtsp://localhost:8554/mystream

(Note: Replace "1" with your designated screen index).


Command Breakdown

Understanding the FFmpeg flags allows you to customize the stream to suit your network bandwidth and hardware:


How to View the Live Stream

To verify that your live desktop stream is working:

  1. Open a media player that supports RTSP streaming, such as VLC Media Player.
  2. Go to Media > Open Network Stream (or press Ctrl+N).
  3. Enter your RTSP stream URL: rtsp://localhost:8554/mystream (replace localhost with the server’s IP address if viewing from another computer on the same network).
  4. Click Play to view your live desktop.