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-i "rtsp://your-stream-url/live": Specifies the input live stream URL.-c:v libvpx-vp9: Encodes the video to VP9.-b:v 1M: Sets the video bitrate to 1 Mbps (adjust based on your bandwidth and quality needs).-c:a libopus: Encodes the audio to Opus.-b:a 128k: Sets the audio bitrate to 128 kbps.output.webm: The destination file.
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.webmKey Performance Flags Explained:
-deadline realtime: Forces the encoder to prioritize real-time encoding speeds, ensuring it keeps up with the incoming live stream.-cpu-used 4: Tells the encoder how much effort to put into compression. Settings range from 0 to 8. Higher numbers (e.g., 4 to 8) significantly speed up encoding at the cost of slightly lower quality per bitrate.
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