您的位置:首页 > 运维架构 > Nginx

nginx搭建流媒体服务器

2015-12-02 16:11 447 查看
申明:该文借鉴网上博客(出处忘记了)+自己整理,配置测试成功

环境;centos5.11

一、FLV视频发布方式简介

FLV视频有两总发布方式

1、  HTTP方式

这种方式要下载FLV视频文件到本地播放,一旦FLV视频文件下载完成,就不会消耗服务器的资源和带宽,但是拖动功能没有RTMP/RTMP流媒体方式强大,很多视频网站都是用HTTP方式实现的,如:YouTube,土豆,酷6等

2、  RTMP/RTMP流媒体方式

这种方式不用下载FLV视频文件到本地,可以实时的播放flv文件,可以任意拖拽播放进度条,但是比较消耗服务器的资源,

 

 

二、使用nginx来搭建flv流媒体服务器

1、使用nginx来搭建flv流媒体服务器简介

nginx中的Flv Stream模块能实现flv流媒体的功能,而且支持flv视频进度条拖拽,另外nignx还可以作为方向代理服务器代理后端基于Flash Media Server或者Red5的RTMP/RTMP流媒体服务器

 

2、  下面我们就来搭建一个完整的nginx流媒体服务器

# vim/etc/ld.so.conf.d/local-libraries.conf

       /usr/local/lib

下载地址:http://softlayer-dal.dl.sourceforge.net/project/faac

#tar zxf faac-1.28.tar.gz

#cd faac-1.28

#./configure --prefix=/usr/local/ --enable-shared

#make && make install

#tar zxf lame-3.98.4.tar.gz

#cd lame-3.98.4

#./configure --prefix=/usr/local/ --enable-shared

#make && make install

#tar zxf opencore-amr-0.1.3.tar.gz

#cd opencore-amr

#./configure --prefix=/usr/local/ --enable-shared

#make && make install

yasm(x264 configure need)

#tar zxf yasm-1.3.0.tar.gz

#cd yasm-1.3.0

#./configure --prefix=/usr/local/

#make && make install

faad2

# tar -xzvf faad2-2.7.tar.gz -C /usr/local/src

#./configure --prefix=/usr/local--enable-shared

#make && make install

x264

wget ftp://ftp.videolan.org/pub/x264/snapshots/last_x264.tar.bz2

#tar jxf last_x264.tar.bz2

#cd x264-snapshot-20141124-2245

#./configure --prefix=/usr/local --enable-shared

#make && make install

需注意

ogg

#wget http://downloads.xiph.org/releases/ogg/libogg-1.3.2.tar.xz
#xz -d libogg-1.3.2.tar.xz

#tar xf libogg-1.3.2.tar

#cd libogg-1.3.2

#./configure --prefix=/usr/local --enable-shared

#make && make install

xvid

#wget http://downloads.xvid.org/downloads/xvidcore-1.3.3.tar.gz
#tar zxf xvidcore-1.3.3.tar.gz

#cd xvidcore/build/generic/

#./configure --prefix=/usr/local

#make && make install

Libvpx

#git clonehttp://git.chromium.org/webm/libvpx.git 

#cd libvpx 

#./configure  --enable-shared 

#make 

#make install 

Libvorbis

#wgethttp://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

nasm,

#tar -xjvf nasm-2. 10.05.tar.bz2 -C /usr/local/src

#./configure --prefix=/usr/local--enable-shared

#make && make install

Ffmpeg

3gp,

源码网址:http://www.3gpp.org/ftp/Specs

包括两个包:amrwb_float下载amr_float下载,安装:解压ffmpeg的源码包后,进入ffmpeg-checkout-20070130/libavcodec/,新建两个新目录
amrwb_float和amr_float,然后解压这两个包,把amrwb_float里面的所有文件复制到amrwb_float,把 amr_float的所有文件复制到amr_float

wget http://ffmpeg.org/releases/ffmpeg-2.8.2.tar.bz2
 # tar -xjvf ffmpeg-2.8.2.tar.bz2 -C/usr/local/src

#cd /usr/local/src/ffmpeg-2.8.2/libavcodec

#mkdir amrwb_fload

#mkdir amr_fload

# cd /usr/local/src/ffmpeg-2.8.2

#./configure --prefix=/usr/local/--enable-libxvid --enable-libx264 --enable-libmp3lame --enable-libvorbis--enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libfaac --enable-gpl --enable-version3 --enable-nonfree --enable-shared --enable-zlib--enable-bzlib
--enable-libvorbis

#make#wait about 10 minutes

#make install

#ldconfig –v

#ffmpeg -version

 修改/etc/ld.so.conf如下: 

#vim/etc/ld.so.conf

