ningx服务器的 nginx-rtmp-module模块很强大,可以支持rtmp, HLS以及MPEG DASH。今天在我的centos 7上用nginx搭建了一个简单的视频服务器
首先,是下载以及编译nginx.
nginx官网(https://nginx.org/en/download.html )可以下载nginx的各种版本,但貌似里面并没有rtmp模块
所以,需要自己下载源代码编译。
1.下载nginx
hg clone http://hg.nginx.org/nginx
2.下载 nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git
3. 安装编译依赖的库文件
yum install gc gcc gcc-c++ pcre-devel zlib-devel make wget openssl-devel libxml2-devel libxslt-devel gd-devel perl-ExtUtils-Embed GeoIP-devel gperftools gperftools-devel libatomic_ops-devel perl-ExtUtils-Embed4. cd到nginx源码目录,将auto目录下的configure文件copy到nignx源码根目录。然后执行一下命令
./configure --add-module=/usr/build/nginx-rtmp-module --with-http_ssl_module
make
make install
此时,nginx默认被安装到/usr/local/nginx目录,把目录/usr/local/nginx/sbin/添加到环境变量。
用nginx命令即可启动服务器,用nginx -s stop关闭服务器
然后,配置nginx服务器
修改
/usr/local/nginx/conf/下的nginx.conf文件为
#user nobody;
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
# sample handlers
#location /on_play {
# if ($arg_pageUrl ~* localhost) {
# return 201;
# }
# return 202;
#}
#location /on_publish {
# return 201;
#}
#location /vod {
# alias /var/myvideos;
#}
# rtmp stat
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# you can move stat.xsl to a different location
root /usr/build/nginx-rtmp-module;
}
# rtmp control
location /control {
rtmp_control all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
rtmp {
server {
listen 1935;
ping 30s;
notify_method get;
application myapp {
live on;
# sample play/publish handlers
#on_play http://localhost:8080/on_play;
#on_publish http://localhost:8080/on_publish;
# sample recorder
#recorder rec1 {
# record all;
# record_interval 30s;
# record_path /tmp;
# record_unique on;
#}
# sample HLS
#hls on;
#hls_path /tmp/hls;
#hls_sync 100ms;
}
# Video on demand
#application vod {
# play /var/Videos;
#}
# Video on demand over HTTP
#application vod_http {
# play http://localhost:8080/vod/;
#}
}
}
此时如果配置正确,打开
http://localhost:8080/stat可以看到rtmp的统计信息
最后,用ffmpeg串流到刚刚配置的rtmp://localhost/myapp/mystream
需要注意的是,目前服务器只支持有限的音视频编码格式,如H264, AAC, MP3等。
ffmpeg -re -i test.mp4 -c:v libx264 -c:a libfaac -ar 44100 -ac 1 -f flv rtmp://localhost/myapp/mystream
此时,浏览器打开
http://localhost:8080/stat能看到信息的变化。
也可以用播放器进行播放,例如
ffplay rtmp://localhost/myapp/mystream