1.下载 ngix库,地址 nginx: download 写博客时候我的最新稳定版本为1.20版本
2.下载nginx-http_flv库。git地址 https://github.com/winshining/nginx-http-flv-modulehttps://github.com/winshining/nginx-http-flv-module
3.解压nginx-1.20和nginx-http-flv
4.执行如下命令(root下)
[root~]# cd nginx-1.20.2
//添加rtmp,flv,mp4和openssl支持,其中http_flv应该默认支持rtmp
[root~]# ./configure --prefix=./output --add-module=../nginx-http-flv-module-master --with-http_ssl_module --with-http_flv_module --with-http_mp4_module
[root~]# make && make install
其中,--prefix=./output为自己新建的输出目录,可根据自己情况更改路径,nginx-http-flv-module-master为上面下载的http_flv库解压路径 根路径。生成文件在output路径下查找
错误:
./configure: error: the HTTP rewrite module requires the PCRE library.
./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.
如果报上面的两个错误,则对应的安装下列库即可
yum -y install pcre-devel
yum -y install openssl openssl-devel
5.创建文件夹
nginx-1.20根路径下创建文件夹 temp/hls
6.修改output/conf/路径下的nginx.conf文件
worker_processes 1;
error_log logs/error.log info;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
application live {
live on;
}
application hls {
live on;
hls on;
hls_path temp/hls;
hls_fragment 8s;
}
}
}
http {
server {
listen 8090;
location / {
root html;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root html;
}
location /hls {
#server hls fragments
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias temp/hls;
expires -1;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
}
location /flv {
flv_live on;
chunked_transfer_encoding on;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
}
}
}
6.将output/sbin下的nginx复制到nginx-1.20路径下,执行nginx。如果不想复制,直接执行,可把 --prefix=./output 路径改为绝对路径
7.测试
我使用ffmpeg在服务器端进行获取服务器本地视频 video1.mp4并通过rtmp推流到nginx,然后在PC端进行http取流。
服务器端:./ffmpeg -re -stream_loop -1 -i video1.mp4 -c copy -f flv rtmp://localhost:1935/hls/video1
PC端用VLC取流:http://192.168.1.5:8090/flv?port=1935&app=hls&stream=video1 IP地址是服务器地址,且PC机跟服务器处于同网段
如果有条件,服务器直连海康摄像头,还可以实现摄像头直播,服务器端ffmpeg命令改为如下:
ffmpeg -i "rtsp://用户名:密码@摄像头IP:554/h264/ch1/main/av_stream" -probesize 12800 -vcodec copy -acodec copy -f flv rtmp://localhost:1935/hls/video1
注意:如果不通,看一下系统防火墙是否关闭了。
使用命令:systemctl status firewalld.service
查看防火墙状态
执行后可以看到绿色字样标注的“active(running)”,说明防火墙是开启状态
使用命令:systemctl stop firewalld.service
关闭运行的防火墙
关闭后,使用命令systemctl status firewalld.service
查看防火墙状态
可以看到,disavtive(dead)的字样,说明防火墙已经关闭
前面的方法,一旦重启操作系统,防火墙就自动开启了,该怎么设置才能永久关闭防火墙呢?
输入命令:systemctl disable firewalld.service,禁止防火墙服务器
如果此时有个公网IP,比如买了阿里云的服务,可以通过frp进行内网穿透,具体看我下一篇博客即可
部署完之后把局域网内的IP换成公网IP即可,其它字段不变(前提是端口一一对应,如果不是,比如非要把8090映射成9090,端口也要随之改变。但我想大部分人不会给自己找麻烦的^_^)