Serve Live Stream with FFmpeg Built-in HTTP Server

This article provides a step-by-step guide on how to utilize the built-in HTTP server functionality in FFmpeg to host and serve a live video stream directly from your machine. You will learn the specific FFmpeg command-line syntax required to listen for incoming HTTP connections and stream video content to clients without needing to set up an external web server.

To serve a live stream directly from FFmpeg, you can use the -listen flag combined with the HTTP protocol. This tells FFmpeg to act as an HTTP server and wait for a client to connect before it begins transmitting the media data. The most compatible format for direct HTTP streaming is MPEG-TS.

Here is the standard command to start an HTTP live streaming server using FFmpeg:

ffmpeg -re -i input.mp4 -c:v libx264 -c:a aac -f mpegts -listen 1 http://0.0.0.0:8080

Command Breakdown

  • -re: This option forces FFmpeg to read the input file at its native frame rate. This is crucial for simulating a real-time live stream from a pre-recorded file.
  • -i input.mp4: Specifies the source video. You can replace this with a physical camera input or desktop capture device.
  • -c:v libx264 and -c:a aac: Transcodes the video to H.264 and the audio to AAC, which ensures high compatibility with modern media players.
  • -f mpegts: Sets the output format to MPEG-TS, a robust container designed for live streaming that can handle transmission errors.
  • -listen 1: Instructs FFmpeg to act as a server and listen for an incoming connection.
  • http://0.0.0.0:8080: The address and port where the server will listen. Using 0.0.0.0 allows connections from both the local machine and other devices on your local network.

How to Play the Stream

Once you run the command, FFmpeg will pause and wait for a client to request the stream. To play the live stream:

  1. Open a media player that supports network streams, such as VLC Media Player.
  2. Select Open Network Stream (or press Ctrl+N).
  3. Enter the URL: http://localhost:8080 (if playing on the same machine) or http://<your-ip-address>:8080 (if playing from another device on the same network).
  4. Click Play. FFmpeg will immediately begin encoding and streaming the video.

Limitations of the Built-in Server

While highly convenient for testing and local network streaming, the FFmpeg built-in HTTP server is designed to handle only one client connection at a time. If the client disconnects, the FFmpeg process will terminate. For multi-user distribution, you should stream from FFmpeg to a dedicated media server (like Nginx with the RTMP module, SRS, or Nimble Streamer) using RTMP, SRT, or HLS.