0%

Nginx+Rtmp+FFmpeg搭建等流媒体服务器

需要安装的工具

yum install -y wget unzip gcc gcc-c++ perl

安装ffmpeg

yum install -y ffmpeg

创建临时文件夹

  • 切换到home目录
    cd /home

  • 新建文件夹
    mkdir build

  • 进入build文件夹
    cd build

下载并解压pcre

  • 下载pcre
    wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gz

  • 解压pcre
    tar -zxvf pcre-8.41.tar.gz

下载并解压zlib

  • 下载zlib
    wget http://www.zlib.net/zlib-1.2.11.tar.gz

  • 解压zlib
    tar -zxvf zlib-1.2.11.tar.gz

下载并安装openssl

  • 下载openssl
    wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz

  • 解压openssl
    tar -zxvf openssl-1.1.1a.tar.gz

  • 切换到openssl目录里
    cd openssl-1.1.1a

  • 生成配置文件 默认配置
    ./config --prefix=/usr/local --openssldir=/usr/local/openssl

  • 编译程序
    make

  • 安装程序
    make install

  • 切换回build目录
    cd ..

下载并解压nginx-rtmp-model

  • 下载nginx-rtmp-module
    wget https://codeload.github.com/arut/nginx-rtmp-module/zip/master

  • 解压nginx-rtmp-module
    unzip -o master.zip

下载并安装nginx

  • 下载nginx
    wget http://nginx.org/download/nginx-1.14.0.tar.gz

  • 解压nginx
    tar -zxvf nginx-1.14.0.tar.gz

  • 切换到nginx目录
    cd nginx-1.14.0

  • 生成配置文件
    ./configure --with-http_ssl_module --with-pcre=../pcre-8.41 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.1.1a --add-module=../nginx-rtmp-module-master

  • 编译程序
    make

  • 安装程序
    make install

修改nginx配置

  • 备份nginx.conf
    cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak

  • 修改nginx.conf
    vi /usr/local/nginx/conf/nginx.conf

  • events后添加

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    rtmp {
    server {

    listen 1935;
    chunk_size 4000;

    application myapp {
    live on;
    record off;
    }

    application hls {
    live on;
    hls on;
    hls_path /tmp/hls;
    }
    }
    }
  • http的service中添加

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #数据格式设置
    charset utf-8;
    #配置hls
    location /hls {
    types {
    application/vnd.apple.mpegurl m3u8;
    video/mp2t ts;
    }
    root /tmp;
    add_header Cache-Control no-cache;
    }

把nginx添加进systemctl

  • 添加并编辑nginx.service文件
    vim /usr/lib/systemd/system/nginx.service

  • 在文件中填入下面文字并保存

    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
    [Unit]

    Description=nginx - high performance web server

    Documentation=http://nginx.org/en/docs/

    After=network.target remote-fs.target nss-lookup.target

    [Service]
    Type=forking

    PIDFile=/usr/local/nginx/logs/nginx.pid

    ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

    ExecReload=/usr/local/nginx/sbin/nginx -s reload

    ExecStop=/usr/local/nginx/sbin/nginx -s stop

    ExecQuit=/usr/local/nginx/sbin/nginx -s quit

    PrivateTmp=true

    [Install]

    WantedBy=multi-user.target
  • 重载systemctl命令
    systemctl daemon-reload

添加hls视频播放网页

  • 添加并编辑文件
    vi /usr/local/nginx/html/video.html

  • 在文件中填入下面文字并保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<script src="https://open.ys7.com/sdk/js/1.3/ezuikit.js"></script>
<video id="myPlayer" poster="" controls playsInline webkit-playsinline autoplay>
<source src="http://服务器ip/test.m3u8" type="application/x-mpegURL" />
</video>
<script>
//初始化播放器
var player = new EZUIPlayer('myPlayer');
player.on('error', function(error) {
playSuccess=0;
console.log('error');
});
player.on('play', function() {
playSuccess=1;
console.log('play');
});
player.on('pause', function() {
playSuccess=0;
console.log('pause');
});

</script>
  • 启动nginx服务
    systemctl start nginx

  • 开启ffmpeg转码
    ffmpeg -f rtsp -rtsp_transport tcp -i "rtsp:填入rtsp地址" -strict -2 -c:v libx264 -vsync 2 -c:a aac -f hls -hls_time 5 -hls_list_size 2 -hls_wrap 2 /usr/local/nginx/html/test.m3u8

  • 在浏览器中打开网址,即可查看转码后视频。服务器要开启80端口
    服务器ip/video.html

添加rtmp视频播放网页

  • 添加并编辑文件
    vi /usr/local/nginx/html/rtmp.html

  • 在文件中填入下面文字并保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<script src="https://open.ys7.com/sdk/js/1.3/ezuikit.js"></script>

<video id="myPlayer" poster="" controls playsInline webkit-playsinline autoplay>
<source src="rtmp://服务器ip:1935/myapp/test" type="" />
</video>

<script>
//初始化播放器
var player = new EZUIPlayer('myPlayer');
player.on('error', function(error) {
playSuccess=0;
console.log('error');
});
player.on('play', function() {
playSuccess=1;
console.log('play');
});
player.on('pause', function() {
playSuccess=0;
console.log('pause');
});

</script>
  • 启动nginx服务
    systemctl start nginx

  • 开启ffmpeg转码
    ffmpeg -f rtsp -rtsp_transport tcp -i "rtsp:填入rtsp地址" -strict -2 -c:v libx264 -vsync 2 -c:a aac -f flv -s 640x360 -q 10 rtmp://localhost:1935/myapp/test

  • 在浏览器中打开网址,即可查看转码后视频。服务器要开启80、1935端口
    服务器ip/rtml.html