通過本文將學會在騰訊雲服務器上實現簡單的直播系統:
1.ngnix 流媒體服務器搭建
2.obs客戶端推流
3.瀏覽器觀看直播
一、動手搭建流媒體服務器
安裝wget
yum -y install wget
安裝 gcc gcc-c++
yum -y install gcc gcc-c++
安裝PCRE庫
cd /usr/local/
wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz
tar -zxvf pcre-8.33.tar.gz
cd pcre-8.33
./configure
make && make install
安裝openssl
cd /usr/local/
wget https://www.openssl.org/source/openssl-1.1.0j.tar.gz
tar xf openssl-1.1.0j.tar.gz
cd openssl-1.1.0j
./config
make && make install
如果提示You need Perl 5,則輸入下面這個命令。
yum -y install Perl 5
安裝zlib
cd /usr/local/
wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar xf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install
下載nginx-rtmp-module 模塊
cd /usr/local/
yum -y install git
git clone https://github.com/arut/nginx-rtmp-module.git
安裝nginx
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar xf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module --with-pcre=../pcre-8.33 --with-openssl=../openssl-1.1.0j --with-zlib=../zlib-1.2.11
make && make install
如果提示
./configure: error: SSL modules require the OpenSSL library.
執行
yum -y install openssl openssl-devel
如果提示
./configure: error: the HTTP rewrite module requires the PCRE library.
執行
yum -y install pcre-devel
我下載並安裝了pcre-8.33.tar.gz 和 openssl-1.0.1j.tar.gz ,並沒有出現上面的錯誤
nginx相關命令
啓動
/usr/local/src/nginx/sbin/nginx
重啓
/usr/local/src/nginx/sbin/nginx –s reload
二、obs實現客戶端推流
nginx.conf 配置
nginx.conf 的位置一般是在/usr/local/nginx/conf中,打開文件
添加下方代碼
rtmp {
out_queue 4096;
out_cork 8;
max_streams 128;
timeout 15s;
server {
listen 1935; #監聽的端口
chunk_size 4000;
application live {
live on; #開啓實時
hls on; #開啓hls
hls_path /usr/local/nginx/html/live; #rtmp推流請求路徑,文件存放路徑
hls_fragment 5s; #每個TS文件包含5秒的視頻內容
}
}
}
添加位置是在http上方
配置寫好之後,重啓 Nginx 服務器:
sudo nginx -s reload
如果報錯,則說明代碼添加的有問題,注意檢查。
輸入雲服務器ip得到以下界面,則說明已經配置好服務器。
下面測試rtmp端口是否打開
輸入上述命令,發現端口未開。需要做兩件事情。
1.將騰訊安全組的相對應的端口打開。
2.設置推流端口
systemctl stop firewalld 關閉防火牆
安裝或更新服務
yum install iptables-services
再使用 systemctl enable iptables 設置開機啓動
iptables -I INPUT -p tcp -m state --state NEW --dport 1935 -j ACCEPT #放行1935端口的 NEW狀態請求
service iptables save #保存規則
service iptables restart #重啓防火牆保證新的規則加載進來
這樣再使用telnet 命令就可以看到
說明成功!
obs客戶端
下載地址:https://cdn-fastly.obsproject.com/downloads/OBS-Studio-24.0.3-Full-Installer-x64.exe
打開文件--> 設置 按照下面進行設置
在來源處,點擊+
點擊顯示此捕獲
如果出現黑屏,可以通過該鏈接https://jingyan.baidu.com/article/c910274bac0d51cd361d2d18.html 找到解決辦法
最終出現上面的內容,說明配置成功。
obs推流
按照下面進行配置,對了,需要在
/usr/local/nginx/html 下建立文件 live文件夾,用於存放相關視頻
在obs中進行如下配置:
點擊開始推流,就完成了。變成綠色就對了
三、瀏覽器觀看設置
新建文件index.html,如下
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>前端播放m3u8格式視頻</title>
<link rel="stylesheet" href="http://vjs.zencdn.net/5.5.3/video-js.css">
<script src="http://vjs.zencdn.net/5.5.3/video.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-contrib-hls/5.12.2/videojs-contrib-hls.js"></script>
</head>
<body>
<video id="myVideo" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="728" height="410" data-setup='{}'>
<source id="source" src="http://<你的ip>/live/myvideo.m3u8" type="application/x-mpegURL">
</video>
</body>
<script>
// videojs 簡單使用
var myVideo = videojs('myVideo',{
bigPlayButton : true,
textTrackDisplay : false,
posterImage: false,
errorDisplay : false,
})
myVideo.play() // 視頻播放
myVideo.pause() // 視頻暫停
</script>
</html>
將文件放入 /usr/local/nginx/html 目錄下,注意裏面sorce 是需要修改的。
最後打開雲服務器,就可以看到直播內容了
參考博客:
[1]https://hywlovexyc.info/blog/archives/572/
[2]https://blog.fuwenwei.com/2019/01/02/H5-live-02.html#32-%E4%BD%BF%E7%94%A8-hls-%E6%92%AD%E6%94%BE