nginx+rtmp實(shí)現(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, 在目錄下新建兩個(gè)文件: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)生一個(gè)m3u8的ts視頻文件索引,同時(shí)保存一個(gè)個(gè)視頻片段緩存,可以拿到再次播放。 hls_path /www/tmp/hls; # 視頻切片ts文件存放的位置 hls_sync 100ms; hls_fragment 5s; # 視頻切片的大小,ts文件大小 hls_cleanup on; #對多余的切片進(jìn)行刪除 hls_playlist_length 60s; #保存m3u8列表長度時(shí)間,默認(rèn)是30秒 } #application vod { # 用于視頻點(diǎn)播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拉流播放:
點(diǎn)擊右上角的PotPlayer,選擇“打開>打開鏈接”,輸入rtmp://ip:1935/live/test123后,點(diǎn)擊”確定“。

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

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

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>
- 因?yàn)楸纠膆tml需要在服務(wù)中打開,本例采用node, 所以在同一目錄下建立server.js:
(提示:不使用node啟動(dòng)服務(wù)可以在IDEA中直接將index.html以服務(wù)run運(yùn)行)
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() 會報(bào)錯(cuò)
response.end();
})
})
server.listen(8000, function () {
console.log('server started at http://localhost:8000/ ......')
});
提示:使用Chrome播放rtmp流的時(shí)候需要允許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實(shí)現(xiàn)直播完整流程的文章就介紹到這了,更多相關(guān)nginx rtmp 直播內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- nginx-rtmp-module構(gòu)建流媒體直播服務(wù)器實(shí)戰(zhàn)指南
- Nginx通過nginx-rtmp-module模塊搭建流媒體服務(wù)器實(shí)現(xiàn)直播
- 利用nginx搭建RTMP視頻點(diǎn)播、直播、HLS服務(wù)器
- 使用Nginx搭載rtmp直播服務(wù)器的方法
- Nginx搭建rtmp直播服務(wù)器實(shí)現(xiàn)代碼
- 詳解Ubuntu18.04下配置Nginx+RTMP+HLS+HTTPFLV服務(wù)器實(shí)現(xiàn)點(diǎn)播/直播/錄制功能
- Nginx-rtmp實(shí)現(xiàn)直播媒體實(shí)時(shí)流效果
- nginx使用nginx-rtmp-module模塊實(shí)現(xiàn)直播間功能
- Mac上搭建nginx+rtmp直播服務(wù)器的步驟詳解
相關(guān)文章
Nginx響應(yīng)頭Vary介紹與應(yīng)用小結(jié)
響應(yīng)頭部字段在控制緩存行為、優(yōu)化性能等方面起著重要作用,Vary頭部字段是其中一個(gè)關(guān)鍵字段,它用于指示緩存代理在何種條件下緩存響應(yīng),下面就來詳細(xì)的介紹一下,感興趣的可以了解一下2025-09-09
詳解php+nginx 服務(wù)發(fā)生500 502錯(cuò)誤排查思路
這篇文章主要介紹了詳解php+nginx 服務(wù)發(fā)生500 502錯(cuò)誤排查思路,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
iis+nginx實(shí)現(xiàn)負(fù)載均衡的詳細(xì)步驟
這篇文章主要為大家詳細(xì)介紹了iis+nginx實(shí)現(xiàn)負(fù)載均衡的詳細(xì)步驟 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
關(guān)閉nginx空主機(jī)頭 防止nginx空主機(jī)頭及惡意域名指向
nginx的默認(rèn)配置中的虛擬主機(jī)允許用戶通過IP訪問,或者通過未設(shè)置的域名訪問,比如有人惡意把他自己的域名指向了你的ip,需要的朋友可以參考下2016-09-09
nginx臨時(shí)搭建rtmp服務(wù)器的方法實(shí)現(xiàn)
nginx是一款優(yōu)秀的反向代理工具,通過Nginx自帶的rtmp模塊,也可以實(shí)現(xiàn)rtmp服務(wù)器的搭建,本文主要介紹了nginx臨時(shí)搭建rtmp服務(wù)器,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02

