rtsp用ffmpeg转hls播放

9 篇文章 0 订阅
8 篇文章 0 订阅
文章介绍了如何使用FFMPEG将RTSP流转换为HLS,以便在Web浏览器中播放。首先,RTSP是用于在线视频流的协议,适用于IP摄像头流。由于RTSP不能直接通过HTTP流式传输,因此需要FFMPEG将其转换为HLS。接着,通过运行FFMPEG命令将RTSP流实时转换为HLS。最后,利用HLS.js库和HTML代码,可以将HLS流传递给Web浏览器进行播放。
摘要由CSDN通过智能技术生成

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
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

Index.html

<!DOCTYPE html>
<html>
<head>
  <title>Live Cam</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<body>
  <!-- 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>
    </div>
-->
  <video id="video" autoplay="true" controls="controls" type='application/x-mpegURL'></video>
  <script>
    if (Hls.isSupported()) {
      var video = document.getElementById('video');
      var hls = new Hls();
      // bind them together
      hls.attachMedia(video);
      hls.on(Hls.Events.MEDIA_ATTACHED, function () {
        console.log("video and hls.js are now bound together !");
        hls.loadSource("http://192.1xx.x.1xx:8080/playlist.m3u8");
        hls.on(Hls.Events.MANIFEST_PARSED, function (event, data) {
        });
      });
    }
  </script>
</body>
</html>

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.

setup-ffmpeg.sh

#!/bin/bash
VIDSOURCE="rtsp://192.1xx.x.xxx:5554"
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

In the bash file, I have provided VIDSOURCE="rtsp://192.1xx.x.xxx:5554" 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

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

FFmpeg是一款强大的多媒体处理工具,可以用来完成各种视频音频处理任务,其中包括将RTSP换成HLS流。 RTSP(实时流媒体传输协议)是一种用于实时数据传输的网络协议,常用于视频流的传输。HLS(HTTP Live Streaming)是一种流媒体传输协议,可以将视频切分为多个小文件,并通过HTTP协议分段传输。 要实现RTSPHLS,首先需要安装FFmpeg工具。在安装完毕后,可以使用以下命令行参数来进行换: ```shell ffmpeg -i rtsp://source_url -c:v copy -c:a copy -hls_segment_type mpegts -hls_time 10 -hls_list_size 0 -f hls output.m3u8 ``` 上述命令中,使用`-i`参数指定RTSP输入源的URL,`-c:v copy`和`-c:a copy`表示不对视频和音频进行编码,直接复制到HLS输出中。 `-hls_segment_type mpegts`指定输出的HLS段文件类型为`mpegts`,`-hls_time 10`表示每个HLS段的时长为10秒,`-hls_list_size 0`表示不限制目标播放列表的最大文件数量。 最后使用`-f hls`参数指定输出格式为HLS,并指定输出文件名为`output.m3u8`。 执行上述命令后,FFmpeg会将RTSP换成HLS流。输出的`output.m3u8`文件是一个HLS播放列表文件,其中包含了各个切片文件的URL地址。 通过将`output.m3u8`和切片文件(.ts文件)部署在HTTP服务器上,即可使用支持HLS协议的播放器进行在线播放。 需要注意的是,换过程中可能会出现一些兼容性或格式方面的问题。在使用FFmpeg进行RTSPHLS时,可以根据具体需求调整命令行参数以达到最佳效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值