轻松搭建自己的直播平台

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'; 

            # 添加CORS头部
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Expose-Headers' 'Content-Length';
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }
        }

        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 官网

8. 浏览器中播放

浏览器中直播

### Nginx RTMP 推流配置教程 以下是基于 `nginx-rtmp-module` 的推流配置方法以及相关设置的说明: #### 1. 编译安装 Nginx 及模块 为了使 Nginx 支持 RTMP 协议,需通过指定选项重新编译 Nginx 并加入 `nginx-rtmp-module` 模块。以下是一个典型的编译命令[^1]: ```bash ./configure --user=nginx --group=nginx --with-http_ssl_module --prefix=/application/nginx --add-module=../nginx-rtmp-module ``` 完成编译后执行以下命令以构建和安装 Nginx: ```bash make && make install ``` #### 2. 配置 Nginx 文件 编辑 Nginx 主配置文件(通常位于 `/application/nginx/conf/nginx.conf`),添加如下 RTMP 相关配置示例[^3]: ```nginx worker_processes auto; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 8080; server_name localhost; location / { root html; index index.html index.htm; } } # HLS 输出目录 location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /tmp/; add_header Cache-Control no-cache; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, HEAD, OPTIONS'; add_header Access-Control-Allow-Headers 'Range'; add_header Access-Control-Expose-Headers 'Content-Length, Content-Range'; } } rtmp { server { listen 1935; # 默认RTMP端口 chunk_size 4096; application live { # 应用名称为live live on; # 开启直播功能 record off; # 不记录视频 hls on; # 启用HLS协议转换 hls_path /tmp/hls; # HLS输出路径 hls_fragment 5s; # 每个TS片段持续时间 hls_playlist_length 60s; # 播放列表总长度 } } } ``` 上述配置实现了以下几个核心功能: - **监听 RTMP 流**:通过 `listen 1935` 设置默认 RTMP 端口。 - **应用定义**:创建名为 `live` 的应用程序实例来接收实时流数据。 - **启用 HLS 转发**:将接收到的 RTMP 数据转化为 HTTP Live Streaming (HLS),存储于临时目录。 #### 3. 使用 OBS 进行推流测试 OBS Studio 是一款流行的开源推流工具,可以用来向 Nginx 发送 RTMP 流。具体步骤如下: - 打开 OBS,在“设置”->“串流”中选择自定义服务器模式; - 填写服务器地址为 `rtmp://<your_server_ip>/live`; - 输入流密钥(Stream Key)作为频道标识符,例如 `teststream`。 启动推流后,可以通过播放器访问生成的 HLS 地址验证效果,比如: ``` http://<your_server_ip>:8080/hls/teststream.m3u8 ``` #### 注意事项 确保防火墙允许必要的端口通信(如 TCP 1935 和 8080)。另外,实际部署时应考虑安全性措施,例如限制客户端 IP 访问范围或者加密传输通道。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

高建伟-joe

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

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

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

打赏作者

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

抵扣说明:

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

余额充值