HTTP Media Streaming via FFmpeg on Linux

FFmpeg includes a built-in, lightweight HTTP server capability that allows you to stream local audio and video files directly over a network without needing heavy external web server software. By utilizing the mp4, mpegts, or webm muxers combined with the HTTP protocol output, you can instantly host a media file from your Linux terminal. This guide covers the exact commands required to set up a local HTTP stream, configure the appropriate formats, and access the stream from any device on your local network.


The Basic HTTP Streaming Command

FFmpeg can listen for incoming HTTP connections by using the http:// protocol prefix in the output argument, combined with the -listen 1 flag. This turns FFmpeg from a standard media converter into a single-use web server.

The foundational syntax for streaming a local file is:

ffmpeg -re -i input.mp4 -c copy -listen 1 -f mp4 http://0.0.0.0:8080

Breaking Down the Flags


Choosing the Right Formats and Codecs

While stream-copying (-c copy) is fast, not all media containers stream smoothly over basic HTTP loops. Depending on your target player, you may need to tweak the container format or transcode the file on the fly.

Standard MP4 files require the player to read the metadata block (moov atom), which usually sits at the end of the file. To stream a standard MP4 effectively, you must fragment it so it can play instantly:

ffmpeg -re -i input.mp4 -c copy -listen 1 -f mp4 -movflags fragmented+empty_moov+default_base_moof http://0.0.0.0:8080

The MPEG Transport Stream format is inherently designed for streaming. It handles network interruptions gracefully and does not require complex metadata flags.

ffmpeg -re -i input.mp4 -c copy -listen 1 -f mpegts http://0.0.0.0:8080

3. On-the-Fly Transcoding (For Maximum Compatibility)

If your source video uses a heavy or unsupported codec (like H.265/HEVC) and you need to play it in a standard web browser, you can re-encode it to H.264 and AAC while streaming:

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

The -preset ultrafast flag minimizes CPU latency, ensuring the real-time transcode keeps up with the stream.


How to Connect to Your Stream

Once the command is running in your Linux terminal, FFmpeg will pause and wait for a client to connect.

  1. Find your Linux machine’s local IP address by running ip route get 1.0.0.0 | awk '{print $7}'.
  2. Open your preferred media player or web browser on the receiving device.
  3. Network URL to enter: http://<your-linux-ip>:8080

Example playback via VLC:

Open VLC, press Ctrl+N (or Cmd+N on Mac), paste your network URL, and click Play.

Limitations to Keep in Mind

Because this uses FFmpeg’s built-in basic network code, -listen 1 will serve the file to exactly one client. As soon as that client disconnects or the video finishes, the FFmpeg process will automatically terminate. If you need to serve multiple clients simultaneously, you will need to output the FFmpeg stream into a dedicated streaming server like Nginx (with the RTMP/HTTP-FLV module) or Icecast.