nginx+rtmp實現(xiàn)直播完整流程
一,環(huán)境準(zhǔn)備
1.下載nginx-rtmp-module:
cd /www/server/ git clone https://github.com/arut/nginx-rtmp-module.git
2.Nginx安裝:
這是用了寶塔哈。
軟件商店 > 應(yīng)用搜索:nginx > 安裝 > 編譯安裝 > 添加自定義模塊
模塊名稱:nginx_rtmp_module
描述: nginx rtmp
參數(shù):–add-module=/www/server/nginx-rtmp-module
3. 編輯conf配置:
新建目錄:
/www/server/nginx/conf/rtmp, 在目錄下新建兩個文件:nginx-rtmp.conf和nginx-rtmp-play.conf新建目錄:
/www/tmp/hls用于存放hls視頻文件# nginx-rtmp.conf rtmp { server { listen 1935; ping 30s; chunk_size 4000; notify_method get; application live { # 推流地址rtmp://ip:1935/live/密鑰,同拉流播放地址 live on; record off; # 是否開啟記錄 off, all,用于錄制直播視頻以便回放重播 #record_unique on; # 記錄值唯一 #record_max_size 200M; # 記錄文件大小 #record_path "/www/tmp/video"; # 記錄文件位置 #record_suffix -%Y-%m-%d-%H_%M_%S.flv; # 記錄文件命名 #on_publish http://127.0.0.1:8686/auth; # 開始推流的回調(diào)地址 #on_done 'http://when live stop call this url'; # 結(jié)束推流的回調(diào)地址 #on_play http://127.0.0.1:8686/auth; # 開始播放的回調(diào)地址 } application hls { # 推流地址rtmp://ip:1935/hls/密鑰,開啟HLS協(xié)議進(jìn)行m3u8直播 live on; hls on; # 開啟hls, hls的推流會產(chǎn)生一個m3u8的ts視頻文件索引,同時保存一個個視頻片段緩存,可以拿到再次播放。 hls_path /www/tmp/hls; # 視頻切片ts文件存放的位置 hls_sync 100ms; hls_fragment 5s; # 視頻切片的大小,ts文件大小 hls_cleanup on; #對多余的切片進(jìn)行刪除 hls_playlist_length 60s; #保存m3u8列表長度時間,默認(rèn)是30秒 } #application vod { # 用于視頻點播flv/mp4 # play /www/tmp/videos; # 本地視頻MP4文件存放地址,作為流播放視頻: rtmp://ip:1935/vod/視頻名稱.mp4 #} #application vod_http { # 播放遠(yuǎn)程http鏈接的視頻,rtmp://ip:1935/vod_http/視頻名稱.mp4 # play http://localhost:8080/vod/; #} } }# nginx-rtmp-play.conf server { listen 1000; location /stat { # http://ip:1000/stat, 監(jiān)控流的地址 rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /www/server/nginx-rtmp-module/; } location /hls { # http拉流的地址,http://ip:1000/hls/密鑰.m3u8 # Serve HLS fragments types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /www/tmp; expires -1; add_header Cache-Control no-cache; add_header Access-Control-Allow-Origin *; } }
? 編輯nginx.conf引入配置: include /www/server/nginx/conf/rtmp/nginx-rtmp.conf;

? 引入配置:include /www/server/nginx/conf/rtmp/nginx-rtmp-play.conf;

4. 重啟Nginx:

注意放行端口:1935和1000:

5. 使用OBS推流:
live推流:
推流地址:rtmp://ip:1935/live,串流密鑰:test123

瀏覽器訪問:http://ip:1000/stat可以看到推流的情況:

使用PotPlayer拉流播放:
點擊右上角的PotPlayer,選擇“打開>打開鏈接”,輸入rtmp://ip:1935/live/test123后,點擊”確定“。

HLS推流:
推流地址:rtmp://ip:1935/hls,串流密鑰:test123

使用PotPlayer拉流播放:
點擊右上角的PotPlayer,選擇“打開>打開鏈接”,輸入http://ip:1000/hls/test123.m3u8后,點擊”確定“。

