需求
本文介绍的是利用nginx和ffmpeg搭建流媒体服务器的过程。例如这种场景:公司内部需要同时观看在线直播时,如果每个人直接观看必然给出口带宽带来压力,影响正常访问外网的同事。所以可以在内网通过nginx+ffmpeg拉一路直播流,然后内网的用户访问内网的这台流媒体服务器即可。通过nginx+ffmpeg还可以实现推流、拉流、转推甚至利用FFmpeg实时切片、视频处理等,实现一套直播服务模型。
环境
系统环境:CentOS release 6.7 (Final)
步骤
安装ffmpeg
安装过程参考官方文档:https://trac.ffmpeg.org/wiki/CompilationGuide
安装Nginx
这里采用了编译安装的方式,需要注意的是:一定要添加nginx-rtmp-module模块
git clone https://github.com/arut/nginx-rtmp-module.git#编译的时候添加nginx-rtmp-module模块--add-module=path_of_/nginx-rtmp-module
我的nginx编译参数
./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre=/opt/software/pcre-8.35 --with-zlib=/opt/software/zlib-1.2.8 --with-openssl=/opt/software/openssl-1.0.1i --add-module=/opt/software/nginx-1.8.1/modules/nginx-rtmp-module
修改nginx配置文件nginx.conf
user nginx;worker_processes 2;error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { use epoll; worker_connections 1024;}#切换自动推送(多 worker 直播流)模式。默认为 offrtmp_auto_push on;#当 worker 被干掉时设置自动推送连接超时时间。默认为 100 毫秒rtmp_auto_push_reconnect 1s;rtmp { server { listen 1935; #直播流配置 application myapp { live on; } # hls切片 application hls { live on; hls on; hls_path /tmp/hls; } # 拉流 application qiniu { live on; push rtmp://拉流地址; } # 转推 application pull { live on; pull rtmp://转推地址; } # rtmp日志设置 access_log logs/rtmp_access.log ; } }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 65; gzip on; server { listen 80; server_name localhost; charset utf-8; access_log logs/host.access.log main; location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /opt/software/nginx-rtmp-module/; } location / { root /opt/www/html; index index.html index.htm; } location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /tmp; add_header Cache-Control no-cache; } #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; } }include vhosts/*.conf;}
新闻热点
疑难解答