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.m3u8

macOS (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


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.