• 使用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自带浏览器访问

  • 相关阅读:
    【需求研发002】--备品备件系统优化
    复现一个循环以及俩个循环问题
    第二证券|小鹏持续萎靡,理想蔚来逆势反弹破月销记录
    APP 专项测试之兼容性测试
    Light Image v6.1.8.0批量调整图片大小
    Hadoop MapReduce 1.x 工作原理
    通用的异常处理程序机制与处理返回值方案
    刷题笔记 1
    mysql语句
    stm32cubemx安装(出现JDK配置错误,导致无法安装)
  • 原文地址:https://blog.csdn.net/u011327801/article/details/132939662