本文在Centos7系统基于Nginx搭建RTMP/HLS 直播,系统及软件:
- Centos:CentOS Linux release 7.3.1611 (Core)
- Nginx:1.12.0
- pcre-8.40 wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz
- zlib-1.2.11 wget http://www.zlib.net/zlib-1.2.11.tar.gz
- openssl-1.0.2l wget http://distfiles.macports.org/openssl/openssl-1.0.2l.tar.gz
- nginx-rtmp-module-master https://github.com/arut/nginx-rtmp-module.git
- nginx_mod_h264_streaming http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
- 注释nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c
/*
if (r->zero_in_uri)
{
return NGX_DECLINED;
}
*/
- 注释nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c
安装Nginx
- ……省略解压等过程
- cd nginx-1.12.0
- ./configure prefix=/usr/local/nginx \
–with-pcre=../pcre-8.40 \
–with-zlib=../zlib-1.2.11 \
–with-openssl=../openssl-1.0.2l \
–add-module=../ngx_cache_purge-master \
–with-http_ssl_module \
–with-http_stub_status_module \
–with-http_gzip_static_module \
–add-module=../nginx-rtmp-module-master \
–add-module=../nginx_mod_h264_streaming-2.2.7 - make
- 如果报 Werror 错误,去掉 objs/Makefile 选项
- make install
配置RTMP
在rtmp{……}中配置
rtmp {
server {
listen 1935;
chunk_size 4000;
application mylive {
live on;
# record first 1K of stream
record all;
record_path /tmp/av;
record_max_size 1K;
record_suffix .mp4;
}
}
}
不需要录制的话去掉record或者 record off 即可;
使用OBS推流
推流地址:rtmp://192.168.1.152:1935/myplive/12345678
使用VLC拉流
拉流地址:rtmp://192.168.1.152:1935/myplive/12345678
结果:
配置HLS
在rtmp{……}中配置
application hls {
live on;
hls on;
hls_path /tmp/hls;
hls_fragment 4s;
hls_playlist_length 12s;
hls_fragment_naming system;
sync 10ms;
}
在http{……}中配置
server {
listen 8008;
server_name localhost;
#charset koi8-r;
access_log logs/stream.access.log;
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
}
然后重启Nginx
使用OBS推流
推流地址:rtmp://192.168.1.152:1935/hls/12345678
使用VLC拉流
推流地址:http://192.168.1.152:8008/hls/12345678.m3u8
注意hls延迟比RTMP大