返回文章列表

Nginx RTMP 模組建置視訊點播串流服務

本文介紹如何使用 Nginx 與 RTMP 模組建置視訊點播(VOD)串流服務,涵蓋 RTMP 與 HTTP 的比較、伺服器建置步驟、組態檔案說明、測試方法以及網頁嵌入播放器的程式碼範例。此外,文章也提供常見問題的解決方案,例如編譯錯誤、自動啟動設定、組態錯誤以及 RTMP 到 RTMPS 的轉換等。

Web 開發 影音串流

在網路影音蓬勃發展的時代,如何有效率地提供視訊點播服務成為一項重要的技術挑戰。本文將引導讀者使用 Nginx 與其 RTMP 模組,一步步建構一個穩定的視訊點播串流伺服器。首先,我們會比較 RTMP 與 HTTP 兩種協定的優缺點,接著詳細說明在 Ubuntu 系統上安裝、組態 Nginx 與 RTMP 模組的過程,並提供一個可實際運作的組態範例。後續將會示範如何使用 VLC 播放器測試串流服務,以及如何利用 Flowplayer 將視訊嵌入網頁中,最後,我們將探討一些常見的設定問題和解決方案,幫助讀者快速排除故障。

使用nginx與RTMP模組實作視訊點播(VOD)串流

概述

本篇技術將詳細介紹如何使用nginx伺服器和RTMP模組來實作不同型別視訊檔案的串流播放。在開始之前,我們將比較RTMP與HTTP視訊傳輸的優缺點。

RTMP與HTTP的比較

RTMP協定簡介

即時訊息傳輸協定(RTMP)最初由Macromedia開發,用於向Adobe Flash應用程式串流點播和直播媒體。它是一種根據TCP的協定,能夠維持持續連線,被定義為「有狀態」的協定。這意味著從客戶端首次連線到斷開連線期間,串流伺服器會持續追蹤客戶端的行為。

HTTP與RTMP的優缺點比較

HTTP的優勢:
  1. 較少被網路中不同層級的防火牆阻擋;RTMP使用預設的1935埠,有時可能被防火牆封鎖,特別是在企業環境中。
  2. 更多的CDN支援(更容易實作映象和邊緣快取)。
  3. 更多自定義HTTP的專業知識。
RTMP的優勢:
  1. 支援多播傳輸。
  2. 安全性與IP保護,可使用TLS/SSL或RTMPE。
  3. 搜尋功能:對於長時間內容尤其有利,因為觀眾無需等待整個視訊檔案載入完成即可跳轉到指定位置,而HTTP傳輸的視訊則需要。
  4. 重新連線:若發生網路中斷,客戶端可以重新建立連線,而視訊可以從緩衝區繼續播放;當客戶端重新連線時,緩衝區將開始填充,以避免視訊或音訊流中斷。

建置串流伺服器

步驟一:安裝必要的軟體和依賴項

首先,在Ubuntu 12.04系統上以root身份登入,並安裝必要的軟體和依賴項:

apt-get install git gcc make
apt-get install libpcre3-dev
apt-get install libssl-dev

步驟二:下載並編譯nginx與RTMP模組

  1. 從Github下載nginx RTMP模組原始碼:
git clone https://github.com/arut/nginx-rtmp-module
  1. 下載nginx原始碼並解壓縮(此例中使用的是1.4.3版本):
wget http://nginx.org/download/nginx-1.4.3.tar.gz
tar -zxvf nginx-1.4.3.tar.gz
cd nginx-1.4.3
  1. 編譯nginx:
./configure --add-module=/root/nginx/nginx-rtmp-module/ --with-http_ssl_module --prefix=/usr/local/nginx-streaming
make
make install

步驟三:設定nginx組態檔案

  1. 編輯nginx組態檔案:
cd /usr/local/nginx-streaming/conf
mv nginx.conf nginx.conf.bkp
nano nginx.conf
  1. 貼上下列組態範例(來自RTMP模組的檔案):
worker_processes 1;
events {
    worker_connections 1024;
}

rtmp {
    server {
        listen 1935;
        chunk_size 4000;

        # 針對flv檔案的視訊點播
        application vod {
            play /var/flvs;
        }

        # 針對mp4檔案的視訊點播
        application vod2 {
            play /var/mp4s;
        }
    }
}

http {
    access_log /var/log/nginx/access-streaming.log;
    error_log /var/log/nginx/error-streaming.log;

    server {
        listen 8080;

        # 提供RTMP統計資料的XML介面
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /var/www/;
        }

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /tmp/app;
            expires 1;
        }
    }
}

步驟四:複製統計檔案並啟動nginx

  1. 複製stat.xsl到指定目錄:
mkdir /var/www
cp /root/nginx/nginx-rtmp-module/stat.xsl /var/www/
  1. 啟動nginx伺服器:
/usr/local/nginx-streaming/sbin/nginx

測試串流服務

  1. 將一個mp4檔案下載到/var/mp4s目錄下,例如sample.mp4
  2. 使用支援RTMP協定的播放器(如VLC)進行測試,輸入URL:
