流媒體服務器(Nginx+rtmp)

使用 nginx 和 rtmp 模塊 ,可以很容易地搭建一個視頻直播和點播服務器出來。

  1. 安裝 nginx 和 rtmp 模塊

有關 nginx 的編譯和安裝比較簡單,這裏就不介紹了,看參考文獻。這裏提示以下幾點:

(1) 安裝好 nginx 後,配置文件在這裏:

/usr/local/nginx/conf/nginx.conf

(2) 啓動 nginx 的命令:

sudo/usr/local/nginx/sbin/nginxsstop sudo
/usr/local/nginx/sbin/nginx

2 配置 nginx 視頻直播和點播服務

先看一下完整的 nginx 配置文件裏有關視頻點播和直播的配置:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        application live {
            live on;
            record off;
        }
        application live2 {
            live on;
            record off;
        }
        # video on demand
        application vod {
            play /var/flvs;
        }
        application vod_http {
            play http://192.168.31.185/vod;//我的是自己本地測試,用自己的IP
        }
        application hls {
            live on;
            hls on;
            hls_path /tmp/hls;
        }
    }
}
HTTP can be used for accessing RTMP stats
http {
    server {
    listen      8080;
    # This URL provides RTMP statistics in XML
    location /stat {
        rtmp_stat all;
        # Use this stylesheet to view XML as web page
        # in browser
        rtmp_stat_stylesheet stat.xsl;
    }
    location /stat.xsl {
        # XML stylesheet to view RTMP stats.
        # Copy stat.xsl wherever you want
        # and put the full directory path here
        root /path/to/stat.xsl/;
    }
    location /hls {
        # Serve HLS fragments
        types {
        application/vnd.apple.mpegurl m3u8;
        video/mp2t ts;
        }
        root /tmp;
        add_header Cache-Control no-cache;
    }
    location /dash {
        # Serve DASH fragments
        root /tmp;
        add_header Cache-Control no-cache;
    }
    }
}

現在來解釋一下里面各行代碼的含義。對於視頻直播服務,如果需要支持多路流輸入的話,很簡單,在 nginx 配置文件裏多配幾個 Application 就只可以了,像下面這樣:

application live {
    live on;
    record off;
}
application live2 {
    live on;
    record off;
}

這樣就可以通過下面的地址來推送直播流,其它觀衆端也可以通過下面的地址來訪問直播流:

rtmp://192.168.31.185/live/test
rtmp://192.168.31.185/live2/test

後面緊跟的 test 關鍵字,可以隨便更換,只要你的推送流和訪問流的地址一樣就可以了。

rtmp 模塊也可以直接支持 VOD 這種視頻點播服務 ,只需要在配置文件裏添加如下內容即可:

application vod {
    play /var/flvs;
}

application vod_http {
    play http://myserver.com/vod;
}

然後把一個 mp4 或是 flv 文件扔到 /var/flvs 目錄下,對於 /var/flvs/dir/file.flv 這個視頻文件,就可以通過下面的網址來訪問了:

  http://myserver.com/vod//dir/file.flv

這樣直接在瀏覽器裏就可以通過網頁觀看視頻。對於 mp4 文件,也可以實現 VOD 服務,不過需要的是採用 H.264 和 AAC 格式編碼的 mp4 文件。

  1. HLS 直播流的配置

如果需要使用 HLS 來視頻直播,可以直接像配置文件那樣,寫上下面這一段:

application hls {
        live on;
        hls on;
        hls_path /tmp/hls;
}

同時把後面有關 http 訪問的內容寫上:

//HTTP can be used for accessing RTMP stats
http {
    server {
        listen    8080;
        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;
            # Use this stylesheet to view XML as web page
            # in browser
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            # XML stylesheet to view RTMP stats.
            # Copy stat.xsl wherever you want
            # and put the full directory path here
            root /path/to/stat.xsl/;
        }
        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        }
        location /dash {
            # Serve DASH fragments
            root /tmp;
            add_header Cache-Control no-cache;
        }
    }
}

配好以後,推流可以使用下面的地址:

rtmp://192.168.31.185/hls/movie

movie 關鍵字可以任何替換。對於觀衆端來說,可以有幾種播放方式:

(1) 用 rtmp:

rtmp://192.168.31.185/hls/movie

(2) 用 hls 播放:

http://192.168.31.185:8080/hls/movie.m3u8

這樣就可以看到主播端推出來的流。注意,如果使用 http 方式,則是監聽的 8080 端口,這個是在配置文件裏寫的。

  1. 網頁播放器插件

在第二步裏,除了可以直接在瀏覽器裏打開網址來觀看視頻,還可以寫一個網頁,實現像優酷那樣的視頻點播業務。通過使用第三方的播放器,在網頁裏植入該播放器來實現這個功能,比如說使用 JWPlayer 播放器。

下載 JWPlayer 播放器,放到 nginx 網頁服務的根目錄,比如說,在我這兒是 /usr/local/nginx/html 這個目錄,把 JWPlayer 解壓後放到這個目錄裏,然後在這個目錄裏新建一個 play.html 的文本文件,再寫入以下內容:

<html>
  <head>
    <script src="/jwplayer/jwplayer.js"></script>
  </head>

<body>
  <div id='my-video'></div>
  <script type='text/javascript'>
    jwplayer('my-video').setup({
      file:'http://192.168.31.185/vod/North.mp4',
      fallback:'false'
    });
  </script>
</body>
</html>

對於 flv 文件,可以採用如下的形式來實現網頁播放:

<html>
  <head>
    <script src="/jwplayer/jwplayer.js"></script>
  </head>

<body>
  <div id='my-video'></div>
  <script type='text/javascript'>
    jwplayer('my-video').setup({
      file:'http://192.168.31.185/vod/1.flv',
      width:'50%',
      aspectratio:'3:2',
      fallback:'false',
      primary:'flash'
    });
  </script>
</body>
</html>

這樣,當你打開 http://192.168.31.185/play.html 這個網頁時,就能看到 html 裏指定的視頻文件。

另外,如果有一些 flv 文件沒有辦法拖動播放,那是因爲沒有給 flv 文件添加“關鍵幀”,可以用工具“yamdi” 和“flvtool”來給 flv 生成關鍵幀,以 yamdi 爲例,下載解壓後從 shell 執行:

yamdi –i src.flv –o dst.flv
意思是將 src.flv 添加關鍵幀,並輸出爲 dst.flv 。這樣 flv 就有關鍵幀了,即可進行拖動播放操作。

轉載自:http://www.tuicool.com/articles/iauQNr

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章