确定现有nginx版本
#查看现有nginx版本信息
nginx -V
#停止运行nginx
systemctl stop nginx
下载nginx源码和nginx-http-flv-module
nginx源码:https://nginx.org/download/nginx-1.18.0.tar.gz
nginx-http-flv-module:https://github.com/winshining/nginx-http-flv-module/archive/refs/tags/v1.2.11.tar.gz
将两个文件放在同一个目录下
#解压
tar -zxvf nginx-1.18.0.tar.gz
tar -zxvf nginx-http-flv-module-1.2.11.tar.gz
安装依赖
sudo apt update
sudo apt install build-essential
sudo apt-get install libxml2 libxml2-dev libxslt1-dev
sudo apt install libgd-dev
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install libssl-dev
sudo apt-get install zlib1g zlib1g-dev
备份原有nginx
sudo mv /usr/sbin/nginx /usr/sbin/nginx_backup
编译nginx
#进入nginx源码目录
cd /path/to/nginx
#开始编译安装
./configure --add-module=../nginx-http-flv-module
make
make install
#再次查看确认
nginx -V
修改或替换nginx配置文件
vi /usr/local/nginx/conf/nginx.conf
内容如下:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#http-flv的配置
location /live {
flv_live on;
chunked_transfer_encoding on; #支持'Transfer-Encoding: chunked'方式回复
add_header 'Access-Control-Allow-Origin' '*'; #添加额外的HTTP头
add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的HTTP头
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
#rtmp配置
rtmp {
out_queue 4096;
out_cork 8;
max_streams 128;
server {
listen 1935;
application live {
live on;
record off;
gop_cache on;
}
}
}
#运行nginx
/usr/local/nginx/sbin/nginx
安装ffmpeg
sudo apt install ffmpeg
推流
ffmpeg -stream_loop -1 -re -i qr3.mp4 -c copy -f flv rtmp://192.168.1.253/live/qr3
-stream_loop -1 循环播放
-re 同步视频时间
-i qr3.mp4 指定推流的视频
-f flv 以flv格式输出
rtmp://127.0.0.1/live/qr3 输出到rtmp推流服务器
以上面的例子,那么观看直播的地址就是
http://192.168.1.253/live?app=live&stream=qr3