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:

  • -f gdigrab / x11grab / avfoundation: Defines the input format/device driver used to capture the desktop.
  • -framerate 30: Sets the capture frame rate to 30 frames per second. You can increase this to 60 for smoother motion if your hardware allows.
  • -i: Specifies the input source. For Windows, it is desktop. For Linux, it is the display port (usually :0.0). For macOS, it is the screen index number.
  • -c:v libx264: Encodes the video using the H.264 video codec, which is highly compatible with almost all RTMP ingestion servers (like YouTube, Twitch, or custom Nginx-RTMP servers).
  • -preset ultrafast: Instructs the H.264 encoder to prioritize encoding speed over compression efficiency. This minimizes CPU usage and prevents dropped frames during real-time encoding.
  • -tune zerolatency: Minimizes encoding delay, which is critical for real-time live streaming interactions.
  • -pix_fmt yuv420p: Sets the pixel format to YUV 420p. This is required because most RTMP players and streaming platforms do not support the default desktop RGB pixel format.
  • -f flv: Forces the output container format to Flash Video (FLV), which is the standard format used to transport video over RTMP.

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.