nginx实现rtmp,flv,mp4流媒体服务器
博客专区 > IamOkay 的博客 > 博客详情
nginx实现rtmp,flv,mp4流媒体服务器
IamOkay 发表于11个月前
nginx实现rtmp,flv,mp4流媒体服务器
  • 发表于 11个月前
  • 阅读 841
  • 收藏 5
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: nginx实现支持rtmp协议服务器

过了个年,突然忘了很多事情,可能我真是上了年龄。

一.安装编译时说需要的扩展

yum install automake autoconf make gcc gcc-c++ 

 

二.安装nginx-rtmp-module

2.1第一种方式比较简单

yum install pcre pcre-devel  
yum install zlib zlib-devel  
yum install openssl openssl--devel  

然后下载nginx,nginx-rtmp-module模块,并解压

git clone https://github.com/arut/nginx-rtmp-module.git
 wget http://nginx.org/download/nginx-1.7.4.tar.gz 

解压略(我们这里假定解压目录在 /usr/tmp中)

注册并安装nginx

cd nginx-1.7.4

#../configure --prefix=/usr/local/nginx 
--with-http_stub_status_module[可选] 
--with-http_ssl_module[可选] 
--with-http_gzip_static_module[可选] 
--with-http_flv_module[可选] 
--with-http_mp4_module  [可选]
--add-module=/usr/tmp/nginx-rtmp-module-master
#make

#make install

注:如果使用flv,MP4点播的话,要注意nginx 1.1.3之后已经默认支持mp4,flv模块,无须第三方模块支持,如果这里不配置,那么mp4和flv视频点播播放将会很慢

2.2第二种方式

需要下载的阔扩展():


   1. wget http://www.openssl.org/source/openssl-0.9.8l.tar.gz (系统默认带有openssl)
   2. wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.00.tar.bz2
   3. wget http://www.zlib.net/zlib-1.2.3.tar.bz2
   4. git clone https://github.com/arut/nginx-rtmp-module.git
   5. wget http://nginx.org/download/nginx-1.7.4.tar.gz 

把这些玩意都解压缩后,就会有(假定解压目录/usr/tmp/):

   1. openssl-0.9.8l
   2. pcre-8.00
   3. zlib-1.2.3
   4. nginx-rtmp-module-master
   5. nginx-1.7.4

注册并安装nginx

cd nginx-1.7.4

#./configure --prefix=/usr/local/nginx 
--with-http_realip_module 
--with-http_sub_module 
--with-http_flv_module 
--with-http_mp4_module  
--with-http_dav_module 
--with-http_gzip_static_module 
--with-http_stub_status_module 
--with-http_addition_module 
--with-pcre=/usr/tmp/pcre-8.00 
--with-openssl=/usr/tmp/openssl-0.9.8l 
--with-http_ssl_module --with-zlib=/usr/tmp/zlib-1.2.3 
--add-module=/usr/tmp/nginx-rtmp-module

#make

#make install

三.配置nginx.conf

