nginx实现rtmp,flv,mp4流媒体服务器

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

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

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

转载于:https://my.oschina.net/ososchina/blog/833909

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要搭建RTMP流媒体服务,你需要完成以下两个步骤: 1. 安装NginxRTMP模块 2. 配置NginxRTMP模块 下面我将为你详细介绍如何完成这两个步骤。 ## 安装NginxRTMP模块 ### 安装Nginx 首先,你需要安装Nginx。在Ubuntu系统上,可以使用以下命令进行安装: ``` sudo apt-get update sudo apt-get install nginx ``` 安装完成后,你可以使用以下命令验证Nginx是否成功安装: ``` nginx -v ``` 如果成功安装,你应该可以看到Nginx的版本信息。 ### 安装RTMP模块 安装Nginx后,你需要安装RTMP模块。RTMP模块可以让Nginx支持RTMP协议,从而实现流媒体服务。 在Ubuntu系统上,你可以使用以下命令下载RTMP模块: ``` sudo apt-get install libnginx-mod-rtmp ``` 安装完成后,你需要启用RTMP模块。可以使用以下命令启用: ``` sudo ln -s /usr/share/nginx/modules-available/mod-rtmp.conf /etc/nginx/modules-enabled/ sudo ln -s /usr/share/nginx/modules-available/mod-rtmp.load /etc/nginx/modules-enabled/ ``` ## 配置NginxRTMP模块 安装完成NginxRTMP模块后,你需要配置NginxRTMP模块以实现流媒体服务。 ### 配置Nginx 首先,你需要编辑Nginx的配置文件。可以使用以下命令打开默认Nginx配置文件: ``` sudo nano /etc/nginx/nginx.conf ``` 在文件末尾添加以下代码: ``` rtmp { server { listen 1935; chunk_size 4096; application live { live on; record off; } } } ``` 这里我们定义了一个RTMP服务器,它将监听1935端口并支持流媒体服务。在application块中,我们定义了一个名为live的应用程序,它将允许直播,并关闭录制。 ### 启动Nginx 完成Nginx配置后,你需要启动Nginx。可以使用以下命令启动: ``` sudo service nginx start ``` ### 配置推流和拉流 现在,你可以使用推流软件将视频流推送到服务器上。例如,可以使用OBS Studio或FFmpeg进行推流。 在OBS Studio中,你需要配置以下设置: - 流类型:自定义流服务器 - URL:rtmp://your-server-ip:1935/live - 流关键字:随意命名 在FFmpeg中,你可以使用以下命令进行推流: ``` ffmpeg -re -i input.mp4 -c copy -f flv rtmp://your-server-ip:1935/live/stream-name ``` 这里我们将本地input.mp4文件推流到服务器上,流名称为stream-name。 完成推流后,你可以使用以下命令进行拉流: ``` ffplay rtmp://your-server-ip:1935/live/stream-name ``` 这里我们使用ffplay命令进行拉流。你也可以使用其他支持RTMP协议的播放器进行拉流。 到这里,你已经成功搭建了RTMP流媒体服务。祝你好运!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值