Stream Desktop Recording to stdout with FFmpeg
Streaming your desktop recording directly to standard output
(stdout) using FFmpeg is a powerful technique that allows
you to pipe real-time video data into other applications, network
streams, or media players without saving a temporary file to your disk.
This article provides a clear, step-by-step guide on how to capture your
screen on Linux, macOS, and Windows, configure the correct output format
for streaming, and direct the media pipeline to stdout.
Understanding the stdout Syntax in FFmpeg
To instruct FFmpeg to output to stdout instead of a
physical file, you use a hyphen (-) or pipe:1
as the output destination at the very end of your command.
Because stdout is a non-seekable protocol, you cannot use formats
that require seeking back to write header information (such as standard
MP4). Instead, you must use streamable container formats like
MPEG-TS (-f mpegts),
Matroska (-f matroska), or
NUT (-f nut).
Commands by Operating System
Select the command below that corresponds to your operating system.
1. Linux (using x11grab)
On Linux systems running X11, use the x11grab input
device to capture your desktop:
ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i :0.0 -c:v libx264 -preset ultrafast -f mpegts --f x11grab: Specifies the X11 screen grabber.-video_size 1920x1080: Sets the resolution of your capture area.-i :0.0: Selects the default X11 display.-f mpegts -: Forces the output format to MPEG-TS and pipes it tostdout.
2. Windows (using gdigrab)
On Windows, use the built-in gdigrab device to capture
your desktop display:
ffmpeg -f gdigrab -framerate 30 -i desktop -c:v libx264 -preset ultrafast -pix_fmt yuv420p -f mpegts --f gdigrab: Specifies the Graphics Device Interface (GDI) grabber.-i desktop: Tells FFmpeg to record the entire desktop.-pix_fmt yuv420p: Ensures compatibility with most players consuming the stream.
3. macOS (using avfoundation)
On macOS, use the native avfoundation framework to
capture your screen:
ffmpeg -f avfoundation -framerate 30 -i "1" -c:v libx264 -preset ultrafast -f mpegts --f avfoundation: Invokes the macOS recording framework.-i "1": Specifies the screen index (usually"1"for the primary desktop).
How to Test and Consume the Output
You can verify that your stdout stream is functioning correctly by
piping the output of your FFmpeg command directly into a media player
like ffplay.
To test this on Linux, run the following pipe command:
ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i :0.0 -c:v libx264 -preset ultrafast -f mpegts - | ffplay -In this command, ffplay reads the video data directly
from the piped stdout of the ffmpeg process, displaying
your desktop recording in real-time with minimal latency.