由于最近公司项目需要,所以经过各种百度,nginx流媒体服务器的架设方法,先就我自己经过1天半的实验,安装步骤如下:

准备工作:

1、安装vmware虚拟机

2、安装centos6.0系统

3、架设lnmp服务器

4、采用nginx升级的方式安装nginx流媒体服务器(因为我们公司生产服务器上面就是nginx,已经正式使用,所以采用升级新增nginx模块的方式安装的)

 

对于已安装好的nginx,需要新增模块,我们可以重新编写安装,
1、先使用 nginx -V 查看原来编译都带了那些参数

nginx -V

查看是否安装了mp4模块的支持,如果没有,就先安装MP4模块

#添加mp4支持模块

      
    wget http://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
      cd nginx_mod_h264_streaming-2.2.7
      git clone git://github.com/arut/nginx-rtmp-module.git


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

安装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 ..
安装ffmpeg
https://ffmpeg.org/download.html">https://ffmpeg.org/download.htmltar -jvxf ffmpeg-3.0.1.tar.bz2
cd  ffmpeg-3.0.1
./configure  --prefix=/usr/local/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


修改/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的源码目录,进行nginx的编译安装

./configure --prefix=/usr/local/nginx  --add-module=/usr/local/nginx_mod_h264_streaming-2.2.7 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_mp4_module --add-module=/usr/local/nginx-rtmp-module  --with-cc-opt=-I/usr/local/ffmpeg/include --with-ld-opt='-L/usr/local/ffmpeg/lib -Wl,-rpath=/usr/local/ffmpeg/lib'


3、编译

    make

    注意,这里就不要make install 了,否则就是覆盖安装了

4、替换nginx 二进制文件

先备份 
 cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

在拷贝新的二进制文件
cp ./objs/nginx /usr/local/nginx/sbin/



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

                    #下载yadmi

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

                   #安装yadmi

           tar xzvf yamdi-1.4.tar.gz

                   cd yamdi-1.4

           make && make install

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

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

6、修改nginx配置


user  www www;

worker_processes auto;

error_log  /home/wwwlogs/nginx_error.log  crit;

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

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }

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;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

        server_tokens off;
        #log format
        log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
        access_log off;



server {

        listen       80;

        server_name  localhost;

        root    /home/wwwroot/default/media/;

        limit_rate_after 5m;    ####在flv视频文件下载了5M以后开始限速
        limit_rate 512k;         ####速度限制为512K

        index   index.html;

        charset utf-8;

        location ~ \.flv {

            flv;

        }

        location ~ \.mp4$ {
                 mp4;
        }



}

}




重启nginx服务:
下载jwplayer的播放器:

编写测试页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>后台管理系统登录</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">

<script src="jwplayer/jquery-1.8.1.min.js" type="text/javascript"></script>  
<script src="jwplayer/jwplayer.js" type="text/javascript"></script>  
<script type="text/javascript">jwplayer.key="+iYXuspbJGjMrCdDoq09NROARMASj+nhqP6pAw==";</script>
<body>
<div id="container"></div>  
<input type="button" class="player-play" value="播放" />  
<input type="button" class="player-stop" value="停止" />  
<input type="button" class="player-status" value="取得状态"  />   
<input type="button" class="player-current" value="当前播放秒数" />   
<input type="button" class="player-goto" value="转到第30秒播放" />  
 <input type="button" class="player-length" value="视频时长(秒)" />  

</body>

</HTML>

<script type="text/javascript">  
    var thePlayer;  //保存当前播放器以便操作  
    $(function() {  
        thePlayer = jwplayer('container').setup({  
            flashplayer: 'jwplayer/jwplayer.flash.swf',  
            file: 'video/test6.mp4',  
            width: 500,  
            height: 350,  
            dock: false  
        });  
          
        //播放 暂停  
        $('.player-play').click(function() {  
            if (thePlayer.getState().toUpperCase() != 'PLAYING') {  
                thePlayer.play(true);  
                this.value = '暂停';  
            } else {  
                thePlayer.play(false);  
                this.value = '播放';  
            }  
        });  
          
        //停止  
        $('.player-stop').click(function() { thePlayer.stop(); });  
          
        //获取状态  
        $('.player-status').click(function() {  
            var state = thePlayer.getState();  
           state = state.toUpperCase();
            var msg;  
            switch (state) {  
                case 'BUFFERING':  
                    msg = '加载中';  
                    break;  
                case 'PLAYING':  
                    msg = '正在播放';  
                    break;  
                case 'PAUSED':  
                    msg = '暂停';  
                    break;  
                case 'IDLE':  
                    msg = '停止';  
                    break;  
            }  
            alert(msg);  
        });  
          
        //获取播放进度  
        $('.player-current').click(function() { alert(thePlayer.getPosition()); });  
  
        //跳转到指定位置播放  
        $('.player-goto').click(function() {  
            if (thePlayer.getState().toUpperCase() != 'PLAYING') {    //若当前未播放,先启动播放器  
                thePlayer.play();  
            }  
            thePlayer.seek(30); //从指定位置开始播放(单位:秒)  
        });  
          
        //获取视频长度  
        $('.player-length').click(function() { alert(thePlayer.getDuration()); });  
    });  
</script>



注意,这是就可以播放mp4,flv的视频格式了,如果不能够拖拽,就使用工具添加关键帧:

可以使用“格式工厂”对视频做编码转换 h262格式