文章目录
- 一、使用 Nginx-rtmp-module 编译 Nginx
- 下载 Nginx-rtmp-module
- 安装 Nginx 依赖
- 下载 Nginx
- 编译 Nginx
- 二、Nginx 配置文件
- 三、启动 Nginx 服务
- 方式一
- 安装nginx初始化脚本、获取nginx初始化脚本
- 方式二
- 直接调用
- 四、使用 RTMP 将视频推送到 Nginx
- 安装FFmpeg
- 捕获网络摄像头 /dev/video0 并将其流式传输到 Nginx
- 流式传输文件示例视频.mp4
- 流式传输另一个 rtmp 流
- 浏览器获取视频流
- 补充
- OBS 推流
- 检查 .m3u8 文件
- 拉流
一、使用 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