参考

https://blog.csdn.net/heng615975867/article/details/80519274

Nginx 直播服务部署(直播 + 分流 + 画面水印):https://juejin.im/post/6844904089361317901?utm_source=gold_browser_extension

搭建 RTMP 直播服务器「nginx-rtmp-module 的使用」:https://wjoan.github.io/2017/06/15/nginx-rtmp-module/

安装 Nginx 依赖库

Ubuntu 版本:https://blog.csdn.net/kingroc/article/details/50839994
RedHat 版本:https://blog.csdn.net/chaijunkun/article/details/7009183

Redhat/CentOS

  1. GCC编译器

    1
    yum install -y gcc
  2. G++编译器:C++来编译Nginx的http模块

    1
    yum install -y gcc-c++
  3. PCRE库:正则表达式

    1
    yum install -y pcre pcre-devel
  4. zlib库,对HTTP包的内容作gzip压缩

    1
    yum install -y zlib zlib-devel
  5. OpenSSL开发库
    如果服务器要支持在SSL协议上传输HTTP就需要OpenSSL

    1
    yum install -y openssl openssl-devel

Ubuntu版本

1
2
3
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev

下载编译 nginx

  1. 下载 nginx

    http://nginx.org/download/nginx-1.18.0.tar.gz

  2. 下载 nginx-rtmp-module

    https://github.com/arut/nginx-rtmp-module

    在 nginx-rtmp-module/test 中包含了一个简单 RTMP 的配置以及前端的测试页面

  3. 进入 nginx 目录,编译安装

    默认安装位置:/usr/local/nginx/

    1
    2
    3
    ./configure --add-module=../nginx-rtmp-module-master
    make
    sudo make install

修改配置

进入 nginx 的 conf 目录,打开 nginx.conf

配置 hls

http -> server 中添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}

location /stat.xsl {
root /path/to/nginx-rtmp-module/;
}

location /control {
rtmp_control all;
}

location /rtmp-publisher {
root /path/to/nginx-rtmp-module/test;
}

location / {
root /path/to/nginx-rtmp-module/test/www;
}

#root /path/to/nginx-rtmp-module/ 中的路径都要改为安装 nginx-rtmp-module 时的路径

配置 rtmp

最下面添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 在配置文件的最后

# my own application
rtmp {
server {
listen 1935;
chunk_size 4096;

#应用名称 live
application live {
live on;
record off;
}
# live_out 应用可以通过 pull 从其他地方获取 RTMP 视频流
# 前端浏览器需要使用flash
# 通过地址 rtmp://192.168.1.10:1112/live_out/mystream 获取视频流

#应用名称 live_out
application live_out {
live on;
record off;
# 现在通过ffmpeg推流到live应用,产生一个rtmp url
# 然后通过 pull 拉到live_out应用
# 如果有一个真实的rtmp流
# 那么只需要将url更改一下即可
pull rtmp://192.168.1.10:1112/live name=mystream static;
}
}
}

运行测试nginx

进入安装目录/usr/local/nginx,运行命令

1
./sbin/nginx

推流

ffmpeg 推流「mp4、rmvb、avi」

1
2
3
4
ffmpeg -re -i "D://3ffc3ec0b29dd05aea22377b0a5dd867.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://192.168.1.7:1935/hls/ggg


ffmpeg -re -i /usr/local/rstp/test.flv -c copy -f flv rtmp://192.168.1.10:1112/live/mystream

播放

rtmp://192.168.1.7:1935/hls/ggg

http://192.168.1.7/hls/ggg.m3u8