nginx+rtmp+ffmpeg流媒体在线播放

模块:nginx_mod_h264_streaming(支持h264编码的视频) 下载地址

注意:先要修改一下这里的源码,注释掉
nginx_mod_h264_streaming-2.2.7/src/ngxhttpstreaming_module.c的158到161行

     /* TODO: Win32 */
     //if (r->zero_in_uri)
     // {
     //   return NGX_DECLINED;
     // }

模块:http_flv_module 支持flv 下载地址
模块:http_mp4_module 支持mp4 下载地址

安装依赖包:

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

安装Yasm,x264,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 https://github.com/webmproject/libvpx  
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 --enable-gpl --enable-libx264 --enable-nonfree
make && make install  
cd ..  

然后编译安装支持flv和mp4支持的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

如果都没问题,那基本上就ok了,编辑nginx配置文件,修改下nginx和rtmp的配置:

#user  nobody;
worker_processes  1;

error_log  logs/error.log;  
#access_log  /usr/local/nginx/logs/access.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

        use epoll;

        worker_connections    65535;

        }



http {  
        include       mime.types;
        default_type  application/octet-stream;
        log_format main         '$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  4 32k;
        access_log on;
        gzip on;
        gzip_min_length  1100;
        gzip_buffers     4 8k;
        gzip_types       text/plain;
        output_buffers   1 32k;
        postpone_output  1460;
        client_header_timeout  3m;
        client_body_timeout    3m;
        send_timeout           3m;
        sendfile                on;
        tcp_nopush              on;
        tcp_nodelay             on;




    server {
        listen       8080;
        server_name  mi.y500.me;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;


        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /stat {
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
                root /mnt/soft/nginx-rtmp-module-master;
        }

        location /control {
                rtmp_control all;
        }

        location ~ \.mp4$ {
                 root /usr/mp4;
                 mp4;
                 limit_rate 200k;
        }

        location / {
                root /root/rtmp/nginx-rtmp-module/test/rtmp-publisher;
        }

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        }

    }

}

rtmp {  #保存所有rtmp配置的块  
    server {  #声明一个rtmp实例
        listen 1935;  #给Nginx添加一个监听端口以接收rtmp连接
        chunk_size 4096;  #流整合的最大的块大小。默认值为4096。这个值设置的越大CPU负载就越小。这个值不能低于128
        application mp4 {  #app的名字为mp4
                live on;  #切换直播模式,即一对多广播
                play /usr/mp4;  #播放指定mp4文件目录
        }

        application myapp {
            live on;
        }
        application hls {
            live on;
            hls on;
            hls_path /tmp/hls;
        }
    }
}

然后把视频文件放到/usr/mp4/下面就可以了,访问http://mi.y500.me:8080/test.mp4就可以播放了,这里有个问题,就是只有声音,没有画面,这个因为视频格式的问题,需要用ffmpeg把视频转成x264的格式:ffmpeg -i test.mp4 -vcodec libx264 output.mp4,然后再访问http://mi.y500.me:8080/output.mp4就可以了