使用Nginx搭建流媒体

文章目录

    • 使用Nginx搭建流媒体
      • 安装 ffmpeg
        • 1、下载安装ffmpeg
        • 2、安装依赖
        • 3、编译安装 ffmpeg
        • 4、创建全局链接
        • 5、验证
      • 安装nginx
        • 1、下载依赖包
        • 2、 解压安装nginx
        • 3、配置启动脚本
        • 5、 启动 nginx
        • 6、上传视频测试验证

使用Nginx搭建流媒体

参考地址

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

安装 ffmpeg

1、下载安装ffmpeg

https://johnvansickle.com/ffmpeg/
https://www.johnvansickle.com/ffmpeg/old-releases/ffmpeg-5.1.1-amd64-static.tar.xz

2、安装依赖

yum -y install bzip2 yasm

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/

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

5、验证

ffmpeg -h

安装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

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

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

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;
    }

5、 启动 nginx


systemctl daemon-reload
systemctl start nginx
systemctl enable nginx
systemctl status nginx

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

参数说明

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

你可能感兴趣的:(nginx,运维,视频)