[yum]yum -y install gcc gcc-c++ openssl openssl-devel automake autoconf libtool make pcre pcre-devel libpcre3 libpcre3-dev libssl-dev
nginx安表部笔记 txt文本
安装 nginx的依赖
主要包括:gcc,pcre, pcre-devel,zlib, zlib-devel, openssl, openssl-devel
命令:
yum install gcc(iunx下的编详) # ubuntu 编译 :apt instll gcc
yum -y install make zlib-devel gcc-c++ libtool openssl openssl-devel
命令
gcc -v
openssl version -a
rpm -qa pcre
rpm -qa zlib
开机启动
vi /etc/systemd/system/nginx.service
加入
Unit
Description=nginx
After=network target
Service
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/Nginx-s reload
ExecStop=/usr/local/ngin/sbin/nginx -squit
Private Tmp=true
Install
Wantedby=Multi-user target
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/Nginx-s reload
ExecStop=/usr/bin/pkill -f nginx
Private Tmp=true
[Install]
WantedBy=multi-user.target
设置开机日启动
systemctl enable nginx service
停止开机目启动
systemctl disable nginx service
启动 nginx报务
systemctl start nginx service
停止 nginx服务
systemctl stop nginx service
重新启动服务器
systemctl restart nginx service
关闭防火
systemctl stop firewalld. serviced
Nginx部署:
rtmp
{
server
{
listen 1935;
chunk_size 8192;
#vod config
application vod
{
play /var/vod/flv;
}
#live config
application live
{
live on;
max_connections 1024;
allow play all;
record_path /var/live;
recorder audio
{
record audio;
record_suffix -%d-%b-%y-%T.flv;
}
recorder chunked
{
record all;
#record_max_size 5120K;
record_interval 15s;
record_path /var/live/chunked;
}
}
#hls
application hls
{
live on;
hls on;
hls_path /var/hls;
hls_playlist_length 30s;
hls_sync 100ms;
meta on;
recorder chunked
{
record all;
#record_max_size 5120K;
record_interval 15s;
record_path /var/hls/Chunked;
}
recorder audio
{
record audio;
record_suffix -%d-%b-%y-%T.flv;
}
}
}
}
配置详解
1、rtmp{}
是一个用于保存所有rtmp配置的块
在这里就是rtmp直播录播配置的根
2、server{}
server块里面放服务器实例,比如配置里的三个application(application在第6个解释)
3、listen
listen比较好理解,监听某个端口,让nginx监听并接收rtmp连接
4、chunk_size
接收网络流的块大小,接触过NIO的应该比较清楚,基于块比基于流效率要高,chunk_size默认值是4096,至少128,数字越大服务器负载越高,服务器调优这里较为重要
5、注释:#
nginx配置文件里使用‘#’作为注释
6、application
见名知意,创建一个应用/实例,后面接上应用实例类型,如上配置,创建了三个应用,比如上面配置的三个服务器应用实例:(1)rtmp录播(vod),(2)rtmp直播(live),(3)hls直播(hls)
重要:rtmp模块的配置与nginx的http模块是两种不同的配置指令,两者不要混淆
7、vod(录播)配置详解
play 录播的播放目录
8、live(直播)配置详解
(1)live on/off
直播模式,一对多广播
(2)max_connections
最大连接数,rtmp直播流协议属于长连接协议,服务器能开多少连接要把握好,hls协议是基于http的协议,所以理论上要比rtmp协议并发量要高很多
(3)allow play/publish all/ip地址
允许来自指定的地址/所有地址播放和发布
比如上面配置使用allow play all允许所有地址播放实时流,如果设置成allow play 127.0.0.1 就是只允许本地播放;
再举个例子:allow publish 127.0.0.1就是允许本机发布实时流,其他地址都不能发布。
(4)record_path
用来指定录制文件的目录,默认是/tmp
(5)record off/all/video/audio/keyframes/maual
record off:什么都不录制
record all:录制所有
record video:只录制视频
record audio:只录制音频
record keyframes:只录制关键帧
record maual:通过接口控制录制的启动停止
可以进行组合,比如:record video keyframes:就是只录制视频关键帧
(6)record suffix
录制文件的文件名后缀,默认是.flv
比如上面的配置 record suffix -%d-%b-%y-%T.flv,录制文件生成文件名就是这样(举例):应用名-24-Jul-04-17:07:45.flv
(7)record_max_size
上面配置:record_size 5120k,录制文件的最大值是5M
(8)record_interval
配置里的:record_interval 15s,就是录制文件的间隔,间隔15秒开始下一个片段的录制;
设置成 record_interval 0就是录制视频文件没有间隔;
设置成record-interval off就会把所有视频流全都写到一个文件里去。
重要:想要把流媒体保存文件,这个可以用来做文件分片,可以按天或者按小时生成新的文件,很实用的功能
9、hls直播配置详解
(1)hls on
这个参考 live on就行了,很简单,就是开不开启hls,hls off就是不开启
(2)hls_path
就是录制视频文件的目录/路径
(3)hls_playlist_length
hls播放列表长度,默认30分钟,这里设置成30秒:hls_playlist_length 30s
(4)hls_sync
设置hls时间戳同步阈值,通俗一点就是强制的音/视频同步,可以防止音画不同步的现象,默认是2ms,
(5)meta on
切换发送视频元数据到客户端,默认就是meta on,如果想要用修改后的视频得用meta off了
Nginx+ffmpeg实现Hls(m3u8)推流播放
Nginx部署:
# vim /application/nginx/conf/nginx.conf
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 20000;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /hls {
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root html;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
}
}
# /application/nginx/sbin/nginx -s reload //重新加载启动
ffmepg部署
#1、安装依赖
yum install yasm -y
#2、下载ffmpeg并安装
git clone https:#git.ffmpeg.org/ffmpeg.git ffmpeg
cd ffmpeg #解压并进入目录
./configure --prefix=/usr/local/ffmpeg
make
make install
#3、拷贝命令到/usr/bin #方便后面调用
ls /usr/local/ffmpeg/ #查看安装目录生成的文件
bin include lib nohup.out share
cp /usr/local/ffmpeg/bin/* /usr/bin/ #复制命令
配置ffmpeg转发
#1、启用ffmpeg进行推流
ffmpeg -re -rtsp_transport tcp -i rtsp://账号:密码@IP:端口/mpeg4/ch1/sub/av_stream.mp4 -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -r 25 -b:v 500k -s 480*480 -c `copy` -f hls -hls_time 2.0 -hls_list_size 0 -hls_wrap 15 /application/nginx/html/hls/test.m3u8
#-i //要处理视频文件的路径,此处地址是一个监控摄像头
#-acodec //音频处理的格式
#-strict experimental //更标准的严格性
#-ar //设置音频采样率
#-ac //设置通道为2
#-b:a //要将输出文件的音频比特率设置为96 kbit/s
#-r //要强制输出文件的帧频为24 fps
#-b:v //要将输出文件的视频比特率设置为500 kbit/s。
#-s //分辨率,传输的带宽根据分辨率来设定的,-b:v不能控制
#-f //强制输出的视频格式
#-hls_time //为切片的秒数
#-hls_list_size //设置播放列表保存的最多条目,我这里设置为0会保存有所片信息,默认值为5
#-hls_wrap //为切片的个数
Tips:-s参数在ffmpeg转发命令里面就算使用了-c copy参数也不会生效!具体的分辨率是根据海康威视监控视频的分辨率来定的(主码流和子码流不相同)。主码流一般为1920x1080P[平均2M网速],子码流一般为1280x720P[平均1.2M网速],704x576[平均800Kb]。海康威视平台配置如下图,设置码率类型为定码率的话流量要稳定一些。
写前端访问代码查看:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="shortcut icon" href="./static/logo.ico">
<style type="text/css">
html,body{margin:0px;height:100%;width:100%; display: flex; align-items: center}
#video{
width:1000px;
height:600px;
}
</style>
<title>无BUG</title>
</head>
<body style="margin:0;overflow:hidden">
<video id="video" muted></video>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script>
function initVideo () {
let video = document.getElementById('video')
if(Hls.isSupported()) {
let hls = new Hls()
//该hls地址为nginx配置中的地址
hls.loadSource('http://192.168.31.88:20000/hls/test.m3u8')
hls.attachMedia(video)
hls.on(Hls.Events.MANIFEST_PARSED,()=>{
video.play()
});
}else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = 'http://192.168.31.88:20000/hls/test.m3u8'
video.addEventListener('loadedmetadata',function() {
video.play()
})
}
}
initVideo()
</script>
</body>
</html>
可以在pc机浏览器上查看视频
后续出现跨域问题配置Nginx端口访问index.html即可
1、如果到启动时缺少了ffmpeg模块
参考链接
解决办法:
1、去其他服务器上有这个模块的拷贝过来libva.so.1 、libva-drm.so.1
2、去下载这个模块放在对应的位置
ldd which ffmpeg
//查看缺少的模块
六、写在最后:
1、主码流和子码流区分如下图:
2、各个直播协议的优缺点如下图:
3、如果是子码流访问的话,需要将子码流的分辨率与码率上限对应
4、-vf scale=320:240这个参数只能在将Rtsp转发为Rtmp的时候使用