环境:ubuntu 14
需要安装:nginx和ffmpeg
ffmpeg:
直接安装apt不行,需要添加源:
但是这是在线安装,只有可执行文件,不能开发!!
add-apt-repository ppa:kirillshkrogalev/ffmpeg-next
apt-get update
apt-get install ffmpeg
因为目前没涉及开发,未尝试源码安装。
nginx:
首先,nginx不支持动态安装插件,一定不要直接apt-get。
先安装nginx-rtmp-module插件,再安装配置时指定添加nginx-rtmp-module模块
git clone https://github.com/arut/nginx-rtmp-module.git
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/src/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module
make
make install
这里遇到问题,却少一些依赖库,根据需要安装就行。
安装完后,打开本地127.0.0.1网页,即可查看是否安装成功。
搭配:
两者都安装完毕,在nginx的配置模块添加配置,支持hls:
打开在nginx目录的conf目录下的nginx.conf文件,在末尾添加如下:
rtmp {
server {
listen 1935; #监听的端口
chunk_size 4000;
application hls { #rtmp推流请求路径
live on;
hls on;
hls_path /home/hls/test;
hls_fragment 3s;
record off;
}
}
}
再在开始server结构中添加如下:
location /hls{
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /home/hls/test/;
expires -1;
add_header Cache-Control no-cache;
}
为了最终测试,最好再加一个跨域策略,让其他ip也可以访问搭建的服务器。
location / {
root html;
index index.html index.htm;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
}
最终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 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
}
#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;
#}
location /hls{
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /home/hls/test/;
expires -1;
add_header Cache-Control no-cache;
}
}
# 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;
# }
#}
}
rtmp {
server {
listen 1935; #监听的端口
chunk_size 4000;
application hls { #rtmp推流请求路径
live on;
hls on;
hls_path /home/hls/test;
hls_fragment 3s;
record off;
}
}
}
而后保存,重启nginx,看是否报错。
./nginx reload #重新加载配置项
./nginx reopen #重新启动nginx
不知道为什么有时候上面这个命令无效,这时使用下列命令:
./nginx -s reload #重新加载配置项
./nginx -s reopen #重新启动nginx
或者使用
./nginx -c 配置文件名 #重新加载配置项
这里经常会报一个错,80端口被占用,无法启动。使用netstat -ntpl 查看端口是否被占用,占用了则使用 kill -9 pid 杀了进程之后,再重启就好了。
无错则进入下一步:
使用ffmpeg分片:
ffmpeg 转换mp4或aac为 hls
转换成的hls,直接放置在nginx目录下的html里面,这时候在浏览器打开响应的m3u8文件就可以播放了