在Ubuntu下使用nginx-rtmp-module搭建直播系统
1. nginx-rtmp-module下载和安装
源码地址:https://github.com/arut/nginx-rtmp-module
使用git命令下载:git clone https://github.com/arut/nginx-rtmp-module.git
2.nginx源码下载及编译安装
nginx下载: wget http://nginx.org/download/nginx-1.16.0.tar.gz
最新版本可以查看http://nginx.org/en/download.html 得到。
在编译nginx之前,需要先下载编译安装好zlib、openssl、pure库
zlib:
wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar xf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && sudo make install
openssl:
wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz
tar xf openssl-1.0.2l.tar.gz
cd openssl-1.0.2l
./config
make && sudo make install
pure:
wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.42/pcre-8.42.tar.gz
tar xf pcre-8.42.tar.gz
cd pcre-8.42
./configure
make && sudo make install
安装之前可以先检查下自己的电脑是否已经安装过了。
然后就可以开始编译nginx了:
tar xf nginx-1.16.0.tar.gz
cd nginx-1.16.0/
./configure --prefix=/usr/local/nginx --add-module=…/nginx-rtmp-module --with-http_ssl_module --with-pcre=…/pcre-8.42 --with-openssl=…/openssl-1.0.2l --with-zlib=…/zlib-1.2.11
make && sudo make install
注意:执行./configure命令时的路径要对应。
执行完上述步骤后,如果没有错误,nginx就会已经部署在/user/local/nginx目录。
启动和关闭nginx:
sudo nginx
sudo nginx -s stop
3. 配置rtmp转发
在nginx.conf文件最后添加,以支持RTMP:
rtmp {
server {
listen 1935;
application live {
live on;
}
application vod {
play /tmp/video;
}
}
}
修改完成之后重启nginx:
sudo nginx -s reload
通过netstat -ltn命令我们可以看到1935端口正在监听.这便是我们配置的rtmp的服务端口。
FFmpeg推流
安装ffmpeg:
sudo apt-get install ffmpeg
推流:
ffmpeg -re -i input.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://127.0.0.1:9999/live/test
ffplay拉流:
ffplay rtmp://127.0.0.1:9999/live/test