How to Stream Video Over WebRTC Using FFmpeg

This guide explains how to stream real-time video over WebRTC by combining the processing power of FFmpeg with third-party media servers. You will learn the architectural role of intermediary servers, how to set up a media gateway like MediaMTX, and the exact FFmpeg commands required to ingest video for low-latency WebRTC playback in browsers.

Why You Need a Third-Party Integration

FFmpeg is an excellent tool for encoding and muxing video, but it does not natively handle the complex network signaling, ICE negotiation, and peer-to-peer handshakes required by the WebRTC protocol. To stream WebRTC using FFmpeg, you must route your FFmpeg stream through a third-party media server. The media server acts as a gateway, accepting a standard protocol from FFmpeg (such as RTSP, RTMP, SRT, or WHIP) and translating it into a WebRTC stream for end-users.

Step 1: Choose and Set Up a Media Server

Several open-source media servers can convert incoming video feeds into WebRTC. Popular options include MediaMTX, Janus Gateway, SRS (Simple Realtime Server), and Pion.

For this guide, we will use MediaMTX because it is lightweight, requires no external dependencies, and supports WebRTC out of the box.

  1. Download and run the MediaMTX binary for your operating system.
  2. Open the configuration file (mediamtx.yml) and ensure WebRTC is enabled (it is enabled by default on port 8889 for HTTP/WHEP signaling).

Step 2: Stream Video from FFmpeg to the Media Server

FFmpeg can push video to the media server using RTSP, which the media server will automatically convert to WebRTC. WebRTC requires specific codecs: H.264 for video (with baseline or constrained profiles for maximum browser compatibility) and Opus for audio.

Run the following FFmpeg command to stream a local video file (or camera input) to MediaMTX:

ffmpeg -re -i input.mp4 -c:v libx264 -pix_fmt yuv420p -profile:v baseline -preset ultrafast -tune zerolatency -c:a libopus -b:a 128k -f rtsp rtsp://localhost:8554/mystream

Key Parameters Explained:

Step 3: Stream Directly via WHIP (Alternative Method)

If you are using a modern version of FFmpeg (version 6.0 or newer) and a media server that supports WHIP (WebRTC HTTP Ingestion Protocol), you can stream WebRTC directly without using RTSP as an intermediary.

Use the following command to ingest via WHIP:

ffmpeg -re -i input.mp4 -c:v libx264 -pix_fmt yuv420p -preset ultrafast -tune zerolatency -c:a libopus -f whip http://localhost:8889/mystream/whip

Step 4: Play the WebRTC Stream in a Browser

Once FFmpeg is actively pushing the stream to the media server, you can consume the WebRTC stream in any modern web browser.

If you are using MediaMTX, you can view the live stream by navigating to: http://localhost:8889/mystream

The media server serves a built-in HTML page containing a WebRTC player (utilizing WHEP - WebRTC HTTP Egress Protocol) that establishes a peer connection and plays your video stream with sub-second latency.