下载nginx源码包:
wget https://mirrors.huaweicloud.com/nginx/nginx-1.14.0.tar.gz
解压缩:
tar -xvzf ./nginx-1.14.0.tar.gz
下载nginx-rtmp源码包:
wget https://github.com/arut/nginx-rtmp-module/archive/v1.2.1.tar.gz
解压缩:
tar -xvzf ./v1.2.1.tar.gz
安装nginx+rtmp组件
nginx (1.3.14 - 1.5.0)版本运行如下命令:
./configure --add-module=/path/to/nginx-rtmp-module --with-http_ssl_module
make
make install
1、rtmp点播:
nginx.conf配置文件中,添加rtmp,与http平行:
rtmp{ server { listen 1996; #//服务端口 chunk_size 4096; #//数据传输块的大小 application vod { play /disk1/guoyiman/rtmp/test; #//视频文件存放位置。 } } } //播放url:rtmp://host:port/vod/videoname |
2、rtmp直播:
events { use epoll;# 选择epoll模型可以达到最佳的IO性能 worker_connections 1024; } rtmp { #RTMP服务 server { listen 1996; #//服务端口 chunk_size 4096; #//数据传输块的大小 application live{ #直播 live on; } } } |
rtmp直播推流,拉流播放操作:
使用ffmpeg推流: ffmpeg -re -i "jin_all.mp4" -f flv rtmp://10.200.20.56:1996/hls/flv 播放url: rtmp://10.200.20.56:1996/hls/flv |
3、hls
RTMP推流请求配置
rtmp { server { # 监听端口 listen 1996; chunk_size 2048; application hls { live on; hls on; hls_path /data/hls; //hls 文件存放地址 } } } |
HTTP播放配置
http{ ... server { listen 18880; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # http播放地址 location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /data; //hls文件路径 add_header Cache-Control no-cache; } # 统计 location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /disk1/guoyiman/rtmp/nginx-rtmp-module/; } } } |
直播推流,hls播放
ffmpeg推流: ffmpeg -re -i "jin_all.mp4" -vcodec libx264 -vprofile baseline -f flv rtmp://10.200.20.56:1996/hls/test1 推流地址:rtmp://10.200.20.56:1996/hls,test1为名称可更改,hls为rtmp 中application 配置名称 rtmp播放:rtmp://10.200.20.56:1996/hls/test1 hls播放:http://10.200.20.56:18880/hls/test1.m3u8 监控统计:访问url:http://10.200.20.56:18880/stat |
遇到的坑点:
1、配置hls_path路径,使用ffmpeg推流,rtmp直播流可访问,hls直播404,检查原因,hls_path路径下没有ts,m3u8文件;
2、视频流切片存放失败原因:使用nginx配置中没有给nginx指定用户名只能走默认的nobody用户和nogroup用户组,其实就是没有组,需要对写入的目录增大权限的修改
3、修改目录权限仍没有出现ts,m3u8文件,原因:目录权限指全路径而非某一文件夹,所有hls_path中全路径都需要增大权限,避免权限增大影响,在主目录下单独开出data目录增大权限,测试后删除文件夹;亦可对nginx指定高权限用户组,暂未尝试
附录:nginx.conf参考文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 18880;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# http播放地址
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /data;
add_header Cache-Control no-cache;
}
# 统计
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /disk1/guoyiman/rtmp/nginx-rtmp-module/;
}
}
}
rtmp {
server {
# 监听端口
listen 1996;
chunk_size 2048;
# HLS
# For HLS to work please create a directory in tmpfs (/tmp/hls here)
# for the fragments. The directory contents is served via HTTP (see
# http{} section in config)
#
# Incoming stream must be in H264/AAC. For iPhones use baseline H264
# profile (see ffmpeg example).
# This example creates RTMP stream from movie ready for HLS:
#
# ffmpeg -loglevel verbose -re -i movie.avi -vcodec libx264
# -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
# -f flv rtmp://localhost:1935/hls/movie
#
# If you need to transcode live stream use 'exec' feature.
#
# rtmp推流请求路径
application hls {
live on;
hls on;
hls_path /data/hls;
}
application live{
live on;
}
application vod {
play /disk1/guoyiman/rtmp/test; #//视频文件存放位置。
}
}
}