DEV Community

Cover image for RTSP stream to Web Browser using FFMPEG

RTSP stream to Web Browser using FFMPEG

tejasvi2 profile image Tejasvi Shinde ・3 min read

If you need to stream your video from the webcam to your browser webpage.
To achieve this I have tried a few different ways but none of them is as good as converting RTSP to HLS and then pass to Browser.

We will follow the below steps

  1. RTSP stream
  2. Understanding FFMPEG
  3. Converting RTSP TO HLS
  4. Passing HLS to a web browser

1.RTSP Stream

What is RTSP?
RTSP, also known as Real-Time Streaming Protocol, is a lesser-known protocol for streaming video online. This protocol was designed to control the streaming servers used in entertainment and communications systems.

When the RTSP controls the server to client connection, video-on-demand streams are used; when it controls the client to server connection, RTSP utilizes voice recording streams.

RTSP commonly is used for Internet Protocol (IP) camera streaming, such as those coming from CCTV or IP cameras.
Rather than forcing your viewers to download an entire video before watching it, the RTSP stream allows them to watch your content before the download is complete.

You cannot directly stream RTSP over HTTP. Because of this, there is no easy, straightforward way to stream RTSP in a web browser, as RTSP is designed more for streaming video on private networks such as security systems within a business. However, you can stream RTSP using additional software that’s embedded onto your website.
Furthermore, to achieve this I have used FFMPEG

2. Understanding FFMPEG

FFmpeg is a command-line tool that converts audio or video formats. It can also capture and encode in real-time from various hardware and software sources such as a TV capture card.
Basically, it is a wrapper that converts RTSP to HLS.
(HLS stands for HTTP Live Streaming. In short, HLS is a media streaming protocol for delivering visual and audio media to viewers over the internet and supported by the web browser)

Check this FFMPEG for more information

3. Converting RTSP TO HLS

To Achieve this we have to use FFMPEG commands.
Basically from node, I will run bash file which has those commands this will run in the background and when it receives RTSP stream It parallelly changes them to HLS.

4. Passing HLS to a web browser

we are almost done because passing HLS to a web browser is easy.

I hope you guys got a basic idea about this conversion.

Remember to run this we need a server to run in the background it can be simple HTTP-SERVER or NGX-SERVER.

Code Implementation

<!DOCTYPE html>
  <title>Live Cam</title>
<script src=""></script>
  <!-- Use this if you only support Safari!!
    <div id="player">
        <video id="video" autoplay="true" controls="controls">
            <source src="http://192.1xx.x.1xx:8080/playlist.m3u8" />
            Your browser does not support HTML5 streaming!
  <video id="video" autoplay="true" controls="controls" type='application/x-mpegURL'></video>
    if (Hls.isSupported()) {
      var video = document.getElementById('video');
      var hls = new Hls();
      // bind them together
      hls.on(Hls.Events.MEDIA_ATTACHED, function () {
        console.log("video and hls.js are now bound together !");
        hls.on(Hls.Events.MANIFEST_PARSED, function (event, data) {

Enter fullscreen mode Exit fullscreen mode

In code you can see I have added the HLS CDN link you can use NPM Package too.
You can see in this line code hls.loadSource("http://192.1xx.x.1xx:8080/playlist.m3u8");

My HTTP-SERVER is running on http://192.1xx.x.1xx:8080 and /playlist.m3u8 I will come to this.
AUDIO_OPTS="-c:a aac -b:a 160000 -ac 2"
VIDEO_OPTS="-s 854x480 -c:v libx264 -b:v 800000"
OUTPUT_HLS="-hls_time 10 -hls_list_size 10 -start_number 1"
ffmpeg -i "$VIDSOURCE" -y $AUDIO_OPTS $VIDEO_OPTS $OUTPUT_HLS playlist.m3u8
Enter fullscreen mode Exit fullscreen mode

In the bash file, I have provided VIDSOURCE="rtsp://" RTSP link.
You can see it at the end playlist.m3u8. This will create a file playlist.m3u8 and start dumping stream one after another so at the end we will refer to this file.

When you run the bash file you can see the changes in your folder like this

Alt Text

FFMPEG provides a lot of useful commands. You can try a different set of commands and use them as per your requirements.

Discussion (0)

Editor guide