Nginx搭建rtmp流媒体服务器
一、Nginx+rtmp安装配置
1、安装依赖包(CentOS 6.5)
# yum –y gcc make# yum -y install pcre-devel openssl-devel
下载模块
#mkdir -p /path/to#cd /path/to#yum install git#git clone https://github.com/arut/nginx-rtmp-module.git#wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
2、编译安装Nginx
# tar -xf nginx-1.12.1.tar.gz //解压包# cd nginx-1.12.1 //进入解压文件# groupadd -r nginx //创建组# useradd -g nginx -s /sbin/nologin -M nginx //创建所属组是nginx的用户# ./configure \ //运行脚本控制配置 --user=nginx \ --group=nginx \ --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-http_flv_module \--add-module=/path/to/nginx_mod_h264_streaming-2.2.7 \--add-module=/path/to/nginx-rtmp-module
3、Make报错:
/path/to/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c:158:错误:‘ngx_http_request_t’没有名为‘zero_in_uri’的成员
把158到161行注释掉
157 /* TODO: Win32 */
158 //if (r->zero_in_uri)
159 //{
160 // return NGX_DECLINED;
161 //}
/path/to/nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:377:16:error: variable ‘stream_priority’ set but not used [-Werror=unused-but-set-variable]
unsigned int stream_priority;^
进入对应的文件,把377行屏蔽注释
4、Make install
5、编写启动脚本:
# vim /etc/rc.d/init.d/nginx //如文档所示# chmod +x /etc/rc.d/init.d/nginx //添加可执行权限# chkconfig --add nginx //添加到服务列表# chkconfig nginx on //设置开机自启动
二、安装ffmpeg
安装编译x264,需要yasm编译。
2.动态链接库
vi /etc/ld.so.conf加入:/usr/local/ffmpeg/lib执行ldconfig三、点播配置:
1、建立媒体文件夹:
#mkdir /usr/local/nginx/vod/flvs
2、准备媒体文件:
把媒体文件 a.flv 复制到 /usr/local/nginx/vod/flvs目录下。
3、在nginx中配置http
#配置事件工作线程数
#events { worker_connections 30; #默认为(1024),windows默认为64,所以需要修改此值小于64}#配置httphttp { include mime.types; default_type application/octet-stream; #配置访问web页 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #配置rtmp状态页 server { listen 8080; location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /usr/local/nginx/nginx-rtmp-module/; #在nginx-rtmp-module源码根目录 } }}#rtmp点播配置rtmp { server { listen 1935; chunk_size 4000; application vod { play /usr/local/nginx/vod/flvs; #点播媒体文件存放目录 } }}
4、 启动nginx
5、测试
使用大牛直播打开
rtmp://192.168.32.33/vod/a.flv
(可用大牛直播播放测试
四、直播配置:
1、ffmpeg推送视屏文件
1、ffmpeg 推送视频文件,音视频的编码格式只能为H264、AAC
ffmpeg -re -i 视频源 -vcodec copy -acodec copy -f flv 输出地址参考
2、网络摄像机 rtsp流转推rtmp直播(不过有丢包情况,还请大家多给指点)
ffmpeg -i rtsp://ip address/original -crf 30 -preset ultrafast -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -vcodec libx264 -r 25 -b:v 500k -s 640*480 -f flv rtmp://ip address/live/stram
2、修改Nginx.conf
#rtmp直播配置rtmp { server { listen 1935; chunk_size 4000; application live { live on; } }}
3、ffmpeg推送视屏文件
#ffmpeg -re -i rtmp://live.hkstv.hk.lxdns.com/live/hks -vcodec copy -acodec copy -f flv rtmp://192.168.32.33:1935/live/test
4、使用大牛直播打开输出视屏
rtmp://192.168.32.33:1935/live/test live是application,test是直播缓存流文件
五、使用flv播放:
直接服务器拿资源:
1、准备网页
下载后,解压到:/usr/local/nginx/html/
建立测试页面test.html,也放到上面目录下。
#jwplayer('my-video').setup({ file:'rtmp://192.168.32.33/vod/a.flv', width:'50%', aspectratio:'3:2', fallback:'false', primary:'flash' });
2、访问链接
用ffmpeg产生一个模拟直播源,向rtmp服务器推送:
建立测试页面test.html,也放到上面目录下。
jwplayer('my-video').setup({ file:'rtmp://192.168.32.33/live/test', #live是applicatioin,test是直播缓存流文件 width:'50%', aspectratio:'3:2', fallback:'false', primary:'flash' });
l 启动服务
nginx
l 推流
用ffmpeg产生一个模拟直播源,向rtmp服务器推送
ffmpeg -re –i rtmp://live.hkstv.hk.lxdns.com/live/hks -strict -2 -c:v libx264 -c:a aac -f flv rtmp://192.168.32.33/live/test
注意,源文件必须是H.264+AAC编码的。
192.168.32.33是运行nginx的服务器IP,l
ive是applicatioin,
test是直播缓存流文件,需要与配置文件中的直播缓存文件名一样。
本文转载自