编译Nginx
下载最新的nginx源码
wget http://nginx.org/download/nginx-1.19.2.tar.gz
如果nginx是用yum安装的,一定不要用网上其余文章所谓的基于现有的nginx编译,yum自带的1.14.1版本bug很多,全是坑,想要基于原有的版本编译出来基本不可能
下载rmtp模块
https://github.com/arut/nginx-rtmp-module
获取当前的nginx的配置,复制下来
nginx -V
解压下载的nginx,进入准备编译
tar -xzvf nginx-1.19.2.tar.gz
cd nginx-1.19.2
将刚才拿到的nginx配置组合成参数,进行编译
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=/root/nginx-rtmp-module
后面增加了
--add-module=/root/nginx-rtmp-module
,路径为刚才下载的模块解压后的文件夹
如果执行出错了一般是缺少一些东西导致的,可以上网搜一下,这里就不一一赘述了
执行make
make
此处肯定会报错,需要修改
objs/Makefile
,在CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection
后添加-Wno-implicit-fallthrough
替换nginx
make install
网上的文章都说此处不要使用make install,但按照他们的方法替换过去,会报一些库文件不匹配,所以不如直接install
重启nginx
systemctl restart nginx
至此,支持流媒体的nginx就部署好了
配置HLS
编辑nginx.conf,增加rtmp配置
rtmp {
server {
listen 1935;
application rtmplive {
live on;
}
application vod{
play /home/work/qiuniu/vod;
}
}
}
注意此处要放在最外层,不要放置到http里面
编辑http配置文件,增加location配置
location /hls {
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /usr/share/nginx/html;
add_header Cache-Control no-cache;
}
视频切片
视频切片使用ffmpeg工具,将mp4文件转为ts
ffmpeg -y -i /home/work/qiuniu/mp4/demo.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb /home/work/qiuniu/ts/demo.ts
然后切分成小段ts,并放置在nginx配置的目录下
ffmpeg -i /home/work/qiuniu/ts/demo.ts -c copy -map 0 -f segment -segment_list /usr/share/nginx/html/hls/demo.m3u8 -segment_time 30 /usr/share/nginx/html/hls/demo%03d.ts
如果分段切得太小,会导致视频一卡一卡的,所以使用
-segment_time 30
增大分段
测试播放
使用VLC软件,点击打开网络,将配置好的路径放进去
点击open,就可以播放了
H5播放
使用vhs播放hls流
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="https://unpkg.com/video.js/dist/video-js.css" rel="stylesheet" />
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<video-js id="example-video" class="vjs-default-skin" width="960" height="400" controls>
<source src="https://localhost/hls/demo.m3u8" type="application/x-mpegURL" />
</video-js>
</div>
<script src="https://unpkg.com/video.js/dist/video.js"></script>
<script src="https://unpkg.com/@videojs/http-streaming/dist/videojs-http-streaming.js"></script>
<script>
var player = videojs('example-video');
player.play();
</script>
</body>
</html>