編譯Nginx
下載最新的nginx源碼
wget http://nginx.org/download/nginx-1.19.2.tar.gz
如果nginx是用yum安裝的砰逻,一定不要用網(wǎng)上其余文章所謂的基于現(xiàn)有的nginx編譯,yum自帶的1.14.1版本bug很多懈息,全是坑,想要基于原有的版本編譯出來(lái)基本不可能
下載rmtp模塊
https://github.com/arut/nginx-rtmp-module
獲取當(dāng)前的nginx的配置芙代,復(fù)制下來(lái)
nginx -V
解壓下載的nginx鼓黔,進(jìn)入準(zhǔn)備編譯
tar -xzvf nginx-1.19.2.tar.gz
cd nginx-1.19.2
將剛才拿到的nginx配置組合成參數(shù),進(jìn)行編譯
./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
谎脯,路徑為剛才下載的模塊解壓后的文件夾
如果執(zhí)行出錯(cuò)了一般是缺少一些東西導(dǎo)致的葱跋,可以上網(wǎng)搜一下,這里就不一一贅述了
執(zhí)行make
make
此處肯定會(huì)報(bào)錯(cuò),需要修改
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
網(wǎng)上的文章都說(shuō)此處不要使用make install娱俺,但按照他們的方法替換過(guò)去稍味,會(huì)報(bào)一些庫(kù)文件不匹配,所以不如直接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文件轉(zhuǎn)為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
如果分段切得太小僵朗,會(huì)導(dǎo)致視頻一卡一卡的赖欣,所以使用
-segment_time 30
增大分段
測(cè)試播放
使用VLC軟件,點(diǎn)擊打開(kāi)網(wǎng)絡(luò)验庙,將配置好的路徑放進(jìn)去
點(diǎn)擊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 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>