发布于 2017-03-16 02:04:45 | 320 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Nginx开发从入门到精通,程序狗速度看过来!

Nginx WEB服务器

Nginx 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。


这篇文章主要给大家介绍了利用nginx与ffmpeg搭建流媒体服务器的全过程,文中介绍的很详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。

需求

本文介绍的是利用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 直播流)模式。默认为 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,并且为hls设置一个临时文件目录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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。谢谢大家对PHPERZ的支持。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务