搭建Nginx与FFmpeg流媒体服务器
需求
本方案旨在阐述基于Nginx与FFmpeg构建流媒体服务器的具体实施步骤。典型应用场景如企业内部需多人同时观看在线直播,若直接外网访问将占用大量出口带宽,干扰其他同事正常上网。为此,可在内网部署一台流媒体服务器,由Nginx结合FFmpeg从外部拉取直播流,内网用户直接访问该服务器即可获取内容。此外,该架构还支持推流、拉流、转推以及利用FFmpeg进行实时切片和视频处理等功能,从而构建完整的直播服务体系。
环境
系统环境:CentOS release 6.7 (Final)
步骤
安装ffmpeg
安装流程可参照官方文档指引:https://trac.ffmpeg.org/wiki/CompilationGuide
安装Nginx
此处采用源码编译方式安装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 直播流)模式。默认为 off
rtmp_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;
}
以下提供一个基础配置示例:RTMP服务监听1935端口;若需启用HLS协议,需设置hls on开启功能,并指定临时文件存储目录为hls_path /tmp/hls;更复杂的配置细节请参阅nginx-rtmp-module的README文档,其中包含详尽的配置说明。
重启Nginx
nginx -t
nginx -s reload
确认Nginx已成功监听1935端口。
使用ffmpeg推流到nginx
将本地MP4文件推流至上述配置的myapp应用:
ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/myapp/test1"
流媒体播放地址为(10.0.0.6为本机IP):rtmp://10.0.0.6:1935/myapp/test1
将本地MP4文件推流至HLS应用:
ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/hls/test2"
流媒体播放地址为: http://10.0.0.6/hls/test2.m3u8
总结
以上内容即为搭建流媒体服务器的全过程,希望能对读者的学习或工作有所助益。如有疑问欢迎留言交流,感谢支持。