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-keymacOS
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-keyLinux
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-keyUnderstanding the FFmpeg Parameters
-f gdigrab / avfoundation / x11grab: Specifies the input format/device driver for capturing the screen.-framerate 30: Captures the desktop at 30 frames per second (adjust to 60 for smoother motion if hardware allows).-i: Specifies the input source (e.g.,desktopor display coordinate:0.0).-c:v libx264: Encodes the video stream into H.264 format, which is the standard for RTMP streaming.-pix_fmt yuv420p: Sets the pixel format to YUV 4:2:0, ensuring compatibility across web browsers and media players.-preset veryfast: Balances CPU usage and encoding quality. Useultrafastif your CPU struggles.-g 60: Sets the Group of Pictures (GOP) size. For a 30fps stream, a GOP of 60 forces a keyframe every 2 seconds, which is a requirement for most streaming platforms.-f flv: Forces the output format to FLV, the container required for RTMP delivery.