音视频开发(6)---nginx视频服务器

nginx视频服务器

 

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-Embed

4. cd到nginx源码目录,将auto目录下的configure文件copy到nignx源码根目录。然后执行一下命令  

 

 

 
  1. ./configure --add-module=/usr/build/nginx-rtmp-module --with-http_ssl_module

  2. make

  3. make install

此时,nginx默认被安装到/usr/local/nginx目录,把目录/usr/local/nginx/sbin/添加到环境变量。
用nginx命令即可启动服务器,用nginx -s stop关闭服务器

然后,配置nginx服务器

修改/usr/local/nginx/conf/下的nginx.conf文件为

 
  1. #user nobody;

  2. worker_processes 1;

  3.  
  4. error_log logs/error.log debug;

  5.  
  6. events {

  7. worker_connections 1024;

  8. }

  9.  
  10. http {

  11. include mime.types;

  12. default_type application/octet-stream;

  13.  
  14. sendfile on;

  15. keepalive_timeout 65;

  16.  
  17. server {

  18. listen 8080;

  19. server_name localhost;

  20.  
  21. # sample handlers

  22. #location /on_play {

  23. # if ($arg_pageUrl ~* localhost) {

  24. # return 201;

  25. # }

  26. # return 202;

  27. #}

  28. #location /on_publish {

  29. # return 201;

  30. #}

  31.  
  32. #location /vod {

  33. # alias /var/myvideos;

  34. #}

  35.  
  36. # rtmp stat

  37. location /stat {

  38. rtmp_stat all;

  39. rtmp_stat_stylesheet stat.xsl;

  40. }

  41. location /stat.xsl {

  42. # you can move stat.xsl to a different location

  43. root /usr/build/nginx-rtmp-module;

  44. }

  45.  
  46. # rtmp control

  47. location /control {

  48. rtmp_control all;

  49. }

  50.  
  51. error_page 500 502 503 504 /50x.html;

  52. location = /50x.html {

  53. root html;

  54. }

  55. }

  56. }

  57.  
  58. rtmp {

  59. server {

  60. listen 1935;

  61. ping 30s;

  62. notify_method get;

  63.  
  64. application myapp {

  65. live on;

  66.  
  67. # sample play/publish handlers

  68. #on_play http://localhost:8080/on_play;

  69. #on_publish http://localhost:8080/on_publish;

  70.  
  71. # sample recorder

  72. #recorder rec1 {

  73. # record all;

  74. # record_interval 30s;

  75. # record_path /tmp;

  76. # record_unique on;

  77. #}

  78.  
  79. # sample HLS

  80. #hls on;

  81. #hls_path /tmp/hls;

  82. #hls_sync 100ms;

  83. }

  84.  
  85. # Video on demand

  86. #application vod {

  87. # play /var/Videos;

  88. #}

  89.  
  90. # Video on demand over HTTP

  91. #application vod_http {

  92. # play http://localhost:8080/vod/;

  93. #}

  94. }

  95. }

此时如果配置正确,打开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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值