利用nginx-rtmp-module搭建HLS流媒体服务器
此篇我们介绍如何使用nginx和它的扩展模块nginx-rtmp-module搭建HLS流媒体服务器。
需要用到2个工程,nginx的源代码和nginx-rtmp-module,分别从他们的官方网站下载:
http://nginx.org/en/download.html
https://github.com/arut/nginx-rtmp-module
nginx注意选择他们的稳定版本的的linux版本,本文的操作的环境是linux。
下载完成后,解压这2个压缩包,然后CD进nginx的源代码目录,然后执行下面的命令:
$ ./configure --add-module=/path/to/nginx-rtmp-module
$ make
$ make install
如果你下载的nginx版本是 1.3.14 – 1.5.0之间,那么 http_ssl_module也需要l:
./configure --add-module=/path/to/nginx-rtmp-module --with-http_ssl_module
安装完成之后,使用下面命令测试以下是否成功安装
$ sudo /usr/local/nginx/sbin/nginx
然后访问 http://localhost
如果你看到如下的页面,说明配置成功:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
接下来我们配置 /usr/local/nginx/conf/nginx.conf
在修改这个文件之前我们先备份这个文件
$ sudo cp nginx.conf nginx.conf-bak
然后修改为如下内容:
————————————————————
#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 80;
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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
server {
listen 8080;
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root html;
expires -1;
}
}
}
rtmp {
server {
listen 1935;
chunk_size 4000;
#HLS
# For HLS to work please create a directory in tmpfs (/tmp/app 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.
#
application hls {
live on;
hls on;
hls_path /usr/local/nginx/html/hls;
hls_fragment 5s;
}
}
}
————————————————————
其中蓝色部分是我们后来添加上去的。
然后使用下面的命令测试我们新修改的配置文件是否有错,
$ /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
如果看到上面的信息,说明没有问题。
然后我们ffmpeg作为上传客户端:
$ ffmpeg -re -i /tmp/tom-jerry.mp4 -vcodec copy -acodec copy -f flv rtmp://localhost/hls/mystream
如果你是从别的机器上传这个流,就将localhost改为nginx所在机器的IP地址。
这样会在
/usr/local/nginx/html/hls
这个目录下面生成一系列的ts文件或一个m3u8文件,注意这个文件的所属是nobody,因此不可以直接cd进去,不过可以
$ sudo ls /usr/local/nginx/html/hls
来查看内容
接下来就是播放我们的这个hls流了,我们可以2个选择vlc和ffplay,这里我们以vlc为例子:
$ ffplay http://localhost:8080/hls/mystream.m3u8
成功播放上传的视频流。
到此结束。
更多关于nginx-rtmp-module的配置的说明,官方网站有非常详尽的说明,包括如何编译,如何配置
https://github.com/arut/nginx-rtmp-module
版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章转载自:IT夜班车,否则按侵权处理.