rtmp://<伺服器IP>:1935/vod2/sample.mp4

在網頁上嵌入串流視訊

若要在網頁上播放串流視訊,可以使用Flash播放器,如Flowplayer。以下是一個範例HTML程式碼:

<html>
<head>
    <script src="http://releases.flowplayer.org/js/flowplayer-3.2.12.min.js"></script>
</head>
<body>
    <div id="player" style="width:644px;height:276px;margin:0 auto;text-align:center">
        <img src="/path/to/background.png" height="260" width="489" />
    </div>
    <script>
        $f("player", "http://releases.flowplayer.org/swf/flowplayer-3.2.16.swf", {
            clip: {
                url: 'sample.mp4',
                scaling: 'fit',
                provider: 'hddn'
            },
            plugins: {
                hddn: {
                    url: "/path/to/flowplayer.rtmp-3.2.12.swf",
                    netConnectionUrl: 'rtmp://<伺服器IP>:1935/vod2'
                }
            },
            canvas: {
                backgroundGradient: 'none'
            }
        });
    </script>
</body>
</html>

程式碼詳解:

  1. 引入Flowplayer JavaScript元件:透過<script>標籤引入Flowplayer的JavaScript檔案。
  2. 設定播放器容器:使用<div>標籤定義播放器的容器,並設定其樣式。
  3. 初始化Flowplayer:使用$f()函式初始化Flowplayer,並指定SWF檔案的URL。
  4. 設定影片剪輯:在clip物件中設定影片的URL、縮放模式和提供者。
  5. 設定RTMP外掛:在plugins物件中設定RTMP外掛的URL和網路連線URL。
  6. 設定畫布屬性:設定畫布的背景漸層效果。

nginx-rtmp-module 串流伺服器常見問題與解決方案

在設定和使用 nginx-rtmp-module 的過程中,使用者可能會遇到各種問題。本篇文章將針對常見的問題進行深入分析,並提供具體的解決方案。

1. make 指令錯誤:make: *** No rule to make target 'build', needed by 'default'. Stop

內容解密:

此錯誤通常發生在使用 make 指令編譯 nginx 時。解決方法如下:

  1. 確認目錄正確性:確保目前工作目錄正確,應該位於 nginx 原始碼目錄中。
  2. 執行 configure 指令:在編譯之前,必須執行 ./configure 指令來產生 Makefile。
    ./configure --add-module=/path/to/nginx-rtmp-module
    
  3. 檢查依賴套件:確保所有必要的編譯工具和函式庫都已安裝。

2. 如何在系統啟動時自動啟動 nginx

內容解密:

要讓 nginx 在系統啟動時自動執行,可以將其新增至系統服務中。步驟如下:

  1. 建立 systemd 服務檔
    sudo nano /etc/systemd/system/nginx.service
    
    內容範例:
    [Unit]
    Description=The NGINX HTTP and reverse proxy server
    After=syslog.target network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin/nginx -t
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/bin/kill -s QUIT $MAINPID
    
    [Install]
    WantedBy=multi-user.target
    
  2. 重新載入 systemd 設定並啟用服務
    sudo systemctl daemon-reload
    sudo systemctl enable nginx.service
    

3. nginx 設定錯誤:unknown directive "worker_processes"

內容解密:

此錯誤表示 nginx 設定檔中存在無法識別的指令。檢查 nginx.conf 檔案:

  1. 語法錯誤:確保設定檔語法正確,特別是 worker_processes 指令後面沒有多餘的空格。
  2. 模組相容性:確認使用的 nginx 版本與 rtmp 模組版本相容。

4. 如何實作 RTMP 到 RTMPS 的轉換

內容解密:

nginx-rtmp-module 目前不直接支援 RTMPS。若要實作 RTMPS,可以使用反向代理伺服器來終止 SSL 連線。

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultTextAlignment center
skinparam rectangleBackgroundColor #F5F5F5
skinparam rectangleBorderColor #333333
skinparam arrowColor #333333

title 內容解密:

rectangle "RTMPS" as node1
rectangle "RTMP" as node2

node1 --> node2

@enduml

圖表翻譯: 此圖示展示了客戶端透過 RTMPS 連線到 nginx 反向代理伺服器,再由反向代理伺服器轉發到後端的 nginx RTMP 伺服器的流程。

5. 在直播斷線時自動播放預設影片

內容解密:

要實作在直播斷線時自動播放預設影片,可以使用 exec_staticexec 指令來執行外部程式處理。

rtmp {
    server {
        listen 1935;
        application live {
            live on;
            exec_static ffmpeg -re -stream_loop -1 -i /path/to/default/video.mp4 -c copy -f flv rtmp://localhost/live/stream;
        }
    }
}

內容解密:

上述設定使用 ffmpeg 在伺服器啟動時開始播放預設影片。當有直播推流時,nginx-rtmp-module 會優先轉發直播內容。