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/mystream2. 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:
-f gdigrab / x11grab / avfoundation: Specifies the format/driver used to capture the desktop.-framerate 30: Captures the desktop at 30 frames per second. You can increase this to60for smoother motion if your hardware permits.-i: Specifies the input source (the desktop).-c:v libx264: Encodes the video using the H.264 codec, which is highly compatible with almost all RTSP media players.-pix_fmt yuv420p: Sets the pixel format to YUV 4:2:0, ensuring compatibility with standard video players like VLC.-preset ultrafast: Instructs the encoder to prioritize speed over compression, minimizing CPU usage and latency.-tune zerolatency: Eliminates frame buffering inside the encoder to achieve the lowest possible delay during live streaming.-f rtsp: Forces the output format to RTSP.
How to View the Live Stream
To verify that your live desktop stream is working:
- Open a media player that supports RTSP streaming, such as VLC Media Player.
- Go to Media > Open Network
Stream (or press
Ctrl+N). - Enter your RTSP stream URL:
rtsp://localhost:8554/mystream(replacelocalhostwith the server’s IP address if viewing from another computer on the same network). - Click Play to view your live desktop.