Stream Linux Desktop to Twitch or YouTube with FFmpeg

This article provides a comprehensive, step-by-step guide on how to stream your Linux desktop screen directly to popular streaming platforms like Twitch or YouTube using the powerful command-line tool FFmpeg. You will learn how to capture your display, configure audio inputs via PulseAudio or PipeWire, optimize video settings for hardware acceleration, and assemble the final command to go live.

Prerequisites and Tools

Before building your streaming command, you need to ensure you have the necessary tools installed and identify your system’s current display server.

Step 1: Capture Your Screen and Audio

FFmpeg uses specific input devices to capture your desktop and system audio depending on your environment.

For X11 Users (x11grab)

If you are on X11, you will use the x11grab device. To find your screen resolution and display name, use the xdpyinfo command. Usually, your primary display is :0.0.

For Wayland Users (PipeWire)

Modern Linux distributions using Wayland rely on PipeWire for screen sharing. FFmpeg can capture this using the libpipewire or kmsgrab inputs, though many users prefer launching a virtual X11 display via Xwayland or using tools like wf-recorder for complex Wayland setups.

Capturing Audio

To stream your desktop audio alongside your microphone, you will capture from PulseAudio or PipeWire’s pulse emulation layer using -f pulse. You can find your specific audio input names by running:

pactl list sources short

Step 2: Configure Video and Audio Encoding

Streaming platforms require specific codecs and configurations to process your live feed smoothly without buffering.

Step 3: Assemble the FFmpeg Streaming Command

Combine your inputs, encoding parameters, and the streaming platform’s RTMP ingest URL into a single command.

Replace YOUR_STREAM_KEY with the private key provided by YouTube Studio or your Twitch Creator Dashboard.

Example Command for Twitch (1080p at 60fps)

ffmpeg -f x11grab -video_size 1920x1080 -framerate 60 -i :0.0 \
-f pulse -i default \
-c:v libx264 -preset veryfast -b:v 6000k -maxrate 6000k -bufsize 12000k \
-pix_fmt yuv420p -g 120 \
-c:a aac -b:a 128k -ar 44100 \
-f flv rtmp://live.twitch.tv/app/YOUR_STREAM_KEY

Example Command for YouTube (1080p at 60fps)

ffmpeg -f x11grab -video_size 1920x1080 -framerate 60 -i :0.0 \
-f pulse -i default \
-c:v libx264 -preset veryfast -b:v 9000k -maxrate 9000k -bufsize 18000k \
-pix_fmt yuv420p -g 120 \
-c:a aac -b:a 128k -ar 44100 \
-f flv rtmp://a.rtmp.youtube.com/live2/YOUR_STREAM_KEY

Key Parameter Breakdown