提示,這不是一個手把手的教程,更多是說了一個思路,需要對docker,系統(tǒng),對程序編譯,有初步認(rèn)識即可。
劇情介紹
有這么一個需求,需要一個服務(wù)展示多個視頻流,并且錄制視頻流,然后就這些
思路:
整體結(jié)構(gòu)是,用戶端obs推流,服務(wù)器端nginx_rtmp做轉(zhuǎn)發(fā)及錄像
整體有這么兩種思路,一個是docker的nginx_rtmp,這個優(yōu)點(diǎn),速度非常快,非常方便,可以說瞬間解決了大部分問題(服務(wù)器端及轉(zhuǎn)發(fā)),問題就是,,不能錄像。
另一個就是自己安裝nginx和rtmp模塊,改配置文件。自定義余量大,可控范圍廣,缺點(diǎn),配置。。。。略微有點(diǎn)復(fù)雜。
方法1
安裝docker(不會自己學(xué)習(xí))
sudo docker run \
-p 1935:1935 \
-p 8080:8080 \
-e RTMP_STREAM_NAMES=live,st1 \
-d \
--rm \
jasonrivers/nginx-rtmp
簡單說兩句
-p 外部端口:內(nèi)部端口
-e 此次使用中的參數(shù)指開兩個頻道 live和st1
-d 容器啟動后,在后臺運(yùn)行
--rm 容器終止運(yùn)行后,自動刪除容器文件
jasonrivers/nginx-rtmp 這個就是此次使用的鏡像https://github.com/JasonRivers/Docker-nginx-rtmp
執(zhí)行完上面的命令,就可以開始推流和查看了
這里說一下踩過的一些坑,本來打算的是-v掛載文件系統(tǒng),直接使用宿主機(jī)的配置文件替換掉鏡像里面的配置文件,但是執(zhí)行后發(fā)現(xiàn)不行,仔細(xì)看了一下上面image的dockfile文件里面有個run腳本,整體流程是,啟動鏡像,然后用run.sh腳本生成了最后的nginx.conf文件,這個也就是上面-e參數(shù)的作用,直接-v指定是無效的。
方法2
這個思路說簡單也簡單,說復(fù)雜也復(fù)雜
下載nginx安裝包,下載rtmp模塊,編譯,安裝,修改配置文件,啟動運(yùn)行
先說踩的一個坑,不要用ubuntu,即便關(guān)了selinux,后面也會出現(xiàn)http播放正常,rtmp無法播放的問題,我是用centos7解決這個問題。
關(guān)鍵的nginx配置文件來了
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2ts ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
location /on_publish {
return 201;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /opt/nginx/conf/stat.xsl;
}
location /control {
rtmp_control all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
on_publish http://localhost:8080/on_publish;
hls on;
hls_path /tmp/hls;
}
application st1 {
live on;
record video;
record_path /tmp/;
record_suffix -%d-%b-%y-%T.flv;
record_unique on;
record_max_size 100M;
on_publish http://localhost:8080/on_publish;
}
}
}
root /opt/nginx/conf/stat.xsl;這個需要根據(jù)實(shí)際情況進(jìn)行配置,看你的文件在那,記住就是了,回來可以通過訪問http://ip:port/stat 查看服務(wù)器狀態(tài),接入頻道,用戶,chrome看有問題,無法直接顯示內(nèi)容,查看頁面源文件,可以看到內(nèi)容。
這個配置文件的效果是生成兩個頻道,live,st1,其中st1進(jìn)行錄像
record [off|all|audio|video|keyframes|manual]*
record video;
record_path /tmp/rec;
mystream-24-Apr-13-18:23:38.flv
record_suffix -%d-%b-%y-%T.flv;
If turned on appends current timestamp to recorded files. Otherwise the same file is re-written each time
new recording takes place
record_unique on;
record_max_size 100M;
其他record的參數(shù),參見
https://github.com/arut/nginx-rtmp-module/wiki/Directives#record
2018年3月6日17:20:31