轻松搭建自己的直播平台

1. 软件

  • Centos: 服务器
  • Nginx: 配置用于推流和拉流服务器
  • OBS Studio: 用于推流
  • VLC: 用于拉流

2. 效果图

推流和拉流:

stream

服务器状态监控:

stat

3. 搭建步骤

3.1 CentOS

本教程使用的操作系统版本 3.10.0-1160.105.1.el7.x86_64

安装基础工具

# 安装 git
yum install -y git
# 安装 gcc-c++ 编译器
yum install -y gcc-c++
# 安装 pcre pcre-devel
yum install -y pcre pcre-devel
# 安装 zlib zlib-devel
yum install -y zlib zlib-devel
# 安装 openssl openssl-devel
yum install -y openssl openssl-devel
# 如果 firewall 服务开启,请开放端口 80、1935
# 查看 firewall 是否开启
firewall-cmd --state
# 开放端口 80
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 开放端口 1935
firewall-cmd --zone=public --add-port=1935/tcp --permanent
# 重新加载配置文件
firewall-cmd --reload

3.2 nginx 相关配置

下载 nginx 源码,编译安装,并创建视频切片的存储位置

# 下载 nginx 源代码
wget https://nginx.org/download/nginx-1.24.0.tar.gz
# 解压 nginx 源代码
tar -zxvf nginx-1.24.0.tar.gz
# 切换目录到 nginx 源代码中
cd nginx-1.24.0
# 拉取 nginx-rtmp-module 代码
git clone https://github.com/arut/nginx-rtmp-module.git
# 设置配置项
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --add-module=./nginx-rtmp-module
# 编译并安装
make && make install
# 复制 stat.xsl 到 /usr/local/nginx/conf/ 用于服务状态监控
cp ./nginx-rtmp-module/stat.xsl /usr/local/nginx/conf/
# 创建视频切片的存放位置
mkdir /usr/local/nginx/html/hls

修改 nginx.conf 文件。

#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 4096;

        application live {
            live on;
            record off;

            # 添加 HLS 支持
            hls on;
            hls_path /usr/local/nginx/html/hls;
            hls_fragment 3;
            hls_playlist_length 60;
        }
    }
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            #注意stat.xsl文件的存放位置,支持相对路径和绝对路径。
            root /usr/local/nginx/conf;
        }

        location /live {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /usr/local/nginx/html/hls;   # 读取文件的位置,应和上面rtmp中的配置一样
            expires -1;
            add_header 'Cache-Control' 'no-cache'; 
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

启动 nginx 服务

# 检查配置文件是否有错误
/usr/local/nginx/sbin/nginx -t
# 启动 nginx 服务
/usr/local/nginx/sbin/nginx

4. OBS Studio 相关配置

OBS Studio 下载

OBS Studio 相关设置如图: 按步骤操作,即可开始推流

OBS

服务器配置项,请使用自己 nginx 服务的 ip ,否则无法推流。

5. VLC 相关配置

VLC 下载

VLC 播放器相关配置如图:

VLC

网络URL中,请使用自己 nginx 服务的 ip ,否则无法拉流。

6. 总结

推流地址: rtmp://192.168.3.59:1935/live
拉流地址: http://192.168.3.59/live/stream.m3u8
服务监控: http://192.168.3.59/stat

7. 参考文献

CentOS 官网
nginx 官网
OBS Studio 官网
VLC 官网

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高建伟-joe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值