JetsonNano学习(七)Nginx 搭建 HLS 直播服务器


一、使用 Nginx-rtmp-module 编译 Nginx

首先,我们需要通过Nginx-rtmp-module编译Nginx,推荐使用sergey-dryabzhinsky,该仓库一直在被积极开发中,相比于arut具有更多的修复与改进。

下载 Nginx-rtmp-module

git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git	(Recommend)
git clone https://github.com/arut/nginx-rtmp-module.git					(original one)

安装 Nginx 依赖

sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

下载 Nginx

Nginx下载网站 选择最新的Nginx版本下载。
例如 nginx-1.24.0 可通过 https://nginx.org/download/nginx-1.24.0.tar.gz 获取。

wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -xf nginx-1.24.0.tar.gz
cd nginx-1.24.0

编译 Nginx

./configure --with-http_ssl_module --add-module=../nginx-rtmp-module   # Pay attention to the nginx-rtmp-module Path
make -j 1		# 1: The number of CPUs on U PC to accelerate the compilation
sudo make install

二、Nginx 配置文件

# 备份 nginx.conf
sudo cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
# 配置 nginx.conf
sudo vim /usr/local/nginx/conf/nginx.conf

参考文档链接

worker_processes  auto;
events {
    worker_connections  1024;
}

# RTMP configuration
rtmp {
    server {
        listen 1935; # Listen on standard RTMP port
        chunk_size 4000;

        application show {
            live on;
            # Turn on HLS
            hls on;
            # Make sure Pernission of Nginx to write
            hls_path /mnt/hls/;
            hls_fragment 3;
            hls_playlist_length 60;
            # disable consuming the stream from nginx as rtmp
            allow play all;
        }
    }
}

http {
    sendfile off;
    tcp_nopush on;
    aio on;
    directio 512;
    default_type application/octet-stream;

    server {
        listen 8080;
		#server_name ***.***.***.***;
        location / {
            # Disable cache
            add_header 'Cache-Control' 'no-cache';

            # CORS setup
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length';

            # allow CORS preflight requests
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }

            types {
                application/dash+xml mpd;
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            root /mnt/;
        }
    }
}

三、启动 Nginx 服务

方式一

安装nginx初始化脚本、获取nginx初始化脚本
wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx
sudo cp nginx /etc/init.d/
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults

sudo service nginx start
sudo service nginx stop

方式二

直接调用
# 开启
sudo /usr/local/nginx/sbin/nginx
# 关闭
sudo /usr/local/nginx/sbin/nginx -s stop

四、使用 RTMP 将视频推送到 Nginx

安装FFmpeg

sudo apt-get install ffmpeg

捕获网络摄像头 /dev/video0 并将其流式传输到 Nginx

ffmpeg -re -f video4linux2 -i /dev/video0 -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -f flv rtmp://localhost/show/stream

流式传输文件示例视频.mp4

ffmpeg -re -i example.mp4 -vcodec libx264 -vprofile baseline -g 30 -acodec aac -strict -2 -f flv rtmp://localhost/show/stream

流式传输另一个 rtmp 流

ffmpeg -i rtmp://example.com/appname/streamname -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -f flv rtmp://localhost/show/stream

浏览器获取视频流

http://localhost:8080/hls/stream.m3u8

补充

OBS 推流

在这里插入图片描述

检查 .m3u8 文件

在这里插入图片描述

拉流

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值