nginx-rtmp(直播點播)配置

1. 安裝
a. 環境選擇,linux
[root@200 software]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)

[root@200 software]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[root@200 nginx-1.16]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.205  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::9435:12c3:18b9:534e  prefixlen 64  scopeid 0x20<link>


b. 下載 nginx
登錄官方網站 nginx.org 下載
下載stable版本,我們以 2019-08-13    nginx-1.16.1爲例
[root@200 software]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

c. 下載 nginx-rmp 模塊
登錄官方網站 https://github.com/arut/nginx-rtmp-module/releases 下載
下載release版本,我們以 1.2.1    nginx-1.16.1爲例
[root@200 software]# wget https://codeload.github.com/arut/nginx-rtmp-module/tar.gz/v1.2.1 -o nginx-rtmp-module-1.2.1.tar.gz

d. 編譯安裝
[root@200 software]# tar -xvzf nginx-1.16.1.tar.gz
[root@200 nginx-1.16.1]# tar -xvzf nginx-rtmp-module-1.2.1.tar.gz 
[root@200 nginx-1.16.1]# cd nginx-1.16.1
[root@200 nginx-1.16]# yum install pcre openssl
[root@200 nginx-1.16.1]#./configure --prefix=/usr/local/nginx-1.16  --add-module=../nginx-rtmp-module-1.2.1  --with-http_ssl_module
[root@200 nginx-1.16.1]# make -j 4 && make install

2. 配置

a. rtmp 配置
[root@200 nginx-1.16]# cd /usr/local/nginx-1.16/
[root@200 nginx-1.16]# cat conf/nginx.conf
修改 nginx 的配置,增加 rtmp 節點,注意是和 HTTP 平級,參考下面的內容

rtmp{
    #RTMP服務
    server{
        #//服務端口
        listen 1935;
        #//數據傳輸塊的大小
        chunk_size 4096;

        application vod {
            #//視頻文件存放位置
            play /root/video;
        }
        
        #設置頻道(房間),多路配置多個 application
        application live {
            #第一處添加的直播字段
            live on;
            #如果懶得推流,那可以用這個,香港衛視的直播推流
            #pull rtmp://live.hkstv.hk.lxdns.com/live/hks;
            record off;
        }
        
        application hls {
            #支持蘋果終端 HLS 協議
            live on;  
            hls on;  
            hls_path /tmp/hls;
        }
    }
}

http {
    include       mime.types;
    default_type  application/octet-stream;
     此處省略....

b. http 直播流狀態配置
需要增加  location /stat 和 location /stat.xsl
http{
     此處省略....     
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
 
        location /stat.xsl {
           root /usr/local/nginx-1.16/hls/;
        } 
[root@200 nginx-1.16]# cat conf/nginx.conf
[root@200 nginx-1.16]# mkdir /root/video
[root@200 nginx-1.16]# mkdir /usr/local/nginx-1.16/hls
[root@200 nginx-1.16]# cp /home/software/nginx-rtmp-module-1.2.1/stat.xsl /usr/local/nginx-1.16/hls

c. 服務啓動
[root@200 nginx-1.16]# /usr/local/nginx-1.16/sbin/nginx
[root@200 nginx-1.16]# /usr/local/nginx-1.16/sbin/nginx -s reload
[root@200 nginx-1.16]# /usr/local/nginx-1.16/sbin/nginx -s reopen

3. 驗證點播
a. 工具準備
windows 機器安裝 VLC media player
windows 機器安裝 OBS Studio

b. 拷貝 test1.mp4 到 linux 服務器 /root/video
[root@200 nginx-1.16]# ls /root/video/test1.mp4 
/root/video/test1.mp4

c. 打開 VLC media player 點播
打開網絡串流,播放網絡流,輸入地址
rtmp://192.168.1.205:1935/vod/test1.mp4


4. 驗證直播
a. 工具準備(已經安裝可忽略)
windows 機器安裝 VLC media player
windows 機器安裝 OBS Studio

b. 準備推直播(最好帶攝像頭)
打開 OBS Studio,設置 推流地址爲
rtmp://192.168.1.205:1935/live
選擇 直播流 數據源爲 視頻捕獲設備(攝像頭),從攝像頭獲取視頻流
點擊推送,開始推流

如果沒有攝像頭,也可以考慮使用 FFmpeg推流(此功能未驗證)
ffmpeg -re -i /root/video/test1.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://192.168.1.205:1935/live/
ffmpeg -re -i /root/video/test1.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://192.168.1.205:1935/hls/


c. 查看狀態
http://192.168.1.205/stat

RTMP    #clients    Video    Audio    In bytes    Out bytes    In bits/s    Out bits/s    State    Time
Accepted: 21    codec    bits/s    size    fps    codec    bits/s    freq    chan    307.85 MB    277.48 MB    2.83 Mb/s    1.79 Mb/s        15h 36m 14s
vod
vod streams    0
live
live streams    2
[EMPTY]    2    H264 High 3.1    2.66 Mb/s    1280x720    30    AAC LC    149 Kb/s    44100    2    27.4 MB    21.69 MB    2.81 Mb/s    1.38 Mb/s    active    1m 27s
Generated by nginx-rtmp-module 1.1.4, nginx 1.16.1, pid 7480, built Dec 12 2019 17:36:44 gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)

d. 打開 VLC media player 查看直播流
打開網絡串流,播放網絡流,輸入地址
rtmp://192.168.1.205:1935/live

瀏覽器端可以直接加載 video.js 播放,也可以考慮加載  VLC media player plugin 播放。

發佈了84 篇原創文章 · 獲贊 26 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章