使用nginx-rtmp搭建视频直播服务器
来源:互联网 发布:js 跳转url 编辑:程序博客网 时间:2024/05/16 07:49
auth:robertkun@163.com
date:2016-06-23
参考文档:
http://blog.csdn.net/redstarofsleep/article/details/45092127
http://blog.csdn.net/cjsafty/article/details/9108587
安装nginx前需要几样准备工作
第一个需要C++编译器,在ubuntu下直接安装g++即可
sudo apt-get install g++
然后,这个版本的nginx默认需要的是pcre和zlib和openssl,分别去这两个官方网站下载最新的版本,我下载的是pcre-8.32.tar.gz,zlib-1.2.8.tar.gz,openssl-1.0.2h.tar.gz
tar -xzvf pcre-8.32.tar.gztar -xzvf zlib-1.2.8.tar.gztar -xzvf openssl-1.0.2h.tar.gz
我将这些包解压在了/home/robert用户目录下,解压在哪里无所谓
/home/robert/pcre-8.32/home/robert/zlib-1.2.8/home/robert/openssl-1.0.2h
然后解压nginx, 解压在了/home/robert用户目录下
tar -xzvf nginx-1.10.1.tar.gz/home/robert/nginx
解压完后进入解压出来的目录
cd /home/robert/nginx
非常重要的rtmp模块:nginx-rtmp-module,使用github下载:
git clone https://github.com/arut/nginx-rtmp-module.git
然后执行configure, 这是比较重要的一步
sudo ./configure --prefix=/usr/local/nginx --add-module=/home/robert/nginx-rtmp-module --with-pcre=/home/robert/pcre-8.32 --with-openssl=/home/robert/openssl-1.0.2h --with-zlib=/home/robert/zlib-1.2.8
prefix指定nginx安装的目录,后面两个是前面的两个模块,如果不需要安装这两个模块可以用–without-http_rewrite_module和–without-http_gzip_module
然后就是执行make和make install,如果没有报错的话nginx就安装完成了
sudo makesudo make install
看一下/usr/local/nginx目录
robert@robert:/usr/local/nginx$ lltotal 48drwxr-xr-x 12 root root 4096 6月 22 11:21 ./drwxr-xr-x 11 root root 4096 6月 22 11:15 ../drwx------ 2 nobody root 4096 6月 22 11:21 client_body_temp/drwxr-xr-x 2 root root 4096 6月 22 17:43 conf/drwxr-xr-x 2 root root 4096 6月 22 11:19 conf_bak/ <手动备份>drwx------ 2 nobody root 4096 6月 22 11:21 fastcgi_temp/drwxr-xr-x 2 root root 4096 6月 22 17:32 html/drwxr-xr-x 2 root root 4096 6月 22 17:43 logs/lrwxrwxrwx 1 root root 16 6月 22 11:17 nginx -> /user/sbin/nginxdrwx------ 2 nobody root 4096 6月 22 11:21 proxy_temp/drwxr-xr-x 2 root root 4096 6月 22 11:15 sbin/drwx------ 2 nobody root 4096 6月 22 11:21 scgi_temp/drwx------ 2 nobody root 4096 6月 22 11:21 uwsgi_temp/
进入sbin目录,执行nginx
sudo ./nginx 启动nginx
查看nginx进程
robert@robert:/usr/local/nginx$ ps aux | grep nginxroot 78775 0.0 0.1 32696 4848 ? Ss 6月22 0:00 nginx: master process sbin/nginx -c conf/nginx_rtmp.confnobody 78776 0.0 0.2 33256 7648 ? S 6月22 0:00 nginx: worker processnobody 78777 0.0 0.2 32912 7244 ? S 6月22 0:00 nginx: cache manager processrobert 79477 0.0 0.0 21292 944 pts/9 S+ 09:54 0:00 grep --color=auto nginx
nginx的启动方法
nginx -c conf/nginx_rtmp.conf 使用指定的配置文件启动nginx -s reload 重启nginx服务nginx -s stop 停止nginx服务,强制快速结束nginx -s quit 停止nginx服务,温柔的结束
打开浏览器输入http://ip可看到nginx已经启动的画面
http://192.168.2.48
配置nginx-rtmp模块
修改nginx配置文件,nginx_rtmp.conf
robert@robert:/usr/local/nginx/conf$ cat nginx_rtmp.conf #user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { worker_connections 1024;}rtmp { server { listen 1935; application myapp { live on; } application hls { live on; hls on; hls_path /tmp/app; hls_fragment 5s; } }}http { server { listen 8080; location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /home/robert/nginx-rtmp-module; } location /control { rtmp_control all; } location /rtmp-publisher { root /home/robert/nginx-rtmp-module/test; } location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } alias /tmp/app; expires -1; } location / { root /home/robert/nginx-rtmp-module/test/rtmp-publisher; } }}
上面配置文件指定了两个application, 一个myapp,一个hls。
myapp来处理flash流,hls来处理m3u8文件,也就是苹果设备上的视频流。
配置好上面的nginx-rtmp-module路径后,重启nginx服务。
服务正常启动后,使用ffmpeg推一个本地视频流到nginx,注意这个本地视频的格式必须是
h264+aac格式的,否则不能正常播放。
向两个application推流:
ffmpeg -re -i s2.mp4 -vcodec copy -acodec copy -f flv rtmp://192.168.2.48/myapp/mystreamffmpeg -re -i s2.mp4 -vcodec copy -acodec copy -f flv rtmp://192.168.2.48/hls/mystream
hls格式的用于在ios上播放。
修改对应的播放页面
我们在上面配置文件指定了根目录/home/robert/nginx-rtmp-module/test/rtmp-publisher查看一下根目录的文件robert@robert:~/nginx-rtmp-module/test/rtmp-publisher$ lltotal 1664drwxrwxr-x 2 robert robert 4096 6月 22 17:27 ./drwxrwxr-x 4 robert robert 4096 6月 22 11:28 ../-rw-rw-r-- 1 robert robert 618 6月 22 17:18 player.html-rw-rw-r-- 1 robert robert 313 6月 22 17:00 playhls.html-rw-rw-r-- 1 robert robert 513 6月 22 17:05 publisher.html-rw-rw-r-- 1 robert robert 314 6月 22 10:46 README.md-rw-rw-r-- 1 robert robert 2830 6月 22 10:46 RtmpPlayerLight.mxml-rw-rw-r-- 1 robert robert 47222 6月 22 10:46 RtmpPlayerLight.swf-rw-rw-r-- 1 robert robert 2133 6月 22 10:46 RtmpPlayer.mxml-rw-rw-r-- 1 robert robert 47281 6月 22 10:46 RtmpPlayer.swf-rw-rw-r-- 1 robert robert 2741 6月 22 10:46 RtmpPublisher.mxml-rw-rw-r-- 1 robert robert 47496 6月 22 10:46 RtmpPublisher.swf-rw-rw-r-- 1 robert robert 1507328 6月 22 17:27 s2.mp4-rw-rw-r-- 1 robert robert 10220 6月 22 10:46 swfobject.js
我们修改一下player.html
<!DOCTYPE html><html><head> <title>RTMP Player</title> <script type="text/javascript" src="swfobject.js"></script> <script type="text/javascript"> var flashVars = { streamer: 'rtmp://192.168.2.48/myapp', file:'mystream' }; var params = {}; params.allowfullscreen = "false"; var attributes = {}; swfobject.embedSWF("RtmpPlayer.swf", "rtmp-publisher", "640", "480", "9.0.0", null, flashVars, params, attributes); </script></head><body> <div id="rtmp-publisher"> <p>Flash not installed</p> </div></body></html>
注意修改文件中这两个语句,这是我们使用ffmpeg推送的地址。
这里把localhost改成了机器ip, 用localhost有不出视频的情况。
streamer: 'rtmp://192.168.2.48/myapp',file:'mystream'
播放HLS文件
HLS文件对应的Html:
<!DOCTYPE html><html><head> <title>HLS Player</title></head><body><video height="270" width="480" controls> <source src="http://server-ip-address:8080/hls/mystream.m3u8" type="application/vnd.apple.mpegurl" /> <p class="warning">Your browser does not support HTML5 video.</p></video></body></html>
在ipad上播放地址:
http://server-ip-address:8080/hls/mystream.m3u8
打开对应的网址,可以看到实际的效果:
- 使用nginx-rtmp搭建视频直播服务器
- 使用nginx-rtmp搭建视频直播服务器
- 树莓派使用nginx+rtmp搭建直播服务器
- nginx 搭建rtmp直播服务器
- 使用 nginx 和 rtmp 插件搭建视频直播和点播服务器
- 使用Nginx+nginx-rtmp-module搭建直播服务器
- 实时监控、直播流、流媒体、视频网站开发方案流媒体服务器搭建及配置详解:使用nginx搭建rtmp直播、rtmp点播、,hls直播服务配置详解
- 实时监控、直播流、流媒体、视频网站开发方案流媒体服务器搭建及配置详解:使用nginx搭建rtmp直播、rtmp点播、,hls直播服务配置详解
- 实时监控、直播流、流媒体、视频网站开发方案流媒体服务器搭建及配置详解:使用nginx搭建rtmp直播、rtmp点播、,hls直播服务配置详解
- Nginx与Nginx-rtmp-module搭建RTMP视频直播和点播服务器
- 视频直播服务端环境搭建(Nginx+RTMP)
- 搭建rtmp直播流服务之1:使用nginx搭建rtmp直播流服务器(nginx-rtmp模块的安装以及rtmp直播流配置)
- Mac上搭建直播服务器Nginx+rtmp
- Mac上搭建nginx+rtmp直播服务器
- Mac上搭建直播服务器Nginx+rtmp
- nginx+rtmp直播流服务器搭建
- ffmpeg + nginx + rtmp 搭建本地直播服务器
- Mac上搭建直播服务器Nginx+rtmp
- Android基础--ActionBar的基本用法,自定义View,标题栏
- java网络编程:DatagramSocket类简单实现文件下载
- Android中的@SystemApi@hide等
- 蓝牙连接后,多个Activity应用
- 85-002-16 利用hibernate的级联功能实现(N+1)
- 使用nginx-rtmp搭建视频直播服务器
- 在Bluemix 使用OpenWhisk 开发Dark Vision应用系统
- Majority Element
- 从今天起开始学习Swift Hello,world
- spring mvc 框架搭建及详解
- lingochamp/FileDownloader
- post和get的区别
- iOS 上传图片(未压缩)
- webrtc2sip项目说明