6. 擴(kuò)展:瀏覽器拉流播放:
- html播放器代碼index.html(使用video.js):
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>HTML5 直播</title>
<link rel="external nofollow" rel="stylesheet">
<script src="https://vjs.zencdn.net/7.0.3/video.js"></script>
<script src="https://cdn.jsdelivr.net/npm/videojs-flash@2/dist/videojs-flash.min.js"></script>
</head>
<body style="margin: auto; width: 1080px;">
<!-- RTMP直播拉流地址 -->
<video id="rtmp-live" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="1080" height="608" data-setup='{}'>
<source src="rtmp://【IP】/live/test123" type="rtmp/flv">
</video>
<hr />
<!-- HTTP直播hls拉流地址 -->
<video id="hls-live" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="1080"height="608" data-setup='{}'>
<source src="http://【IP】:1000/hls/test123.m3u8" type="application/x-mpegURL">
</video>
</body>
</html>
- 因為本例的html需要在服務(wù)中打開,本例采用node, 所以在同一目錄下建立server.js:
(提示:不使用node啟動服務(wù)可以在IDEA中直接將index.html以服務(wù)run運行)
var http = require('http');
// 導(dǎo)入文件讀寫的js
var fs = require('fs');
var server = http.createServer(function (request, response) {
console.log('someone has visited my first node server !');
//根據(jù)訪問的路徑來選擇響應(yīng)的文件
let filePath;
if (request.url === '/') {
filePath='index.html';
} else {
filePath='notfound.html';
}
//讀取文件并寫入響應(yīng)內(nèi)容中去
fs.readFile(filePath,function(err,data ){
response.write(''+data);
//不能直接寫data 是16進(jìn)制的數(shù),需要轉(zhuǎn)成字符串
//我寫data.toString() 會報錯
response.end();
})
})
server.listen(8000, function () {
console.log('server started at http://localhost:8000/ ......')
});
提示:使用Chrome播放rtmp流的時候需要允許Flash執(zhí)行,而使用hls播放m3u8則不用
命令安裝rtmp模塊
下載并解壓 RTMP 模塊
cd /www/server/nginx/src/ git clone https://github.com/arut/nginx-rtmp-module.git
安裝 pcre 庫
sudo apt update sudo apt install libpcre3 libpcre3-dev
cd /www/server/nginx/src/ wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz tar -xzvf pcre-8.43.tar.gz
cd pcre-8.43 ./configure make sudo make install
確保 Nginx 配置正確
cd ~/nginx-1.24.0/ ./configure --user=www --group=www --prefix=/www/server/nginx \ --add-module=/www/server/nginx/src/ngx_devel_kit \ --add-module=/www/server/nginx/src/lua_nginx_module \ --add-module=/www/server/nginx/src/ngx_cache_purge \ --with-openssl=/www/server/nginx/src/openssl \ --with-pcre=/www/server/nginx/src/pcre-8.43 \ --with-http_v2_module \ --with-stream \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_image_filter_module \ --with-http_gzip_static_module \ --with-http_gunzip_module \ --with-ipv6 \ --with-http_sub_module \ --with-http_flv_module \ --with-http_addition_module \ --with-http_realip_module \ --with-http_mp4_module \ --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module-master \ --with-ld-opt=-Wl,-E \ --with-cc-opt=-Wno-error \ --with-http_dav_module \ --add-module=/www/server/nginx/src/nginx-dav-ext-module \ --add-module=/www/server/nginx/src/nginx-rtmp-module \ --with-ld-opt="-Wl,-rpath,/usr/local/lib/"
安裝
make make install
檢查是否成功,在輸入下方命令后查找是否安裝完成trmp模塊
nginx -V
如果提示沒有找到luajit查看是否安裝
luajit -v
如果已經(jīng)安裝查找安裝目錄
whereis luajit
根據(jù)目錄設(shè)置環(huán)境變量
export LUAJIT_LIB=/usr/local/lib export LUAJIT_INC=/usr/local/include/luajit-2.1
到此這篇關(guān)于nginx+rtmp實現(xiàn)直播完整流程的文章就介紹到這了,更多相關(guān)nginx rtmp 直播內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- nginx-rtmp-module構(gòu)建流媒體直播服務(wù)器實戰(zhàn)指南
- Nginx通過nginx-rtmp-module模塊搭建流媒體服務(wù)器實現(xiàn)直播
- 利用nginx搭建RTMP視頻點播、直播、HLS服務(wù)器
- 使用Nginx搭載rtmp直播服務(wù)器的方法
- Nginx搭建rtmp直播服務(wù)器實現(xiàn)代碼
- 詳解Ubuntu18.04下配置Nginx+RTMP+HLS+HTTPFLV服務(wù)器實現(xiàn)點播/直播/錄制功能
- Nginx-rtmp實現(xiàn)直播媒體實時流效果
- nginx使用nginx-rtmp-module模塊實現(xiàn)直播間功能
- Mac上搭建nginx+rtmp直播服務(wù)器的步驟詳解
相關(guān)文章
Nginx+uwsgi+ssl配置https的詳細(xì)步驟
nginx是一個輕量級的web服務(wù)器,在處理靜態(tài)資源和高并發(fā)有優(yōu)勢,uwsgi是一個基于python的高效率的協(xié)議,處理后端和動態(tài)網(wǎng)頁有優(yōu)勢,我這里使用的是Ubuntu18.04版本,服務(wù)器在阿里云,感興趣的朋友跟隨小編一起看看吧2023-10-10
Nginx網(wǎng)站根目錄更改及導(dǎo)致403 forbidden的問題解決
最近因為工作需要,要將Nginx網(wǎng)站根目錄更改下,通過網(wǎng)上的一些教程更改后,但發(fā)現(xiàn)測試的時候一直提示403 forbidden錯誤,后臺通過一個朋友的提示也解決了,所以現(xiàn)在將詳細(xì)的步驟分享給大家,有需要的朋友們可以參考學(xué)習(xí)。2016-10-10

