Stream Live Desktop over RTMP with FFmpeg

Streaming your desktop activity using the Real-Time Messaging Protocol (RTMP) is an efficient way to broadcast presentations, tutorials, or gameplay to platforms like YouTube, Twitch, or a private media server. This guide provides a straightforward, step-by-step approach to capturing your desktop video and audio using FFmpeg and streaming it live via RTMP across Windows, macOS, and Linux.


Prerequisites

Before you begin, ensure you have: 1. FFmpeg installed on your system. 2. Your RTMP URL and Stream Key from your streaming provider (e.g., rtmp://a.rtmp.youtube.com/live2/your-stream-key).


Step 1: Identify Your Capture Device

FFmpeg captures desktop activity using different input devices depending on your operating system.

Windows (gdigrab)

Windows uses the built-in gdigrab device to capture the display. To capture the entire desktop, use desktop as the input.

macOS (avfoundation)

macOS uses the avfoundation framework. To list your available screen and audio capture devices, run:

ffmpeg -f avfoundation -list_devices true -i ""

Note the index numbers for your screen (usually 1 or 2) and your audio device.

Linux (x11grab)

Linux uses the X11 display server to capture screens. Your default display is typically :0.0.


Step 2: Choose Your Command

Select the command corresponding to your operating system. Replace rtmp://your-server/live/stream-key with your actual RTMP destination.

Windows

To stream your desktop with high compatibility:

ffmpeg -f gdigrab -framerate 30 -i desktop -c:v libx264 -pix_fmt yuv420p -preset veryfast -maxrate 3000k -bufsize 6000k -g 60 -f flv rtmp://your-server/live/stream-key

macOS

Replace "1:0" with the correct [video_index]:[audio_index] found in Step 1:

ffmpeg -f avfoundation -framerate 30 -i "1:0" -c:v libx264 -pix_fmt yuv420p -preset veryfast -maxrate 3000k -bufsize 6000k -g 60 -c:a aac -b:a 128k -f flv rtmp://your-server/live/stream-key

Linux

To stream the desktop along with PulseAudio system sound:

ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i :0.0 -f pulse -i default -c:v libx264 -pix_fmt yuv420p -preset veryfast -maxrate 3000k -bufsize 6000k -g 60 -c:a aac -b:a 128k -f flv rtmp://your-server/live/stream-key

Understanding the FFmpeg Parameters