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.
- FFmpeg: Ensure you have FFmpeg installed with
support for
libx264(for software encoding) or hardware encoders likeh264_vaapiornvenc. - Display Server: Identify whether you are running a
X11 or Wayland session, as this
dictates which video grabber FFmpeg will use. You can check this by
running
echo $XDG_SESSION_TYPEin your terminal.
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 shortStep 2: Configure Video and Audio Encoding
Streaming platforms require specific codecs and configurations to process your live feed smoothly without buffering.
- Video Codec:
libx264is the most compatible H.264 software encoder. Use thecbr(Constant Bitrate) rate control method, as streaming platforms reject variable bitrates. - Audio Codec: AAC (
-c:a aac) is the standard audio format for RTMP streaming, usually set at a bitrate of 128k or 192k. - Pixel Format: Most streaming services require the
yuv420ppixel format to decode the video properly on player devices.
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_KEYExample 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_KEYKey Parameter Breakdown
-g 120: Sets the group of pictures (GOP) size. Streaming platforms strictly require a 2-second keyframe interval. At 60fps, 60 x 2 = 120.-preset veryfast: Balances CPU usage and video quality. If your CPU struggles and drops frames, change this tosuperfastorultrafast.-bufsize: The rate control buffer. Setting this to twice your video bitrate (-b:v) helps maintain a stable stream output over shifting network conditions.