include ld.so.conf.d/*.conf 

/lib 

/lib64 

/usr/lib 

/usr/lib64 

/usr/local/lib 

/usr/local/lib64 

/usr/local/ffmpeg/lib 

 

1)、Nginx服务器的安装

                     #安装zlib

#tar xzvf zlib-1.2.7.tar.gz -C /usr/local/src

                     #cd/usr/local/src/zlib-1.2.7

                     #./configure–prefix=/usr/local/zlib

                   #  make&& make install

                    #安装pcre

                     #tarzxvf pcre-8.36.tar.gz

                     #cdpcre-8.36

                    # ./configure--prefix=/usr/local/pcre

                   #  make&& make install

                    #添加mp4支持模块

                   # wgethttp://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz

                     #tar -zxvf nginx_mod_h264_streaming-2.2.7.tar.gz -C /usr/local/src/

                     #安装nginx

#groupadd www

                   #  useradd -g www www

#wget http://www.nginx.eu/download/sources/nginx-0.7.9.tar.gz              #nginx不能太新

                  #tarxzvf nginx-0.7.9.tar.gz

                #  cdnginx-0.7.9

                #         ./configure--add-module=/usr/local/src/nginx_mod_h264_streaming-2.2.7--with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.36--with-zlib=/usr/local/src/zlib-1.2.7 --user=www --group=www--prefix=/usr/local/nginx--with-http_flv_module --with-http_stub_status_module

  # make && make install

../nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c:158:错误:‘ngx_http_request_t’ 没有名
a2a3
为 ‘zero_in_uri’ 的成员

make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] 错误 1

make[1]: Leaving directory `/usr/local/src/nginx-1.9.7'

make: *** [build] 错误 2

解决办法:

      修改ngx_http_streaming_module.c,注释掉

 if (r->zero_in_uri)   { 

    return NGX_DECLINED;  

 } 

#vim /usr/local/src/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c

 if(r->zero_in_uri)   { 

    returnNGX_DECLINED;  

 } 

改为

/*

 if(r->zero_in_uri)   { 

    returnNGX_DECLINED;  

 } 

*/

 #         ./configure--add-module=/usr/local/src/nginx_mod_h264_streaming-2.2.7--with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.36--with-zlib=/usr/local/src/zlib-1.2.7 --user=www --group=www --prefix=/usr/local/nginx--with-http_flv_module --with-http_stub_status_module

#yum install git

git clonegit://github.com/arut/nginx-rtmp-module.git 

#./configure--prefix=/usr/local/nginx  --add-module=/usr/local/src/nginx_mod_h264_streaming-2.2.7 --with-http_ssl_module  --with-pcre=/usr/local/src/pcre-8.36--with-http_flv_module  --with-zlib=/usr/local/src/zlib-1.2.7--user=www
--group=www --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/usr/local/ffmpeg/include--with-ld-opt='-L/usr/local/ffmpeg/lib -Wl,-rpath=/usr/local/ffmpeg/lib' 

  # make && make install

 

2)、安装yamdi

        yadmi的作用是为flv文件添加关键帧,才能实现拖动播放

                   #下载yadmi

                  wget http://sourceforge.net/projects/yamdi/files/yamdi/1.4/yamdi-1.4.tar.gz/download

                  #安装yadmi

           tarxzvf yamdi-1.4.tar.gz

                  cd yamdi-1.4

           make &&make install

      使用方法:yamdi -i input.flv -o out.flv

      给input.flv文件 添加关键帧,输出为out.flv文件

         3)、配置nginx

               vi/usr/local/nginx/conf/nginx.conf 添加以下内容(根据自身情况修改):

user  www www;

worker_processes 30;

error_log  /usr/local/nginx/logs/error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

 

events {

        use epoll;

        worker_connections      65535;

        }

http {

        include       mime.types;

        default_type  application/octet-stream;

        log_formatmain  '$remote_addr - $remote_user [$time_local] '

                                                '"$request"$status $bytes_sent '

                                                '"$http_referer""$http_user_agent" '

                                                '"$gzip_ratio"';

        keepalive_timeout  60;

        server_names_hash_bucket_size  128;

        client_header_buffer_size    32k;

 

        large_client_header_buffers  432k;

 

        access_log off;

        gzip on;

        gzip_min_length  1100;

        gzip_buffers     48k;

        gzip_types       text/plain;

 

        output_buffers   132k;

        postpone_output  1460;

 

        client_header_timeout  3m;

        client_body_timeout    3m;

        send_timeout           3m;

 

        sendfile                on;

        tcp_nopush              on;

        tcp_nodelay             on;

 

######################################################################

 

server {

        listen       80;

        server_name  192.168.1.105;

        root    /usr/local/nginx/html/flv_file/;

        limit_rate_after5m;    ####在flv视频文件下载了5M以后开始限速

        limit_rate512k;         ####速度限制为512K

        index   index.html;

        charset utf-8;

        location ~ \.flv {

            flv;

        }

        location ~ \.mp4$ {

                mp4;

        }

        error_page   500502 503 504  /50x.html;

        location =/50x.html {

            root   html;

        }

 

}

}

 

4)、基本上已经设置完毕,但是此时我们测试的时候还需要一个支持拖拽播放的flash播放器,开源的JW Player就可以实现这样的功能,我将编译的播放器上传上来,供大家下载:

       下载链接:http://blogimg.chinaunix.net/blog/upfile2/100607142612.rar

       下载播放器后,上传到上面设置的/usr/local/nginx/html/flv_file/目录下,闭关把flv视频文件也放到该目录下!

 

5)、启动nginx后测试:

#/usr/local/nginx/conf/nginx -t             #测试配置

#/usr/local/nginx/conf/nginx                #启动

#ps -ef | grep"nginx: master process" | grep -v "grep" | awk -F ' ''{print $2}'

#kill -9 16450               #结束进程

       http://192.168.100.200/player.swf?type=http&file=zimu3.flv

   http://192.168.100.200/player.swf?type=rtmp&file=zimu3.flv

            说明: #我的ip是192.168.1.105

                          #player.swf是我的JW Player播放器

                          #http是表示居于http分发方式

                          #test1.flv是我的flv视频文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息