Capture Live Stream to WebM Using FFmpeg

This article provides a straightforward guide on how to capture a live video stream—such as an RTMP, RTSP, or HTTP feed—and save it directly into a WebM container using FFmpeg. You will learn the exact command-line arguments needed, the compatible codecs required for the WebM format, and optimization flags to ensure smooth, real-time encoding without dropping frames.

Understanding WebM Requirements

The WebM container format is strictly defined and only supports specific codecs. To write a stream to a .webm file, you must transcode the incoming video and audio using the following supported formats: * Video Codecs: VP8 or VP9 (libvpx or libvpx-vp9) * Audio Codecs: Opus or Vorbis (libopus or libvorbis)

Because live streams often arrive in H.264/AAC formats, re-encoding is necessary to package them into a WebM container.

The Basic Command

To capture an incoming RTSP or RTMP stream and convert it to WebM in real-time, use the following template:

ffmpeg -i "rtsp://your-stream-url/live" -c:v libvpx-vp9 -b:v 1M -c:a libopus -b:a 128k output.webm

Optimizing for Real-Time Encoding

VP9 encoding is CPU-intensive. When processing a live stream, standard encoding settings can cause latency, lag, or dropped frames. To prevent this, you must instruct the VP9 encoder to prioritize speed over maximum compression efficiency.

Use the optimized command below for live streams:

ffmpeg -i "rtmp://your-stream-url/live" \
  -c:v libvpx-vp9 \
  -b:v 2M \
  -deadline realtime \
  -cpu-used 4 \
  -c:a libopus \
  -b:a 128k \
  output.webm

Key Performance Flags Explained:

Alternative: Using VP8 for Lower CPU Usage

If your system’s hardware is struggling to encode VP9 in real-time, you can use the older VP8 video codec and Vorbis audio codec. This setup requires much less processing power:

ffmpeg -i "rtsp://your-stream-url/live" \
  -c:v libvpx \
  -b:v 1.5M \
  -quality good \
  -cpu-used 5 \
  -c:a libvorbis \
  -b:a 128k \
  output.webm