Stream Live Desktop Activity as HLS Using FFmpeg
This guide provides a straightforward, step-by-step tutorial on how to capture your live desktop activity and stream it as an HTTP Live Streaming (HLS) feed using the powerful, open-source multimedia framework FFmpeg. You will learn the exact FFmpeg commands required for different operating systems, how to configure the HLS segmenter, and how to optimize the settings for low-latency playback.
To stream your desktop as an HLS feed, FFmpeg must capture your
display as an input, encode the video using a compatible codec (usually
H.264), and segment the output into a playlist (.m3u8) and
media segment files (.ts).
Step 1: Run the FFmpeg Command for Your OS
Select the command below that matches your operating system. These commands capture the screen at 30 frames per second, encode it to H.264, and output HLS segments to a directory.
Windows (using gdigrab)
ffmpeg -f gdigrab -framerate 30 -i desktop -c:v libx264 -pix_fmt yuv420p -preset ultrafast -g 60 -b:v 2000k -f hls -hls_time 4 -hls_list_size 5 -hls_flags delete_segments stream.m3u8macOS (using avfoundation)
ffmpeg -f avfoundation -framerate 30 -i "1" -c:v libx264 -pix_fmt yuv420p -preset ultrafast -g 60 -b:v 2000k -f hls -hls_time 4 -hls_list_size 5 -hls_flags delete_segments stream.m3u8(Note: You may need to replace "1" with your
specific screen device index, which you can find by running
ffmpeg -f avfoundation -list_devices true -i "")
Linux (using x11grab)
ffmpeg -f x11grab -video_size 1920x1080 -framerate 30 -i :0.0 -c:v libx264 -pix_fmt yuv420p -preset ultrafast -g 60 -b:v 2000k -f hls -hls_time 4 -hls_list_size 5 -hls_flags delete_segments stream.m3u8(Note: Adjust -video_size 1920x1080 to match your
actual screen resolution.)
Step 2: Understanding the Command Parameters
-f gdigrab / avfoundation / x11grab: Specifies the OS-specific device driver used to capture the desktop screen.-framerate 30: Captures the desktop at 30 frames per second.-c:v libx264: Encodes the video using the H.264 codec, which is universally compatible with HLS.-pix_fmt yuv420p: Ensures the pixel format is compatible with standard web players.-preset ultrafast: Instructs the encoder to use the fastest settings to minimize CPU usage and encoding latency.-g 60: Sets the Group of Pictures (GOP) size to 60. At 30 FPS, this forces a keyframe every 2 seconds. Aligning keyframes is crucial for HLS segment cutting.-b:v 2000k: Sets the video bitrate to 2000 kbps (2 Mbps). Adjust this based on your network upload speed.-f hls: Sets the output muxer to HLS.-hls_time 4: Sets the target length of each video segment to 4 seconds.-hls_list_size 5: Keeps only the 5 most recent segments in the.m3u8playlist file to prevent it from growing indefinitely.-hls_flags delete_segments: Automatically deletes old.tsfiles as they fall out of the playlist buffer, saving disk space.stream.m3u8: The name of the output index playlist file.
Step 3: Serve and Play the Stream
Once you run the command, FFmpeg will continuously generate a
stream.m3u8 file and several numbered .ts
segment files (e.g., stream0.ts, stream1.ts)
in your current working directory.
To make the feed viewable by others, you must host these generated
files on a web server (such as Nginx, Apache, or a simple Node.js
server) and point an HLS-compatible media player to the URL of the
stream.m3u8 file.
You can test the playback locally using VLC Media Player by opening
the Network Stream option and entering the local path to your
.m3u8 file.