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:8080Breaking Down the Flags
-re: This flag reads the input file at its native frame rate (real-time). Without it, FFmpeg will dump the data as fast as your CPU allows, which will overwhelm the streaming buffer and cause playback to fail.-i input.mp4: Specifies the path to your local media file.-c copy: Stream copies the video and audio codecs without re-encoding them. This uses virtually zero CPU power, but requires the source file to already be in a format compatible with your playback device.-listen 1: Instructs FFmpeg to act as an HTTP server and accept an incoming connection.-f mp4: Forces the output format to MP4. For streaming, you can also usempegtsorwebm.http://0.0.0.0:8080: Binds the server to port 8080 on all available network interfaces, allowing other devices on your local network to connect.
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.
1. Fragmented MP4 (Recommended for Browsers)
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:80802. MPEG-TS (Recommended for VLC and Media Players)
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:80803. 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:8080The
-preset ultrafastflag 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.
- Find your Linux machine’s local IP address by running
ip route get 1.0.0.0 | awk '{print $7}'. - Open your preferred media player or web browser on the receiving device.
- 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.