How to Stream RTSP to WebSocket with FFmpeg

Streaming live video from an RTSP IP camera to a web browser often requires converting the feed into a WebSocket-compatible format for low-latency playback. Since web browsers cannot native play RTSP streams, FFmpeg is used to capture the camera feed, transcode it, and send it to a WebSocket relay server. This guide provides the exact FFmpeg command required to achieve this pipeline, along with a clear explanation of how each parameter works.

The Streaming Pipeline

Because FFmpeg cannot act as a WebSocket server directly, the standard approach is to stream the video from FFmpeg to a local relay server (such as a Node.js helper script or jsmpeg relay) via TCP or HTTP. The relay server then broadcasts the incoming stream to connected web browsers via WebSockets.

The FFmpeg Command

Run the following command in your terminal to capture the RTSP stream, transcode it to MPEG-TS (highly compatible with web-based WebSocket players like JSMpeg), and output it to your local relay server:

ffmpeg -rtsp_transport tcp -i rtsp://username:password@camera_ip:554/stream_path -f mpegts -codec:v mpeg1video -s 1280x720 -b:v 1500k -r 30 -bf 0 -codec:a mp2 -ar 44100 -ac 1 http://127.0.0.1:8081/supersecret

Command Parameter Breakdown

By running this command alongside a WebSocket relay utility, you can achieve near-instantaneous IP camera viewing inside any modern web browser without requiring third-party browser plugins.