使用Nginx+rtmp搭建流媒体服务器
背景介绍
本文使用的流媒体服务器的搭建是基于rtmp(Real Time Message Protocol)协议的,rtmp协议是应用层的协议,要依靠底层的传输层协议,好比tcp协议来保证信息传输的可靠性。最后提供了一个不错的测试方案。nginx
流媒体服务器依赖的服务:
1.nginx 服务器;
2.nginx服务器安装须要依赖的服务 OpenSSL、pcre、zlib、 c++、gcc等。c++服务器环境是Centos 8.1 64 位,用的vmvare,也可以使用现在流行的云服务器 。
安装nginx
首先须要注意的是虽然nginx可使用yum安装,可是yum安装使用的都是编译后的文件进行安装,而且后面所须要的rtmp模块通常在yum源里面是找不到的,因此要想将rtmp也安装进来,咱们选择源码安装,通过步骤配置,编译,安装。
本文是使用nginx源码,自行编译安装。首先是下载源码,咱们先用wget将nginx次新版本下载下来,而后将web模块源码从github上下载下来。
cd
mkdir rtmpsc #建立源码目录 后面的源码都放在这个目录
cd rtmpsc
wget http://nginx.org/download/nginx-1.20.1.tar.gz #从github服务器上将nginx的源代码下载下来
tar -zxvf nginx-1.20.1.tar.gz
yum -y install git #安装git
git clone https://github.com/arut/nginx-rtmp-module.git #将rtmp模块的源码下载下来
下载依赖模块源码pcre、OpenSSL、zlib 若是机器上已经安装了这些模块就不须要了
rpm -qa|grep 模块名字 #查询安装的模块的包信息
#下载OpenSSL源码包,用来做加密
wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1j.tar.gz
tar -zxvf openssl-1.1.1j.tar.gz
#用来做正则表达式,可以重写流路径的指向
wget https://ftp.pcre.org/pub/pcre/pcre-8.45.tar.gz #下载pcre源码包,2.0不行,更换了
tar -zxvf pcre-8.45.tar.gz
#做视频流的压缩
wget http://www.zlib.net/zlib-1.2.11.tar.gz #下载zlib包源码
tar -zxvf zlib-1.2.11.tar.gz
tar -zxvf 包名 #解压各个包源码
附一张目录结构图

找到nginx源码包中 configure,如下脚本就是基于configure来实现的,在本机上configure 命令在nginx的目录下,当前目录/home/atguigu/rtmpsc/nginx-1.20.1 就在这个目录下编写脚本vi build.sh 加入如下内容:
./configure --prefix=/home/root/rtmpsc/nginx \
--with-pcre=../pcre-8.45 \
--with-openssl=../openssl-1.1.1j \
--with-zlib=../zlib-1.2.11 \
--with-http_v2_module \
--with-http_flv_module \
--with-http_mp4_module \
--add-module=/home/root/rtmpsc/nginx-rtmp-module/
注意:
1.网上有些教程是pcre进行make makeinstall等操作后,然后把生成的目录作为with--xx后跟的路径,试了下不可行
2.这里pcre选择pcre会报错,使用pcre就行
保存后给文件赋予操做权限,再执行
chmod 777 build.sh #赋予权限
./build.sh #执行脚本
make
检查配置没有问题

make install
安装成功

现在的安装路径是nginx这个

配置下nginx的环境变量,可以全局使用nginx命令
#当前不是root用户,得取得管理权限
sudo vim /etc/profile
#在最下面加入
export NGINX_HOME="/home/root/rtmpsc/nginx/sbin" # do not add "bin" at the end of the path**
export PATH="$NGINX_HOME:$PATH"
#保存后(关于vim的编辑命令,自行百度)
#然后让profile生效
source /etc/profile
nginx -v

nginx安装完毕了。
现在需要做些变更:
1.云服务器控制台对相应的端口开放;
2.而且Linux机器对默认端口80开放(若是想修改默认端口自行百度),因为本机80端口被Apache 服务器占用了,因此将nginx默认端口改成8086
#找到nginx/conf/nginx.conf文件,然后加入rtmp的配置
#注意可能像live或者server前面,有来自html或者其他的奇怪空格,需要删除然后重新敲出来
rtmp {
server{
listen 1935;
chunk_size 4000;
application cctvf {
#rtmp推流请求路径 (切记路径错了会推不上流)
live on; #开启实时
#hls on #开启hls
##hls_path /home/atguigu/rtmpsc/cctvf; #rtmp推流请求路径,文件存放路径
##hls_fragment 5s; #每个TS文件包含5秒的视频内容
}}
}
#全文
#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;
chunk_size 4000;
application cctvf {
#rtmp推流请求路径 (切记路径错了会推不上流)
live on; #开启实时
#hls on #开启hls
##hls_path /home/atguigu/rtmpsc/cctvf; #rtmp推流请求路径,文件存放路径
##hls_fragment 5s; #每个TS文件包含5秒的视频内容
}}
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8086;
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
#保存后,让nginx生效
nginx -s reload
从这里下载ffmpeg for windows,FFmpeg Windows Builds - gyan.dev
下载编译好的可用的ffmpeg.exe,推流到服务器

.\ffmpeg.exe -re -i d:\01.自己动手搭建流媒体服务器.wmv -vcodec libx264 -acodec aac -f flv rtmp://192.168.10.201:1935/cctvf/mystream
#mystream 为自定义的流标识,随意起,不需要在nginx中标出来
这样就可以推流了

需要最下面一行,在不断的刷新上传数据,表示正常上传。
验证
可以使用vlc Downloads - VideoLAN,下载安装下,然后媒体->网络串流,输入链接,来进行验证

能正常播放音视频就算完成了。