nginx搭建http和rtmp协议的流媒体服务器

时间:2013-09-23 23:52来源:佚名 作者:本站 举报 点击:232次

实验目的:让Nginx支持flv和mp4格式文件,同时支持Rtmp协议;同时打开rtmp的hls功能
资料:
HTTP Live Streaming(缩写是 HLS)是一个由苹果公司提出的基于HTTP的流媒体 网络传输协议。
HLS只请求基本的HTTP报文,与实时传输协议(RTP)不同,HLS可以穿过任何允许HTTP数据通过的防火墙或者代理服务器。它也很容易使用内容分发网络来传输媒体流。
使用ffmpeg来完成对flv、mp4、mp3等格式的转化(点播实验暂时不测试)

一、准备工作
模块:nginx_mod_h264_streaming(支持h264编码的视频)
模块:http_flv_module 支持flv
模块:http_mp4_module 支持mp4
下载地址:
http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
http://nginx.org
https://github.com/arut/nginx-rtmp-module

1、安装依赖包:
#yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64

2、安装git工具:
#mkdir soft-source
#cd soft-source
#wget http://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
#tar xzvf git-latest.tar.gz 
#cd git-2013-02-04
#autoconf
#./configure
#make && make install
# git --version
git version 1.8.1.GIT
#cd ..

3、安装ffmpeg及其依赖包:
++++++++Yasm+++++++++++
#wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
#tar xzvf yasm-1.2.0.tar.gz
#cd yasm-1.2.0
#./configure
#make
#make install
#cd ..
++++++++x264+++++++++++
#git clone git://git.videolan.org/x264
#cd x264
#./configure --enable-shared 
#make
#make install
#cd ..
++++++++LAME+++++++++++
#wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
#tar xzvf lame-3.99.5.tar.gz
#cd lame-3.99.5
#./configure --enable-nasm
#make
#make install
#cd ..
++++++++libogg+++++++++++
#wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
#tar xzvf libogg-1.3.0.tar.gz
#cd libogg-1.3.0
#./configure
#make
#make install
#cd ..
++++++++libvorbis+++++++++++
#wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
#tar xzvf libvorbis-1.3.3.tar.gz
#cd libvorbis-1.3.3
#./configure
#make
#make install
#cd ..
++++++++libvpx+++++++++++
#git clone http://git.chromium.org/webm/libvpx.git
#cd libvpx
#./configure  --enable-shared
#make
#make install
#cd ..
++++++++FAAD2+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
#tar zxvf faad2-2.7.tar.gz
#cd faad2-2.7
#./configure
#make
#make install
#cd ..
++++++++FAAC+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
#tar zxvf faac-1.28.tar.gz
#cd faac-1.28
#./configure
#make
#make install
#cd ..
++++++++Xvid+++++++++++
#wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
#tar zxvf xvidcore-1.3.2.tar.gz
#cd xvidcore/build/generic
#./configure
#make
#make install
cd ..
#git clone git://source.ffmpeg.org/ffmpeg
#cd ffmpeg
#./configure  --prefix=/opt/ffmpeg/ --enable-version3  --enable-libvpx --enable-libfaac --enable-libmp3lame  --enable-libvorbis --enable-libx264 --enable-libxvid --enable-shared --enable-gpl --enable-postproc --enable-nonfree  --enable-avfilter --enable-pthreads
#make && make install
#cd ..

修改/etc/ld.so.conf如下:
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/ffmpeg/lib
#ldconfig
二、安装Nginx相关模块
#tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
#git clone git://github.com/arut/nginx-rtmp-module.git
#tar zxvf pcre-8.12.tar.gz
#cd pcre-8.12
#./configure
#make && make install
#tar zxvf nginx-1.2.6.tar.gz
#cd nginx-1.2.6
#./configure --prefix=/usr/local/nginx  --add-module=../nginx_mod_h264_streaming-2.2.7 --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_mp4_module --add-module=../nginx-rtmp-module --add-module=../nginx-rtmp-module/hls --with-cc-opt=-I/opt/ffmpeg/include --with-ld-opt='-L/opt/ffmpeg/lib -Wl,-rpath=/opt/ffmpeg/lib'
#make && make install
三、修改nginx主配置文件,配置虚拟主机(这里我们暂时只配置和测试点播,直播请看借鉴地址):
user  nobody nobody;
worker_processes 4;
error_log  logs/nginx_error.log  info;
pid        logs/nginx.pid;
worker_rlimit_nofile 51200;
events
   {
       use epoll;
       worker_connections 51200;
   }
