目的:使Nginx支持Rtmp协议推流,并支持hls分发功能及FFMPEG转码多码率功能。
一、准备工作
模块:nginx-rtmp-module-master(支持rtmp协议)
下载地址:
http://nginx.org
https://github.com/arut/nginx-rtmp-module
1、安装依赖包:
#yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64 gcc-c++ wget xz perl-ExtUtils-MakeMaker package libcurl-devel unzip
2、安装Git工具:
#mkdir soft-source
#cd soft-source
#wget http://codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.xz
#xz -d git-latest.tar.xz
#tar xvf git-latest.tar
#cd git-2016-06-20/
#autoconf
#./configure
#make && make install
# git --version
git version 2.9.0
#cd ..
3、安装ffmpeg及其依赖包:
++++++++Yasm+++++++++++
#wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
#tar xzvf yasm-1.2.0.tar.gz
#cd yasm-1.2.0
#./configure
#make
#make install
#cd ..
++++++++x264+++++++++++
#git clone git://git.videolan.org/x264
#cd x264
#./configure --enable-shared
#make
#make install
#cd ..
++++++++LAME+++++++++++
#wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
#tar xzvf lame-3.99.5.tar.gz
#cd lame-3.99.5
#./configure --enable-nasm
#make
#make install
#cd ..
++++++++libogg+++++++++++
#wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
#tar xzvf libogg-1.3.0.tar.gz
#cd libogg-1.3.0
#./configure
#make
#make install
#cd ..
++++++++libvorbis+++++++++++
echo /usr/local/lib >> /etc/ld.so.conf; ldconfig
#wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
#tar xzvf libvorbis-1.3.3.tar.gz
#cd libvorbis-1.3.3
#./configure
#make
#make install
#cd ..
++++++++libvpx+++++++++++
git clone https://github.com/webmproject/libvpx
cd libvpx
./configure --enable-shared
make
make install
cd ..
++++++++FAAD2+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
#tar zxvf faad2-2.7.tar.gz
#cd faad2-2.7
#./configure
#make
#make install
#cd ..
++++++++FAAC+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
#tar zxvf faac-1.28.tar.gz
#cd faac-1.28
# sed -i 's@^char \*strcasestr@//char *strcasestr@' ./common/mp4v2/mpeg4ip.h
#./configure
#make
#make install
#cd ..
++++++++Xvid+++++++++++
#wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
#tar zxvf xvidcore-1.3.2.tar.gz
#cd xvidcore/build/generic
#./configure
#make
#make install
cd ../../../
++++++++ffmpeg+++++++++++
#git clone git://source.ffmpeg.org/ffmpeg
#cd ffmpeg
#./configure --prefix=/opt/ffmpeg/ --enable-version3 --enable-libvpx --enable-libfaac --enable-libmp3lame --enable-libvorbis --enable-libx264 --enable-libxvid --enable-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads
#make && make install
#cd ..
修改/etc/ld.so.conf如下:
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/ffmpeg/lib
#ldconfig
【说明】
动态装入器找到共享库要依靠两个文件 — /etc/ld.so.conf 和 /etc/ld.so.cache。
安装完成后,ffmpeg位于/opt/ffmpeg/bin目录下。
二、安装Nginx相关模块
1.环境准备
yum install pcre pcre-devel -y
yum install zlib zlib-devel -y
2.下载nginx及rtmp模块
#cd /root/soft-source/
#wget http://nginx.org/download/nginx-1.6.3.tar.gz
#tar xzvf nginx-1.6.3.tar.gz
#git clone git://github.com/arut/nginx-rtmp-module.git
3.编译nginx-rtmp
cd nginx-1.6.3
yum -y install libxml2-devel libxslt-devel
./configure --prefix=/usr/local/nginx --add-module=/root/soft-source/nginx-rtmp-module --with-http_stub_status_module --with-http_xslt_module
make
make install
安装完成后,nginx位于/usr/local/nginx/sbin目录下,配置文件nginx.conf在/usr/local/nginx/conf目录下
vi /etc/rc.d/init.d/nginx #编辑启动文件添加下面内容
#!/bin/bash
# Tengine Startup script# processname: nginx
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/usr/local/nginx/logs/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "tengine already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid } reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
保存退出
chmod 775 /etc/rc.d/init.d/nginx #赋予文件执行权限
chkconfig --level 012345 nginx on #设置开机启动
/etc/rc.d/init.d/nginx start
关闭软件防火墙:
chkconfig iptables off iptables
service iptables stop
打开网页http://localhost,如果显示Welcome表示安装下正确,如果没有显示,请查看一下nginx的日志。
++++++++测试RTMP+++++++++++
修改/usr/local/nginx/conf/nginx.conf的内容如下:
#debug
daemon off;
master_process off;
error_log ./error.log debug;
events{
worker_connections 1024;
}
rtmp{
server {
listen 1935;
chunk_size 4000;
#live
application myapp {
live on;
}
}
}
模拟测试:
用ffmpeg产生一个模拟直播源,向rtmp服务器推送
/opt/ffmpeg/bin/ffmpeg -i /root/fenxiang.mp4 -c:a libfaac -ar 44100 -ab 48k -c:v libx264 -f flv rtmp://10.10.6.237/myapp/test
注意,源文件必须是H.264+AAC编码的。
10.10.6.237是运行nginx的服务器IP
下载这个软件进行测试:
VLC media player
进行打开串流播放尝试。
++++++++测试HLS切片功能+++++++++++
修改/usr/local/nginx/conf/nginx.conf的内容如下:
#debug
daemon off;
master_process off;
error_log ./error.log debug;
events{
worker_connections 1024;
}
rtmp{
server {
listen 1935;
chunk_size 4000;
#live
application myapp {
live on;
hls on;
hls_path /tmp/hls;
hls_fragment 2s;
hls_playlist_length 6s;
}
}
}
#HTTP
http{
server {
listen 80;
#welcome
location / {
root html;
index index.html index.htm;
}
#hls
location /hls {
types {
application/vnd.apple.mpegusr m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
location /stat {
rtmp_stat all;
allow 127.0.0.1;
}
location /nclients
{
proxy_pass http://127.0.0.1/stat;
xslt_stylesheet /root/soft-source/nginx-rtmp-module/stat.xsl app='$arg_app' name='$arg_name';
add_header Refresh "3; $request_uri";
}
}
}
使用VLC或iPAD上的播放器进行查看 http://10.10.6.237/hls/test.m3u8。 打完,收工!
http://10.10.6.237/stat
根据直播频道访问以下地址:
http://10.10.6.237/nclients?app=myapp&name=channel1