运行环境:ubuntu 20.04
时间:2024年10月26日
环境更新
sudo apt-get update
sudo apt-get install build-essential libtool libpcre3 libpcre3-dev zlib1g-dev openssl
下载nginx
wget http://nginx.org/download/nginx-1.19.2.tar.gz
tar xvzf nginx-1.19.2.tar.gz
cd nginx-1.19.2/
./configure --prefix=/usr/local/rtmp-nginx --with-http_ssl_module --add-module=/home/jbj/zvmdia/nginx/nginx-http-flv-module
sudo make -j 8
sudo make install
最终安装到⽬录:
ls /usr/local/rtmp-nginx/
sudo chown -R jbj:root .
sudo chmod -R 777 .
nginx配置测试
1.启动nginx
2.ffmpeg 推流,nginx会监听到推流 进行application live转码出多个高低码流文件
ffmpeg -re -i time.mp4 -vcodec copy -acodec copy -f flv -y rtmp://127.0.0.1/live/jbj
3.ffplay 拉流播放
ffplay rtmp://127.0.0.1/live/jbj
或者使用vlc 访问其他码流
http://127.0.0.1:8081/hls/jbj.m3u8
http://127.0.0.1:8081/hls/jbj_low/index.m3u8
http://127.0.0.1:8081/hls/jbj_hi/index.m3u8
nginx配置文件
/tmp/hls 转码文件目录
宽带和码率不匹配时,建议提示用户手动切换。因为网络抖动会导致频繁切换,最终观影体验极差。
cpp
user root;
# 关闭 Daemon 进程
daemon off;
# 关闭 Master 进程
master_process off;
# 设置日志级别
error_log /tmp/error.log debug;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4000;
# Live 应用配置
application live {
live on;
# 使用 FFmpeg 进行转码并输出多码率 FLV 流
exec /usr/bin/ffmpeg -i rtmp://localhost/live/$name
-c:a copy -c:v libx264 -b:v 300K -g 30 -s 720x420 -f flv rtmp://localhost/hls/$name_hi
-c:a copy -c:v libx264 -b:v 200K -g 30 -s 462x254 -f flv rtmp://localhost/hls/$name_mid
-c:a copy -c:v libx264 -b:v 100K -g 30 -s 230x128 -f flv rtmp://localhost/hls/$name_low;
}
# HLS 应用配置
application hls {
live on;
hls on;
hls_path /tmp/hls;
hls_nested on;
hls_fragment 2s;
hls_playlist_length 6s;
# 配置不同码率的 HLS 流
hls_variant _hi BANDWIDTH=350000;
hls_variant _mid BANDWIDTH=250000;
hls_variant _low BANDWIDTH=150000;
}
}
}
http {
server {
listen 8081;
# 默认 Web 服务根目录
location / {
root html;
index index.html index.htm;
}
# HLS 文件访问配置
location /hls {
types {
application/vnd.apple.mpegusr m3u8;
video/mp2t ts;
}
alias /tmp/hls;
add_header Cache-Control no-cache;
}
}
}