使用 nginx 和 rtmp 模塊 ,可以很容易地搭建一個視頻直播和點播服務器出來。下面我們來看一下具體實施步驟:
1. 安裝 nginx 和 rtmp 模塊
有關 nginx 的編譯和安裝比較簡單,這里就不介紹了,看參考文獻。這里提示以下幾點:
(1) 安裝好 nginx 后,配置文件在這里:
/usr/local/nginx/conf/nginx.conf
(2) 啟動 nginx 的命令:
$ sudo /usr/local/nginx/sbin/nginx -s stop
$ 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;
}
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 這種視頻點播服務 ,只需要在配置文件里添加如下內容即可:
# video on demand
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 文件。
3. 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 端口,這個是在配置文件里寫的。
4. 網頁播放器播放
在第二步里,除了可以直接在瀏覽器里打開網址來觀看視頻,還可以寫一個網頁,實現像優酷那樣的視頻點播業務。通過使用第三方的播放器,在網頁里植入該播放器來實現這個功能,比如說使用 EasyPlayer播放器。
EasyPlayer是一款流媒體播放器系列項目, 支持RTSP、RTMP、HTTP、HLS、UDP、RTP、File等多種流媒體協議播放、 支持本地文件播放,支持本地抓拍、本地錄像、播放旋轉、多屏播放、 倍數播放等多種功能特性,核心基於ffmpeg,穩定、高效、可靠、可控。