• 使用Nginx搭建流媒体


    使用Nginx搭建流媒体

    参考地址

    https://blog.csdn.net/u013416034/article/details/130649958
    https://www.nxrte.com/jishu/9697.html
    https://devpress.csdn.net/cicd/62ee4cce7e66823466182187.html
    
    • 1
    • 2
    • 3

    安装 ffmpeg

    1、下载安装ffmpeg
    https://johnvansickle.com/ffmpeg/
    https://www.johnvansickle.com/ffmpeg/old-releases/ffmpeg-5.1.1-amd64-static.tar.xz
    
    • 1
    • 2
    2、安装依赖
    yum -y install bzip2 yasm
    
    • 1
    3、编译安装 ffmpeg
    xz -d ffmpeg-5.1.1-amd64-static.tar.xz
    tar xf ffmpeg-5.1.1-amd64-static.tar -C /usr/local/src/
    
    • 1
    • 2
    4、创建全局链接
    ln -sv /usr/local/src/ffmpeg-5.1.1-amd64-static/ffmpeg /usr/local/bin/ffmpeg
    ln -sv /usr/local/src/ffmpeg-5.1.1-amd64-static/ffprobe /usr/local/bin/ffprobe
    
    • 1
    • 2
    5、验证
    ffmpeg -h
    
    • 1

    安装nginx

    1、下载依赖包
    yum -y install gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel libtool git autoconf automake libxml2-devel  zlib-devel libgo-devel openssl-devel
    
    
    • 1
    • 2
    2、 解压安装nginx
    # 解压
    tar xf nginx-1.24.0.tar.gz -C /usr/local/src/
    
    # 配置构建所需选项
    ./configure  \
    --sbin-path=/usr/sbin/nginx \
    --lock-path=/var/run/nginx.lock \
    --conf-path=/etc/nginx/nginx.conf \
    --with-pcre \
    --with-http_auth_request_module \
    --with-http_degradation_module \
    --with-http_geoip_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_image_filter_module \
    --with-http_mp4_module \
    --with-http_perl_module \
    --with-http_realip_module \
    --with-http_secure_link_module \
    --with-http_slice_module \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_v2_module \
    --with-stream_ssl_module \
    --with-stream \
    --with-threads \
    --prefix=/etc/nginx
    
    
    # 编译
    make
    make install
    
    # 验证
     nginx -V
    
    • 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
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    3、配置启动脚本
    $ vim  /etc/systemd/system/nginx.service
    [Unit]
    Description=nginx - high performance web server
    Documentation=https://nginx.org/en/docs/
    After=network.target remote-fs.target nss-lookup.target
    Wants=network-online.target
    [Service]
    Type=forking
    PIDFile=/etc/nginx/logs/nginx.pid
    ExecStartPre=/usr/bin/rm -f /etc/nginx/logs/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
    ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
    ExecReload=/bin/kill -s HUP $MAINPID
    KillSignal=SIGQUIT
    TimeoutStopSec=5
    KillMode=process
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    4、修改 nginx.conf 文件

    在原有的nginx.conf 文件中 server 里面增加如下配置

    location /live {
            types {
                application/vnd.apple.mpegurl m3u8;
             }
            add_header Access-Control-Allow-Origin *;
            alias /etc/nginx/html/hls;
            expires -1;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    5、 启动 nginx
    
    systemctl daemon-reload
    systemctl start nginx
    systemctl enable nginx
    systemctl status nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    6、上传视频测试验证
    # 新建文件夹
    mkdir /etc/nginx/html/hls
    chmod 777 /etc/nginx/html/hls
    
    # 上传视频文件到 hls 目录下面使用 ffmpeg 进行分割
    ffmpeg -i 022309.mp4 -c copy -map 0 -f segment -segment_time 10 -segment_list 022309.m3u8 -segment_format mpegts 022309_%03d.ts
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    参数说明

    • 022309.mp4 是要切割的视频文件名,-c copy -map 0表示直接复制原始视频流
    • -f segment表示将视频分段
    • -segment_time 10表示每段视频的时长为10秒
    • -segment_list 022309.m3u8 表示生成m3u8索引文件
    • -segment_format mpegts表示每个分段视频的格式为ts
    • 022309_%03d.ts表示输出文件的文件名模板。

    使用Mac自带浏览器访问

  • 相关阅读:
    架构漫谈 - 如何设计高性能、高可用、高扩展架构
    行情分析——加密货币市场大盘走势(10.16)
    实现ALV页眉页脚
    npm:发布/迭代个人开发包到世界仓库
    经典面试题 之 Dubbo和Zookeeper
    笔算开2次方根、3次方根详细教程
    自制的数据库安全攻防题,相关靶机自己制作
    Unity Shader—05 Unity中的纹理采样
    java正则校验金额
    C/S架构学习之UDP服务器
  • 原文地址:https://blog.csdn.net/u011327801/article/details/132939662