在 ubuntu server 14 安装流程
1.先下载安装 nginx 和 nginx-rtmp 编译依赖工具
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev
2. 创建一个nginx目录,并切换到nginx目录
mkdir ~/nginx cd ~/nginx
3. 下载 nginx 和 nginx-rtmp源码
wget http://nginx.org/download/nginx-1.9.9.tar.gz wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
4. 安装unzip工具,解压下载的安装包
sudo apt-get install unzip
5.解压 nginx 和 nginx-rtmp安装包
tar -zxvf nginx-1.9.9.tar.gz unzip master.zip
6. 切换到 nginx-目录
cd nginx-1.9.9
7.添加 nginx-rtmp 模板编译到 nginx
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master
8.编译安装
make sudo make install
9. 安装nginx init 脚本
sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx sudo chmod +x /etc/init.d/nginx sudo update-rc.d nginx defaults
10. 启动和停止nginx 服务,生成配置文件
sudo service nginx start sudo service nginx stop
11. 安装 FFmpeg
sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next sudo apt-get update sudo apt-get install ffmpeg
12. 配置 nginx-rtmp 服务器
打开 /usr/local/nginx/conf/nginx.conf
添加location
location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root ~/nginx/www; expires -1; add_header Cache-Control no-cache; }
文件末尾添加rtmp配置
rtmp { server { listen 1935; publish_time_fix on; application myapp { live on; #stream on live allow allow publish all; # control access privilege allow play all; # control access privilege } application hls { live on; hls on; #这个参数把直播服务器改造成实时回放服务器。 hls_path ~/nginx/www/hls; #切片视频文件存放位置。 wait_key on; #对视频切片进行保护,这样就不会产生马赛克了。 hls_fragment 10s; #每个视频切片的时长。 hls_playlist_length 60s; #总共可以回看的事件,这里设置的是1分钟。 hls_continuous on; #连续模式。 hls_cleanup on; #对多余的切片进行删除。 hls_nested on; #嵌套模式。 } } }
13. 保存上面配置文件,然后重新启动nginx服务
sudo service nginx restart
14. ffmpeg将rtsp转码为rtmp
(后面的rtmp在其他地方的访问地址为:rtmp://ip:1935/myapp/stream-name,可用VLC media player打开)
ffmpeg -i "rtsp://xxxx" -f flv -r 15 -s 1280x960 -an "rtmp://localhost:1935/myapp/stream-name"
15. ffmpeg将rtsp转码为m3u8:
(m3u8的访问地址为:http://ip:port/hls/stream-name.m3u8,port为nginx的访问端口号)
ffmpeg -i "rtsp://xxxx" -strict -2 -c:v libx264 -c:a aac -f hls ~/nginx/www/hls/stream-name.m3u8
16. html中使用video.js访问流媒体服务器:
国人处理好的videojs包为:http://pan.baidu.com/s/1kVuU3PX,此包已经支持IE8的视频播放。
<!DOCTYPE html> <html> <head> <title>Video.js | HTML5 Video Player</title> <!-- Chang URLs to wherever Video.js files will be hosted --> <link href="video-js.css" rel="stylesheet" type="text/css"> <!-- video.js must be in the <head> for older IEs to work. --> <script src="video.js"></script> <!-- Unless using the CDN hosted version, update the URL to the Flash SWF --> <script> videojs.options.flash.swf = "video-js.swf"; </script> </head> <body> <video id="example_video_1" class="video-js vjs-default-skin" controls preload="none" width="640" height="264" poster="oceans-clip.png" data-setup="{}"> <source src="rtmp://e.5iwf.cn:1935/myapp/video5" type="rtmp/flv"> <!-- 如果上面的rtmp流无法播放,就播放hls流 --> <source src="http://e.5iwf.cn:9999/hls/video5.m3u8" type='application/x-mpegURL'> </video> </body> </html>