编译安装

安装各种包,包括opensslpcrezlibnginx-http-flv-module-master。其中nginx-http-flv-module-master是关键,其他为可选包。

#!/bin/bash

yum install -y gcc gcc-c++ autoconf wget vim
yum -y install wget gcc-c++ ncurses ncurses-devel cmake make perl bison openssl openssl-devel gcc* libxml2 libxml2-devel curl-devel libjpeg* libpng* freetype* unzip bzip2

dst_dir=/home/nginx

function build()
{
    dir_name=$1
    temp_file=$2
    url=$3
    cd ${dst_dir}
    if [ ! -e ${dir_name} ]; then
        wget --no-check-certificate  $url -O ${temp_file}
        tar -xf ${temp_file}
    fi
    cd ${dir_name}
    ./config --prefix=/usr/local/${dir_name}
    make install -j4
}


mkdir ${dst_dir}
build openssl-1.1.1l openssl-1.1.1l.tar.gz https://www.openssl.org/source/openssl-1.1.1l.tar.gz
build pcre-8.45 pcre-8.45.tar.bz2 https://nchc.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.bz2
build zlib-1.2.11 zlib-1.2.11.tar.gz http://www.zlib.net/zlib-1.2.11.tar.gz

cd ${dst_dir}
if [ ! -e nginx-http-flv-module-master ]; then
    wget https://github.com/winshining/nginx-http-flv-module/archive/refs/heads/master.zip -O nginx-http-flv-module-master.zip
    unzip nginx-http-flv-module-master.zip
fi

cd ${dst_dir}
if [ ! -e nginx-1.21.4 ]; then
    wget http://nginx.org/download/nginx-1.21.4.tar.gz
    tar -xzf nginx-1.21.4.tar.gz
fi

cd nginx-1.21.4
./configure --prefix=/usr/local/nginx-1.21.4 \
    --with-openssl=${dst_dir}/openssl-1.1.1l \
    --with-pcre=${dst_dir}/pcre-8.45 \
    --with-zlib=${dst_dir}/zlib-1.2.11 \
    --add-module=${dst_dir}/nginx-http-flv-module-master \
    --with-http_ssl_module
make install -j4

修改服务器配置

/usr/local/nginx-1.21.4/conf/nginx.conf文件的末尾添加:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        application hls {
            live on;
            hls on;
            hls_path /usr/local/nginx-1.21.4/html/hls;
            hls_fragment 5s;
        }
    }
}

在该文件的{http}-->{server}节中新增:

location /live {
    flv_live on;
    chunked_transfer_encoding  on;
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
}

location /hls {
    types {
        application/vnd.apple.mpegurl m3u8;
        video/mp2t ts;
    }
    root /usr/local/nginx-1.21.4/html/hls;
    add_header 'Cache-Control' 'no-cache';
}

播放媒体数据

ffmpeg -re -i test.mp4 -vcodec copy -codec copy -f flv rtmp://IP:1935/hls/test

其中,

  • test.mp4为视频文件,也可以是摄像头的rtsp地址。
  • url中的test是可以根据视频改变的。

观看媒体数据

  • 使用vlc
    打开串流,地址:
    • rtmp://IP:1935/hls/test
    • http://IP:80/live?port=1935&app=hls&stream=test
    • http://ip:80/hls/test.m3u8
  • 使用浏览器
    访问地址:
    http://IP:80/live?port=1935&app=hls&stream=test