Stream Desktop Capture to RTMP Using FFmpeg

This article provides a straightforward guide on how to capture your computer screen and stream it directly to an RTMP server in real-time using FFmpeg. You will find the exact command-line configurations required for Windows, macOS, and Linux, along with a breakdown of the key FFmpeg parameters needed to ensure a low-latency, high-quality live stream.

Step 1: Choose Your OS-Specific Command

FFmpeg uses different input devices depending on your operating system to capture the desktop. Replace rtmp://your-rtmp-server-url/live/stream_key in the examples below with your actual RTMP ingest URL and stream key.

Windows (using gdigrab)

On Windows, gdigrab is the built-in device for grabbing the desktop screen.

ffmpeg -f gdigrab -framerate 30 -i desktop -c:v libx264 -preset ultrafast -tune zerolatency -pix_fmt yuv420p -f flv rtmp://your-rtmp-server-url/live/stream_key

Linux (using x11grab)

On Linux systems running X11, use x11grab to capture the display.

ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i :0.0 -c:v libx264 -preset ultrafast -tune zerolatency -pix_fmt yuv420p -f flv rtmp://your-rtmp-server-url/live/stream_key

Note: Adjust -video_size 1920x1080 to match your screen resolution.

macOS (using avfoundation)

On macOS, use the avfoundation input device.

ffmpeg -f avfoundation -framerate 30 -i "1" -c:v libx264 -preset ultrafast -tune zerolatency -pix_fmt yuv420p -f flv rtmp://your-rtmp-server-url/live/stream_key

Note: -i "1" specifies the screen index. You can list available capture devices on macOS by running ffmpeg -f avfoundation -list_devices true -i "".

Step 2: Understanding the Parameters

To customize your stream, it is important to understand what each parameter in the command does:

Step 3: Adding Audio to Your Stream (Optional)

If you need to stream desktop audio alongside the video capture, you must add an audio input source to the command.

Windows (using dshow to capture stereo mix or microphone)

First, identify your audio devices by running:

ffmpeg -list_devices true -f dshow -i dummy

Then, add the audio device to your desktop capture command:

ffmpeg -f gdigrab -framerate 30 -i desktop -f dshow -i audio="Your_Audio_Device_Name" -c:v libx264 -preset ultrafast -tune zerolatency -pix_fmt yuv420p -c:a aac -b:a 128k -f flv rtmp://your-rtmp-server-url/live/stream_key

Linux (using ALSA or PulseAudio)

ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i :0.0 -f pulse -i default -c:v libx264 -preset ultrafast -tune zerolatency -pix_fmt yuv420p -c:a aac -b:a 128k -f flv rtmp://your-rtmp-server-url/live/stream_key

macOS (using avfoundation)

ffmpeg -f avfoundation -framerate 30 -i "1:0" -c:v libx264 -preset ultrafast -tune zerolatency -pix_fmt yuv420p -c:a aac -b:a 128k -f flv rtmp://your-rtmp-server-url/live/stream_key

Note: "1:0" maps screen index 1 to video, and audio input index 0 to audio.