資源地址
nginx-rtmp: https://github.com/arut/nginx...
OBS推流: https://obsproject.com/
HTTP Live Streaming
(HLS)是蘋果公司實現的基於HTTP
的流媒體傳輸協議,可實現流媒體的直播和點播,相對於常見的流媒體直播協議,例如RTMP
協議、RTSP
協議、MMS
協議等,HLS
直播最大的不一樣在於,直播客戶端獲取到的,並非一個完整的數據流。HLS
協議在服務器端將直播數據流存儲爲連續的、很短時長的媒體文件(MPEG-TS
格式),而客戶端則不斷的下載並播放這些小文件,由於服務器端老是會將最新的直播數據生成新的小文件,這樣客戶端只要不停的按順序播放從服務器獲取到的文件,就實現了直播。因而可知,基本上能夠認爲,HLS
是以點播的技術方式來實現直播。因爲數據經過HTTP
協議傳輸,因此徹底不用考慮防火牆或者代理的問題,並且分段文件的時長很短,客戶端能夠很快的選擇和切換碼率,以適應不一樣帶寬條件下的播放。不過HLS
的這種技術特色,決定了它的延遲通常老是會高於普通的流媒體直播協議。css
cd /tmp #wget http://nginx.org/download/nginx-1.14.0.gar.gz # 使用華爲雲鏡像 wget https://mirrors.huaweicloud.com/nginx/nginx-1.14.0.tar.gz # 解壓縮 tar -xvzf ./nginx-1.14.0.tar.gz
# 下載nginx-rtmp模塊源碼 wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz # 解壓縮,這個須要用到,轉入正式目錄 tar -xvzf ./v1.2.1.tar.gz mv /tmp/nginx-rtmp-module-1.2.1 /usr/local/nginx-rtmp-module
yum -y install pcre* openssl openssl-devel
# 建立組 groupadd nginx # 建立不容許登陸,無主目錄的用戶 useradd -s /sbin/nologin -g nginx -M nginx
cd /tmp/nginx-1.14.0 ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=/usr/local/nginx-rtmp-module make & make install
參數 | 說明 |
---|---|
--user=nginx | 指定運行權限的用戶 |
--group=nginx | 指定運行的權限用戶組 |
--prefix=/usr/local/nginx | 指定安裝路徑 |
--with-http_stub_status_module | 支持nginx狀態查詢 |
--with-http_ssl_module | 開啓ssl支持 |
--with-http_gzip_static_module | 開啓GZIP功能 |
--add-module=/usr/local/nginx-rtmp-module | 添加nginx-rtmp-module模塊 |
vim /usr/lib/systemd/system/nginx.service [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
vim /usr/local/nginx/conf/nginx.conf user nginx; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include ./conf.d/*.conf; } rtmp { server { # 監聽端口 listen 1935; chunk_size 2048; # HLS # For HLS to work please create a directory in tmpfs (/tmp/hls here) # for the fragments. The directory contents is served via HTTP (see # http{} section in config) # # Incoming stream must be in H264/AAC. For iPhones use baseline H264 # profile (see ffmpeg example). # This example creates RTMP stream from movie ready for HLS: # # ffmpeg -loglevel verbose -re -i movie.avi -vcodec libx264 # -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1 # -f flv rtmp://localhost:1935/hls/movie # # If you need to transcode live stream use 'exec' feature. # # rtmp推流請求路徑 application hls { # 開啓實時 live on; # 開啓hls hls on; # 對視頻切片進行保護,這樣就不會產生馬賽克了 wait_key on; # rtmp推流請求路徑,文件存放路徑 hls_path /data/video/hls; # 每一個TS文件包含視頻內容的長度 hls_fragment 2s; # 總共能夠回看的事件 hls_playlist_length 60s; # 連續模式 hls_continuous on; # 對多餘的切片進行刪除 hls_cleanup on; # 嵌套模式 hls_nested on; } } }
# 建立配置目錄(在nginx.conf裏面指定了include) mkdir /usr/local/nginx/conf/conf.d vim /usr/local/nginx/conf/conf.d/video.conf server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/master.access.log main; location / { root /usr/local/nginx/html; index index.html index.htm; } #error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/local/nginx/html; } # http播放地址 location /live { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } alias /data/video/hls; expires -1; add_header Cache-Control no-cache; } # 統計 location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /usr/local/nginx-rtmp-module/; } }
# 開啓端口,1935推流,725統計 firewall-cmd --add-port=80/tcp --permanent firewall-cmd --add-port=1935/tcp --permanent firewall-cmd --reload
ffmpeg -re -i "e:\video\02.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://192.168.1.244:1935/hls/flv
ffmpeg -re -i "e:video02.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 340x640 -q 10 rtmp://192.168.1.244:1935/hls/flvhtml
ffmpeg -re -i "e:\video\02.mp4" -f flv "rtmp://192.168.1.244:1935/hls/flv"
下載最新客戶端: https://obsproject.com/nginx
自定義流媒體服務器
以ffmpeg推流的地址爲例:rtmp://192.168.1.244:1935/hls/flv
流URL:rtmp://192.168.1.244:1935/hls
流名稱:flvgit
http://192.168.1.244/stat
github
使用plyr.js
和hls.js
播放地址:http://192.168.1.244/live/flv...web
<!doctype html> <html lang="zh-cmn-Hans"> <head> <meta charset="UTF-8"/> <meta name="renderer" content="webkit"/> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/> <meta http-equiv="Cache-Control" content="no-siteapp"/> <title>直播</title> <link href="http://cdn.bootcss.com/plyr/3.3.9/plyr.css" rel="stylesheet"> </head> <body> <div class="player" style="width:640px;height:360px"> <video title="直播測試" poster="http://192.168.1.230:8004/static/img/index_p3.jpg" controls crossorigin></video> </div> <script src="http://cdn.bootcss.com/hls.js/0.9.1/hls.light.min.js"></script> <script src="http://cdn.bootcss.com/plyr/3.3.9/plyr.min.js"></script> <script> window.onload = function () { var hlsUrl = 'http://192.168.1.244/live/flv/index.m3u8'; var video = document.querySelector('video'); if (Hls.isSupported()) { var hls = new Hls({autoStartLoad: false}); hls.loadSource(hlsUrl); hls.attachMedia(video); } else { var nativeHLS = video.canPlayType('application/vnd.apple.mpegurl'); video.src = nativeHLS ? hlsUrl : fallbackUrl; } video.addEventListener('play', function () { hls.startLoad(); }, {once: true}); new Plyr(video); }; </script> </body> </html>