利用nginx-rtmp搭建視頻點播、直播、HLS服務器

目錄

  1. 概述
  2. 編譯nginx
  3. 點播服務器的配置
  4. 直播服務器的配置
  5. 實時回看服務器的配置
  6. 問題

1. 概述

Nginx是一個非常出色的HTTP服務器,F(xiàn)Fmpeg是一個非常出色的音視頻解決方案。

兩者通過nginx的nginx-rtmp-module模塊組合可以搭建一個功能相對完善的流媒體服務器,可以支持RTMP和HLS流媒體協(xié)議。

2. 編譯nginx

(1) 系統(tǒng)和版本

  • ubuntu 16.04,這里使用的阿里云的ECS服務器上搭建的環(huán)境。
  • nginx-1.8.1

(2) nginx和nginx-rtmp-module下載

git clone https://github.com/arut/nginx-rtmp-module.git
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz

(3) 安裝nginx的依賴庫

sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev

(4) 配置并編譯nginx

進入到nginx-1.16.1安裝目錄, 使用nginx的默認配置,添加nginx的rtmp模塊。 add-module為下載的nginx-rtmp-module文件路徑。

cd nginx-1.16.1
./configure --add-module=../nginx-rtmp-module
make
sudo make install

(5) 運行測試nginx

進入安裝目錄/usr/local/nginx,運行命令./sbin/nginx

注意:以后所有的命令都在/usr/local/nginx目錄運行,也nginx配置文件的相對目錄。

打開瀏覽器在地址欄輸入云服務器的外網(wǎng)ip,如出現(xiàn)如下圖所顯示則證明nginx服務器搭建成功了。


image

如果訪問連接失敗,則按以下步驟排查以下:

  • 在服務器上執(zhí)行命令“curl localhost”,如果返回Welcome to nginx的內(nèi)容,則表示服務器上nginx已啟動成功。
  • 如果nginx已啟動成功,而外網(wǎng)訪問云服務器并沒有返回nginx的內(nèi)容,則需檢查云服務器的安全組配置,需要添加80端口的訪問權(quán)限。

3. 點播服務器的配置

(1) nginx 配置

通過上一步nginx服務器已經(jīng)搭建完成,然后我們就可以開啟一個視頻點播的服務了。

打開配置文件nginx.conf,默認的配置如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
                                                    

添加RTMP的配置:

worker_processes  1;
events {
   worker_connections  1024;
}
rtmp {                #RTMP服務
   server {
       listen 1935;  #//服務端口
       chunk_size 4096;   #//數(shù)據(jù)傳輸塊的大小
       application vod {
           play /opt/video; #//視頻文件存放位置。
       }
   }
}
http {
   include       mime.types;
   default_type  application/octet-stream;
   sendfile        on;
   keepalive_timeout  65;
   server {
       listen       80;
       server_name  localhost;
       location / {
           root   html;
           index  index.html index.htm;
       }
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
       }
   }
}

目錄/opt/video為存放視頻文件的位置,例如放置一個test.mp4文件。然后重啟一下nginx

sudo ./sbin/nginx -s reload

(2) 客戶端使用軟件進行測試

使用支持rtmp的播放器比如VLC播放器來驗證:

  • 媒體-》打開網(wǎng)絡串流-》網(wǎng)絡
  • 輸入rtmp://36.106.1.1/vod/test.mp4, 點擊播放即可以播放了。

4. 直播服務器的配置

(1) 服務器配置
接著在點播服務器配置文件的基礎(chǔ)之上添加直播服務器的配置。一共2個位置,第一處就是給RTMP服務添加一個application,這個名字可以任意起,也可以起多個名字,由于是直播這里把它命名為live,第二處就是添加兩個location字段,字段的內(nèi)容請直接看文件。

worker_processes  1;

events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application vod {
            play /opt/video;
        }

        application live{ #第一處添加的直播字段
            live on;
        }
    }
}

http {
    include      mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen      80;
        server_name  localhost;

        location /stat {    #第二處添加的location字段。
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl { #第二處添加的location字段。
           root /etc/rtmpServer/nginx-rtmp-module/;
        }

        location / {
            root  html;
            index  index.html index.htm;
        }

        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
            root  html;
        }
    }
}

添加完這兩處之后,重新啟動nginx打開瀏覽器查看http://36.106.1.1/stat,是否有如下圖顯示

nginx-rtmp.png

有沒有看到紅框框的live字樣呢?如果可以顯示出來,說明配置生效了。

(2) 使用OBS推送直播流到服務器

接下來我們推送一個節(jié)目到服務器

  • 安裝OBS軟件,安裝方式自行百度。
  • 在來源框添加一個本地文件。
    image
  • 點擊設置,配置節(jié)目的輸出流,文件路徑“rtmp://36.106.1.1/live/test”,如果發(fā)現(xiàn)直播比較卡,把重新播放輸出的分辨率調(diào)低一些。
    image
  • 點擊開始錄制。
  • 查看我們錄制的節(jié)目,服務器有沒有接收到呢?打開我的服務器地址“http://36.106.1.1/stat”查看一下
nginx-rtmp_live.png

5. 實時回看服務器的配置

如果直播服務能夠把節(jié)目錄制在本地,我們就可以直接進行回看先前的節(jié)目了。繼續(xù)看nginx的配置。

(1) 服務器配置

worker_processes  1;  

events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application vod {
            play /opt/video;
        }   

        application live {
            live on; 
            hls on; #這個參數(shù)把直播服務器改造成實時回放服務器。
            wait_key on; #對視頻切片進行保護,這樣就不會產(chǎn)生馬賽克了。
            hls_path /opt/video/hls; #切片視頻文件存放位置。
            hls_fragment 10s;     #每個視頻切片的時長。
            hls_playlist_length 60s;  #總共可以回看的事件,這里設置的是1分鐘。
            hls_continuous on; #連續(xù)模式。
            hls_cleanup on;    #對多余的切片進行刪除。
            hls_nested on;     #嵌套模式。
        }   
    }   
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /usr/local/nginx/nginx-rtmp-module/;
        }

        location /live {  #這里也是需要添加的字段。
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /opt/video/hls;
            expires -1;
            add_header Cache-Control no-cache;
        }

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

添加完成后需要重新啟動nginx

(2) 查看視頻文件是否真的錄制上沒有,已經(jīng)產(chǎn)生切片視頻文件了。其中還有一個index.m3u8。
(3) 播放視頻,這次可是http開頭的了,“http://36.106.1.1/live/test/index.m3u8”。
(4) 已經(jīng)可以播放了,如何回看呢?其實這個index.m3u8文件僅僅是目錄。想回看那個就播放那個.ts文件就可以了。

6. 問題

6.1 測試的時候居然把ip地址寫錯了。

參考Read more about debug log查看nginx的debug日志。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容