ffmpeg輕鬆轉碼rtsp到瀏覽器中播放

最近需要把hikvision的rtsp流播放在瀏覽器中,由於rtmp和flv都需要flash支持,故定了rtsp轉hls的解決方案。

環境爲ubuntu18,linux下手動編譯安裝用戶軟件,源碼放在/usr/local/src中,安裝路徑在/usr/local下。權限問題可使用sudo命令。

準備

安裝編譯需要用到的庫和工具

apt-get install build-essential libtool gcc automake autoconf make

安裝pcre,支持rewrite功能
源碼地址:https://ftp.pcre.org/pub/pcre/

wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz 
tar -zxvf pcre-8.40.tar.gz
cd pcre-8.40.tar.gz
./configure
sudo make
sudo make install

安裝zlib,支持gzip壓縮
源碼地址:http://zlib.net/

wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
sudo make
sudo make install

安裝ssl
源碼地址:https://www.openssl.org/source/

wget https://www.openssl.org/source/openssl-1.0.2o.tar.gz
tar -zxvf openssl-1.0.2o.tar.gz
cd openssl-1.0.2o
./config
sudo make
sudo make install

nginx的搭建

1. 下載編譯nginx源碼及rtmp模塊

源碼地址:http://nginx.org/en/download.html
rtmp模塊源碼:https://github.com/arut/nginx-rtmp-module

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0.tar.gz

./configure --sbin-path=/usr/local/nginx/nginx 
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.40 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.0.2o \
--add-module=/[path:源碼地址]/nginx-rtmp-module

sudo make
sudo make install

編譯完成後,通過命令nginx -V查看版本及配置參數。
在這裏插入圖片描述

2. 檢查80端口是否被佔用

使用命令:

netstat -ano | grep 80
lsop -i:80

3. 啓動配置

nginx命令添加到全局命令:

sudo cp /usr/local/nginx/nginx /user/local/sbin/

在nginx的配置文件nginx.conf中http模塊上面加入下面內容:

rtmp {
    server {
		
		listen 1935;
		chunk_size 4000;

		application hls {
			live on;
			hls on;
			hls_path /user/local/nginx/html/live;
			hls_fragment 5s;
		}
	}
}

編寫index

接下來進入html文件夾,index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Live Cam</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>

<video id="video" autoplay="true" controls="controls" style="width:500px;height:300px"></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 !");
        hls1.loadSource("http://localhost/live/mystream.m3u8");
        hls1.on(Hls.Events.MANIFEST_PARSED, function (event, data) {
          console.log("manifest loaded, found " + data.levels.length + " quality level");
        });
      });
    }
</script>

</html>

編寫運行腳本

html文件夾下新建live文件夾,chmod命令更改權限。
新建ffmpeg轉碼腳本stream.sh:

#!/bin/bash
VIDSOURCE="rtsp://[username]:[password]@[ip]:554/"
AUDIO_OPTS="-c:a libfaac -b:a 32k -ac 2"
VIDEO_OPTS="-s 854*480 -vcodec copy -b:v 800000"
OUTPUT_HLS="-hls_time 5 -hls_list_size 10 -start_number 1"
ffmpeg -i "$VIDSOURCE" -y $AUDIO_OPTS $VIDEO_OPTS $OUTPUT_HLS mystream.m3u8

通過命令./stream.sh,開始轉碼。瀏覽器中輸入本地地址即可
在這裏插入圖片描述

如有其他問題歡迎留言交流。

在這裏插入圖片描述歡迎關注公衆號獲取源碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章