rtmp {

    server {

        listen 1935;

        chunk_size 4000;

        # TV mode: one publisher, many subscribers 电视直播模式,1对多
        application mytv {

            # enable live streaming
            live on;

            # record first 1K of stream
            record all;
            record_path /tmp/av;
            record_max_size 1K;

            # append current timestamp to each flv
            record_unique on;

            # publish only from localhost
            allow publish 127.0.0.1;
            deny publish all;

            #allow play all;
        }

        # Transcoding (ffmpeg needed)
        application big {
            live on;

           #将推过来的媒体文件流转码至small

            exec ffmpeg -re -i rtmp://localhost:1935/$app/$name -vcodec flv -acodec copy -s 32x32
                        -f flv rtmp://localhost:1935/small/${name};
        }

        application small {
            live on; 
            # Video with reduced resolution comes here from ffmpeg
        }

        application webcam {
            live on;

            # Stream from local webcam
            exec_static ffmpeg -f video4linux2 -i /dev/video0 -c:v libx264 -an
                               -f flv rtmp://localhost:1935/webcam/mystream;
        }

        application mypush {
            live on;

            # Every stream published here
            # is automatically pushed to
            # these two machines
            push rtmp1.example.com;
            push rtmp2.example.com:1934;
        }

        application mypull {
            live on;
#拉流
            # Pull all streams from remote machine
            # and play locally
            pull rtmp://rtmp3.example.com pageUrl=www.example.com/index.html;
        }

        application mystaticpull {
            live on;

            # Static pull is started at nginx start
            pull rtmp://rtmp4.example.com pageUrl=www.example.com/index.html name=mystream static;
        }

        # video on demand
#点播
        application vod {
            play /var/flvs;
        }
     
        application vod2 {
            play /var/mp4s;
        }

        # Many publishers, many subscribers
        # no checks, no recording
#视频聊天
        application videochat {

            live on;

            # The following notifications receive all
            # the session variables as well as
            # particular call arguments in HTTP POST
            # request

            # Make HTTP request & use HTTP retcode
            # to decide whether to allow publishing
            # from this connection or not
            on_publish http://localhost:8080/publish;

            # Same with playing
            on_play http://localhost:8080/play;

            # Publish/play end (repeats on disconnect)
            on_done http://localhost:8080/done;

            # All above mentioned notifications receive
            # standard connect() arguments as well as
            # play/publish ones. If any arguments are sent
            # with GET-style syntax to play & publish
            # these are also included.
            # Example URL:
            #   rtmp://localhost/myapp/mystream?a=b&c=d

            # record 10 video keyframes (no audio) every 2 minutes
            record keyframes;
            record_path /tmp/vc;
            record_max_frames 10;
            record_interval 2m;

            # Async notify about an flv recorded
            on_record_done http://localhost:8080/record_done;

        }


        # HLS

        # For HLS to work please create a directory in tmpfs (/tmp/hls here)
        # for the fragments. The directory contents is served via HTTP (see
        # http{} section in config)
        #
        # Incoming stream must be in H264/AAC. For iPhones use baseline H264
        # profile (see ffmpeg example).
        # This example creates RTMP stream from movie ready for HLS:
        #
        # ffmpeg -loglevel verbose -re -i movie.avi  -vcodec libx264
        #    -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
        #    -f flv rtmp://localhost:1935/hls/movie
        #
        # If you need to transcode live stream use 'exec' feature.
        #
        application hls {
            live on;
            hls on;
            hls_path /tmp/hls;
        }

        # MPEG-DASH is similar to HLS

        application dash {
            live on;
            dash on;
            dash_path /tmp/dash;
        }
    }
}

# HTTP can be used for accessing RTMP stats
http {

    server {

        listen      8080;

        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;

            # Use this stylesheet to view XML as web page
            # in browser
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl { #状态文件
            # XML stylesheet to view RTMP stats.
            # Copy stat.xsl wherever you want
            # and put the full directory path here
            root /path/to/stat.xsl/; #注意root指令的用法
        }

        location /hls {
            # Serve HLS fragments 支持Hls直播,注意,nginx本身支持hls点播,这里是直播
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        }

        location /dash {
            # Serve DASH fragments
            root /tmp;
            add_header Cache-Control no-cache;
        }

    location ~ \.flv$                      ---------匹配flv文件 
    { 
        flv; 
    } 
  
    location ~ \.mp4$                    ----------匹配mp4文件 
    { 
     mp4; 
#    mp4_buffer_size    1m;        
#    mp4_max_buffer_size  5m; 
     limit_rate_after 5m;                ---------下载5m后再限速 
     limit_rate 100k;                    ----------限速100k 
#    limit_conn perip 1; 
    }
    }
}

 四.安装到此结束,其

中有个big->small转换有些问题,需要安装ffmpeg才行,如果要装ffmpeg请参考

流媒体:在CentOS 7 安装ffmpeg流媒体工具

 

五.附录

[终极精简版][图解]Nginx搭建flv mp4流媒体服务器

nginx搭建支持http和rtmp协议的流媒体服务器之一 

nginx搭建支持http和rtmp协议的流媒体服务器之二 

nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器
http://blog.csdn.net/redstarofsleep/article/details/45092147
http://www.cnblogs.com/cheng5x/p/5646920.html
http://blog.creke.net/801.html

共有 人打赏支持
粉丝 169
博文 425
码字总数 323523
×
IamOkay
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: