使用 nginx 和 rtmp 模块 ,能够很容易地搭建一个视频直播和点播服务器出来。下面咱们来看一下具体实施步骤:nginx
有关 nginx 的编译和安装比较简单,这里就不介绍了,看参考文献。这里提示如下几点:web
(1) 安装好 nginx 后,配置文件在这里:浏览器
/usr/local/nginx/conf/nginx.conf
(2) 启动 nginx 的命令:服务器
$ sudo /usr/local/nginx/sbin/nginx -s stop $ sudo /usr/local/nginx/sbin/nginx
先看一下完整的 nginx 配置文件里有关视频点播和直播的配置:app
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 就只能够了,像下面这样:ide
application live { live on; record off; } application live2 { live on; record off; }
这样就能够经过下面的地址来推送直播流,其它观众端也能够经过下面的地址来访问直播流:this
rtmp://192.168.31.185/live/test
rtmp://192.168.31.185/live2/test
后面紧跟的 test 关键字,能够随便更换,只要你的推送流和访问流的地址同样就能够了。编码
rtmp 模块也能够直接支持 VOD 这种视频点播服务 ,只须要在配置文件里添加以下内容便可:url
# 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 这个视频文件,就能够经过下面的网址来访问了:code
http://myserver.com/vod//dir/file.flv
这样直接在浏览器里就能够经过网页观看视频。对于 mp4 文件,也能够实现 VOD 服务,不过须要的是采用 H.264 和 AAC 格式编码的 mp4 文件。
若是须要使用 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 端口,这个是在配置文件里写的。
在第二步里,除了能够直接在浏览器里打开网址来观看视频,还能够写一个网页,实现像优酷那样的视频点播业务。经过使用第三方的播放器,在网页里植入该播放器来实现这个功能,好比说使用 EasyPlayer播放器。
EasyPlayer是一款流媒体播放器系列项目, 支持RTSP、RTMP、HTTP、HLS、UDP、RTP、File等多种流媒体协议播放、 支持本地文件播放,支持本地抓拍、本地录像、播放旋转、多屏播放、 倍数播放等多种功能特性,核心基于ffmpeg,稳定、高效、可靠、可控。