#rtmp_auto_push on;
rtmp {
   server {
   listen 1935;
   application vod {
           play /opt/media/nginxrtmp/flv;
   }
   }
}
http
   {
       include       mime.types;
       default_type  application/octet-stream;
       server_names_hash_bucket_size 128;
       client_header_buffer_size 32k;
       large_client_header_buffers 4 32k;
       client_max_body_size 50m ;
       limit_conn_zone $binary_remote_addr zone=perip:256k;
       limit_conn_log_level notice;
       sendfile on;
       tcp_nopush     on;
       keepalive_timeout 6000;#测试并发临时调大
       tcp_nodelay on;
       gzip on;
       gzip_min_length  1k;
       gzip_buffers     4 16k;
       gzip_http_version 1.0;
       gzip_comp_level 2;
       gzip_types       text/plain application/x-javascript text/css application/xml;
       gzip_vary on;
       #log format
       log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
            '"$http_user_agent" $http_x_forwarded_for';

#支持flv
server
       {
           listen       8081;
           server_name 192.168.0.33;
           root /opt/pub/media/nginx; #http协议时候,flv视频位置
           location ~ .*.(flv|swf|mp4|wma|wmv)$ {
                   valid_referers none blocked *.xxxx.com http://localhost;
                   if ($invalid_referer) {
                   return 403;
                   }
           }
           location ~ \.flv$ {
                   flv;
                   limit_conn one 20;#限制客户端并发连接数
                   limit_rate 200k;#限制每客户端最大带宽
           }
           location ~ \.mp4$ {
                   flv;
                   limit_conn one 20;
                   limit_rate 200k;
           }
           access_log  logs/nginxflv_access.log  main;
}

server
       {
           listen       8082;
           server_name 192.168.0.33;
           index index.html;
           location / {
               root /opt/pub/media/nginx-rtmp;
           }
           access_log  logs/nginxrtmpflv_access.log  main;
       }

server {
       listen      8080;
       location /stat {
           rtmp_stat all;
           rtmp_stat_stylesheet stat.xsl;
       }
       location /stat.xsl {
           root /opt/pub/soft/nginx-rtmp-module;
       }
       location / {
           root /opt/pub/soft/nginx-rtmp-module/test/rtmp-publisher;
       }
   }

http://192.168.0.33:8080/stats 查看rtmp客户请求信息
http://192.168.0.33:8081/index.html 查看nginx http协议时候的flv视频
http://192.168.0.33:8082/index.html  查看nginx rtmp协议时候的flv视频

四、基本功能测试:
播放器采用开源播放器jwplayer,将播放器放到index.html同级目录下
NGINX-Http测试页面index.html(切记http的时候需要给视频添加关键帧)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test of nginx-http</title>
</head>
<body>
<div id="myElement"></div>
<object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="480" height="360">
 <param name="movie" value="player.swf" />
 <param name="quality" value="high" />
 <param name="wmode" value="opaque" />
 <param name="swfversion" value="6.0.65.0" />
 <param name="flashvars" value="type=http&file=http://192.168.0.33:8081/2.flv&autostart=true">
 <EMBED SRC="player.swf" WIDTH="480" HEIGHT="360" QUALITY="high" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/i
ndex.cgi?P1_Prod_Version=ShockwaveFlash" flashvars="type=http&file=http://192.168.0.33:8081/2.flv&autostart=true" wmode="opaq
ue" /> 
</object>
</body>
</html>
NGINX-RTMP测试页面index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>nginx-rtmp</title>
</head>

<body>
<div id="myElement"></div>
<object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="480" height="360">
 <param name="movie" value="player.swf" />
 <param name="quality" value="high" />
 <param name="wmode" value="opaque" />
 <param name="swfversion" value="6.0.65.0" />
 <param name="flashvars" value="streamer=rtmp://192.168.0.33:1935/vod&file=2.flv&autostart=true">
 <EMBED SRC="player.swf" WIDTH="480" HEIGHT="360" QUALITY="high" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/i
ndex.cgi?P1_Prod_Version=ShockwaveFlash" flashvars="streamer=rtmp://192.168.0.33:1935/vod&file=2.flv&autostart=true" wmode="o
paque" /> 
</object>
</body>
</html>

页面能打开正常播放就说明配置是正确的。

五、并发测试
NGINX-HTTP采用loadrunner测试(略)
NGINX-RTMP采用开源软件flazr测试:
前提需要安装jdk软件
client.bat rtmp://192.168.0.33:1935/vod/2.flv -load 500
这里的500是并发数

由于局域网的限制,带宽测试到一定大小的时候就上不去了,故此步截图不忽略。

安装相关问题解决:高版本nginx问题:http://lxneng.com/posts/160 https://github.com/arut/nginx-rtmp-module/issues/184

nginx搭建http和rtmp协议的流媒体服务器的更多相关文章

  1. 利用Nginx搭建http和rtmp协议的流媒体服务器[转]

    利用nginx搭建http和rtmp协议的流媒体服务器 实验目的:让Nginx支持flv和mp4格式文件,同时支持Rtmp协议:同时打开rtmp的hls功能资料:HTTPLive Streaming( ...

  2. 利用Nginx搭建http和rtmp协议的流媒体服务器

    http://www.linuxidc.com/Linux/2013-02/79118.htm

  3. nginx搭建支持http和rtmp协议的流媒体server之中的一个

    实验目的:让Nginx支持flv和mp4格式文件,支持RTMP协议的直播和点播:同一时候打开RTMP的HLS功能 ​资料:HTTP Live Streaming(缩写是 HLS)是一个由苹果公司提出的 ...

  4. rtmp与hls流媒体服务器搭建:ubuntu下Nginx搭建初探与rtmp-module的添加

    关键词:Nignx(http服务器):rtmp,hls(流媒体服务) 前言:感谢开源,感谢战斗民族.现在在做流媒体服务的一些工作,流媒体服务器搭建的网上教程多入牛毛,但是细细查看,发现很多同志贴上来的 ...

  5. 基于Nginx+nginx-rtmp-module+ffmpeg搭建rtmp、hls流媒体服务器(二)

    前言 Nginx-rtmp-module插件针对RTMP协议中一些命令,实现了事件通知和exec外部脚本处理.这里我通过一个简单的SpringBoot项目和Python代码,快速搭建一个HTTP服务来 ...

  6. Nginx系列2:用Nginx搭建一个可用的静态资源Web服务器

    上一节中编译好自己的nginx服务器后, 现在要对nginx.conf文件进行配置,搭建一个可用的静态资源Web服务器 1.放入可访问的html文件到nginx文件夹下,如图1所示: 这里我放入的是一 ...

  7. 基于Nginx+nginx-rtmp-module+ffmpeg搭建rtmp、hls流媒体服务器

    上篇文章是基于Red5与ffmpeg实现rtmp处理NVR或摄像头的监控视频处理方案,有兴趣的朋友可以查看. Nginx及nginx-rtmp-module安装 新建目录 mkdir /usr/loc ...

  8. nginx::基于Nginx+nginx-rtmp-module+ffmpeg搭建rtmp、hls流媒体服务器

    待续 ffmpeg -re -i "/home/bk/hello.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 4 ...

  9. 使用nginx搭建一个可用的静态资源web服务器

    新建dlib目录,dlib里面放着很多index.html文件 修改conf文件 配置location,/所有的请求,这里一般使用alias,这样url后面的路径和dlib/下面的路径是一一对应的,如 ...

随机推荐

  1. git提交报异常,fatal: The remote end hung up unexpectedly

    转自:http://liucanwen.iteye.com/blog/2021601 早上提交代码到 oschina代码库时,报了这个错误: fatal: The remote end hung up ...

  2. C++的那些事:表达式与语句

    表达式 1,应该把函数调用当作是一种运算符,这种运算符对参与运算的对象没有数量限制. 2,关于“左值(lvalue)”和“右值(rvalue)”可以做一个简单的归纳:当一个对象被用作右值的时候,用的是 ...

  3. Kinect学习笔记(五)——更专业的深度图

           这一节的内容就是把深度图转换为彩色图,然后不再使用硬解码,而是继续采用sdk里面封装好的功能来减少测量的误差,以及避免转换为灰度图时,出现绿巨人时候的掉针的bug.       下面直接 ...

  4. hbase shell中log4j重复问题

    [root@centos ~]# hbase shellSLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding ...

  5. python 自定义排序函数

    自定义排序函数 Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 ...

  6. Android 编程下短信监听在小米手机中失效的解决办法

    相信很多人写的短信监听应用在小米手机上是拦截不到短信的,这是因为小米对短信的处置权优先分给了系统.我们可以在短信的[设置]→[高级设置]→[系统短信优先]中发现短信的优先处理权默认是分给系统的,只要关 ...

  7. javascript优化--06模式(对象)01

    命名空间: 优点:可以解决命名混乱和第三方冲突: 缺点:长嵌套导致更长的查询时间:更多的字符: 通用命名空间函数: var MYAPP = MYAPP || {}; MYAPP.namespace = ...

  8. 贪心 BestCoder Round #39 1001 Delete

    题目传送门 /* 贪心水题:找出出现次数>1的次数和res,如果要减去的比res小,那么总的不同的数字tot不会少: 否则再在tot里减去多余的即为答案 用set容器也可以做,思路一样 */ # ...

  9. BZOJ3570 : DZY Loves Physics I

    考虑两个质量均为m,速度分别v1.v2的小球发生完全弹性碰撞的影响: 由动能守恒得: $\frac{1}{2}mv_1^2+\frac{1}{2}mv_2^2=\frac{1}{2}mv_1'^2+\ ...

  10. ios cocos2d FPS过低的解决方法

    每当运行程序时,左下角的FPS就低到了10,使app很卡, 原来程序主要卡的部分 -(void)draw{ NSDate *startTime = [NSDate date]; [self func] ...