使用 nginx 和 rtmp 模塊 ,能夠很容易地搭建一個視頻直播和點播服務器出來。javascript
首先,看一下最經典的參考文獻: How to set up your own private RTMP server using nginxcss
1. 安裝 nginx 和 rtmp 模塊html
有關 nginx 的編譯和安裝比較簡單,這裏就不介紹了,看參考文獻。這裏提示如下幾點:java
(1) 安裝好 nginx 後,配置文件在這裏:nginx
/usr/local/nginx/conf/nginx.conf
(2) 啓動 nginx 的命令:git
$ sudo /usr/local/nginx/sbin/nginx -s stop $ sudo /usr/local/nginx/sbin/nginx
2. 配置 nginx 視頻直播和點播服務github
先看一下完整的 nginx 配置文件裏有關視頻點播和直播的配置:web
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 就只能夠了,像下面這樣:shell
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. 網頁播放器插件
在第二步裏,除了能夠直接在瀏覽器裏打開網址來觀看視頻,還能夠寫一個網頁,實現像優酷那樣的視頻點播業務。經過使用第三方的播放器,在網頁裏植入該播放器來實現這個功能,好比說使用 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 就有關鍵幀了,便可進行拖動播放操做。