在centos7上搭建nginx-rtmp直播服务器(一)基础篇

系统环境

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

安装开发工具

yum install epel-release -y
yum install build-essential -y
yum install gcc pcre pcre-devel openssl openssl-devel zlib zlib-devel -y

下载nginx及rtmp模块

mkdir rtmp
cd rtmp/
yum install wget -y
wget http://nginx.org/download/nginx-1.9.7.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
yum install unzip -y
tar -xvf nginx-1.9.7.tar.gz
unzip master.zip

编译安装nginx

cd nginx-1.9.7
./configure --add-module=/root/rtmp/nginx-rtmp-module-master
make
make install
cd /usr/local/nginx/sbin/
ls
./nginx

关闭防火墙

yum install net-tools -y
netstat -antup | grep 80
systemctl stop firewalld

编辑nginx配置文件

vi /usr/local/nginx/conf/nginx.conf

在nginx.conf末尾添加如下内容,注意要添加在括号里面

rtmp {
    server {
        listen 1935;  #服务端口,默认1935,避开重要端口即可
        application live {
        live on;
    }
        application hls_alic{
    live on;
    hls  on;
    hls_path /tmp/hls;
    }
}

重启nginx

nginx -s reload


创建软链接以直接使用nginx命令

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

安装ffmpeg推流

rpm --import  
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
yum install ffmpeg
ffmpeg -version

编辑html网页

/usr/local/nginx/html
ls
mv index.html index.html.bak
vi index.html
<html>
<head>
    <title>Live</title>
    <meta charset="utf-8">
    <link href="http://vjs.zencdn.net/5.5.3/video-js.css" rel="stylesheet">
    <!-- If you'd like to support IE8 -->
    <script src="http://vjs.zencdn.net/ie8/1.1.1/videojs-ie8.min.js"></script>
    <script src="http://vjs.zencdn.net/5.5.3/video.js"></script>
</head>
<body>
<video id="my-video" class="video-js" controls preload="auto" width="1280" height="720"
       poster="http://ppt.downhot.com/d/file/p/2014/08/12/9d92575b4962a981bd9af247ef142449.jpg" data-setup="{}">
    <source src="rtmp://10.10.10.16:1935/live/" type="rtmp/flv">
    </p>
</video>
</body>
</html>

把上面的IP地址换成nginx的IP地址

推流

ffmpeg -re -i 民兵葛二蛋-1集.mp4 -c copy -f flv "rtmp://localhost/live"

CTRL+C可以停止推流,网址后面的live是nginx.conf配置下rtmp模块定义的名称

 application live {

效果查看

1、用VLC播放器查看效果,如下:

VLC.jpg

2、网页查看:

由于现在浏览器都不支持flash插件了,所以需要去flash.cn网站下载安装一个flash中心的软件,里面集成了个网址浏览功能,可以用它输入IP地址查看效果。

flash-web.jpg

最后编辑于:2023/07/07作者: admin

发表评论