nginx +rtmp 流媒体服务器搭建 + SRS流媒体服务器搭建 +ffmpeg

部署nginx+rtmp 的流媒体服务器
##yum 安装nginx 版本号为nginx 1.12.2
yum install nginx -y

下载nginx-rtmp-module模块
git clone https://github.com/arut/nginx-rtmp-module.git
下载nginx-1.12.2.tar.gz
wget http://nginx.org/download/nginx-1.12.2.tar.gz 
tar -zxvf nginx-1.12.2.tar.gz 
cd nginx.1.12.2
编译
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module  --with-debug --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'  --add-module=/root/nginx-rtmp-module
编译过程报错处理

更换这个
--with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector \ 
--param=ssp-buffer-size=4 -m64 -mtune=generic' \

更换为
--with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
安装依赖库
yum -y install libxml2 libxml2-dev libxslt-devel 
yum -y install gd-devel 
yum -y install perl-devel perl-ExtUtils-Embed 
yum -y install GeoIP GeoIP-devel GeoIP-data
yum -y install pcre-devel
yum -y install openssl openssl-devel
安装
make && make install
重启nginx
systemctl nginx restart
查看安装模块rtmp是否添加成功
nginx -V


nginx配置文件添加
rtmp {
server {
   listen 1935; #监听的端口
   chunk_size 4000;
   application cctvf {#rtmp推流请求路径 (切记路径错了会推不上流)
   live on; #开启实时
   hls on; #开启hls
   hls_path /usr/local/src/nginx/html/cctvf; #rtmp推流请求路径,文件存放路
   hls_fragment 5s; #每个TS文件包含5秒的视频内容
 }
}
}

推流测试
obc软件推流

拉流测试
vlc播放器测试拉流


SRS流媒体搭建
使用centos6系统安装
在http://www.ossrs.net/srs.release/releases/ 下下载srs
安装
修改配置文件sys.conf
listen              1935;
max_connections     200;
srs_log_tank        file;
srs_log_file        ./objs/srs.log;
 
http_api {
    enabled         on;
    listen          1985;
}
 
http_server {
    enabled         on;
    listen          8080;
    dir             ./objs/nginx/html;
}
 
stats {
    network         0;
    disk            sda sdb xvda xvdb;
}
 
vhost __defaultVhost__ {
    # http-flv设置
    http_remux{
        enabled    on;
        mount      [vhost]/[app]/[stream].flv;
        hstrs      on;
    }
 
    # hls设置
    hls{
        enabled       on;
        hls_path      ./objs/nginx/html;
        hls_fragment  10;
        hls_window    60;
    }
}

./objs/srs -c conf/srs.conf

保存流媒体文件到本地
安装ffmpeg
cd /root
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
编译安装
./configure --prefix=host --enable-shared --disable-static --disable-doc 
make&&make install

可能遇到报错
  ./ffmpeg: error while loading shared libraries: libavdevice.so.57: cannot open shared object file: No such file or directory

配置
vim /etc/ld.so.conf.d/ffmpeg.conf
##添加lib路径
/root/ffmpeg/host/lib
然后执行  ldconfig 使配置生效
执行/root/ffmpeg/host/bin/ffmpeg -version查看是否安装成功

监控并保存视频流
nohup /root/ffmpeg/host/bin/ffmpeg -i rtmp://localhost:1935/cctvf/mystream -c copy dump-`date +%F`.flv & 

 

  • 使用python 保存接受rtmp流 并按时间段保存为flv格式视频
  • 主代码
  • #!/usr/bin/python
    import os
    import time
    
    class stream:
        '定义流监控'
        def __init__(self,streamCode,rtmpUrl):  #流码,rtmp url
            self.streamCode=streamCode
            self.rtmpUrl=rtmpUrl
            ##启动接受rtmp流
        def startStreamMonitor(self):
            os.popen('nohup /root/ffmpeg/host/bin/ffmpeg -i ' +self.rtmpUrl+'/'+self.streamCode + ' -c copy  ' +self.streamCode+'-`date +%F_%T`.flv >/dev/null 2>&1 &')
             ##获取pid
        def getProcessPid(self):
            try:
                p = os.popen("ps -ef|grep %s/%s|grep -v 'grep'|awk -F ' ' '{print $2}'" %(self.rtmpUrl,self.streamCode) )
                line = p.readlines()
                if line:
                    pid = line[0]
                    pid = pid.strip()
                    return pid
            except IndexError as e:
                print(str(e))
    
    
           #关闭流监控
        def stopStreamMonitor(self):
            pid=self.getProcessPid()
            os.popen('kill -9 %s' %pid)
    
    if __name__=='__main__':
        rtmpUrl='rtmp://localhost:1935/cctvf'
        flag=True
        flag_stop_stream=True
        stream_period=3600*24
        while flag:
            list_stream = []
            ##读取配置文件
            with open('stream.conf','r') as f:
                for item in f:
                    if item.startswith('list_streamCode'):
                        list_streamCode=eval(item.split(':')[1])
                    if item.startswith('stream_period'):
                        stream_period=int(eval(item.split(':')[1].strip()))
                 ###实例化所有的trmp对象,并启动接受,并保存为flv格式
            for i in list_streamCode:
              streamInstance='stream'+i
              streamInstance=stream(i,rtmpUrl)
              ##将对象存储在list中
              list_stream.append(streamInstance)
            if flag_stop_stream:
                for item in list_stream:
                    if item.getProcessPid():
                       item.stopStreamMonitor()
    
                flag_stop_stream=False
            for item in  list_stream:
                 item.startStreamMonitor()
            ##视频文件时长
            time.sleep(stream_period)
            ##关闭所有rtmp接受
            for item in list_stream:
                  item.stopStreamMonitor()

    配置文件

  • ###推流码
    list_streamCode: ['mystream','123456','mystream2','mystream3']
    ##单位秒
    stream_period: 60*60
    

     

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值