在centos服务器和mac上搭建rtmp推流服务

栏目: 服务器 · Nginx · 发布时间: 6年前

内容简介:我们接下来要做的是rtmp推流功能,第一步就是需要会搭建一个rtmp服务被用于推流,所以下面介绍了两种最常用的rtmp推流服务搭建方式:nginx编译依赖gcc环境nginx的http模块使用pcre来解析正则表达式

我们接下来要做的是rtmp推流功能,第一步就是需要会搭建一个rtmp服务被用于推流,所以下面介绍了两种最常用的rtmp推流服务搭建方式:

  • 服务端搭建nginx用于远程推流服务(在云主机搭建,随时随地可以推流)
  • 为了避免外网环境差等因素,在本地mac终端搭建nginx用于推流

centos服务器搭建rtmp推流服务

安装gcc

nginx编译依赖gcc环境

yum -y install gcc gcc-c++
复制代码

安装pcre pcre-devel

nginx的http模块使用pcre来解析正则表达式

yum install -y pcre pcre-devel
复制代码

安装zlib

nginx使用zlib对http包的内容进行gzip

yum install -y zlib zlib-devel
复制代码

安装open ssl

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel
复制代码

下载并解压nginx-rtmp-model

#下载rtmp包
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
#解压下载包(centos中默认没有unzip命令,需要yum下载)
unzip -o master.zip
#修改文件夹名
mv nginx-rtmp-module-master nginx-rtmp-module
复制代码

安装nginx

#下载nginx
wget http://nginx.org/download/nginx-1.13.8.tar.gz
#解压nignx 
tar -zxvf nginx-1.13.8.tar.gz
#切换到nginx中
cd nginx-1.13.8
#生成配置文件,将上述下载的文件配置到configure中
./configure --prefix=/usr/local/nginx  --add-module=/home/nginx-rtmp-module  --with-http_ssl_module
#编译程序
make
#安装程序
make install
#查看nginx模块
nginx -V
复制代码

修改配置nginx

vi /usr/local/nginx/conf/nginx.conf
复制代码
#工作进程
worker_processes  1;
#事件配置
events {
    worker_connections  1024;
}
#RTMP配置
rtmp {  
    server {  
        #监听端口
        listen 1935;  
        #
        application myapp {  
            live on;  
        }  
        #hls配置
        application hls {  
            live on;  
            hls on;  
            hls_path /tmp/hls;  
        }  
    }  
}  

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        #配置hls
        location /hls {  
            types {  
                application/vnd.apple.mpegurl m3u8;  
                video/mp2t ts;  
            }  
            root /tmp;  
            add_header Cache-Control no-cache;  
        }  
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
复制代码

启动nginx

/usr/local/nginx/sbin/nginx
复制代码

推送rtmp流

ffmpeg -re -i "/home/123.mp4" -vcodec libx264 -vprofile baseline -acodec aac  -ar 44100 -strict -2 -ac 1 -f flv -s 640x480 -q 10 rtmp://localhost:1935/myapp/test1
复制代码

mac上搭建rtmp服务器

安装homebrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
复制代码

安装nginx

brew tap denji/nginx

brew install nginx-full --with-rtmp-module
复制代码

安装过程中会提示需要安装xcode command line tool,Mac最新场景下安装Xcode时已经没有Command Line了,需要单独安装。根据提示在使用命令xcode-select --install

在apple官网下载对应的版本并安装 developer.apple.com/download/mo…

我当前的开发环境是:

  • macOS Mojave-10.14.5
  • XCode version-10.2.1

所以下载的版本对应如下图所示:

在centos服务器和mac上搭建rtmp推流服务

查看nginx安装信息:

brew info nginx-full
复制代码

显示如下:

==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

- Tips -
Run port 80:
 $ sudo chown root:wheel /usr/local/Cellar/nginx-full/1.17.1/bin/nginx
 $ sudo chmod u+s /usr/local/Cellar/nginx-full/1.17.1/bin/nginx
Reload config:
 $ nginx -s reload
Reopen Logfile:
 $ nginx -s reopen
Stop process:
 $ nginx -s stop
Waiting on exit process
 $ nginx -s quit

To have launchd start denji/nginx/nginx-full now and restart at login:
  brew services start denji/nginx/nginx-full
Or, if you don't want/need a background service you can just run:
  nginx

复制代码

nginx安装位置:

/usr/local/Cellar/nginx-full/
复制代码

nginx配置文件位置:

/usr/local/etc/nginx/nginx.conf
复制代码

nginx服务器根目录位置:

/usr/local/var/www
复制代码

验证是否安装成功,执行命令:

nginx
复制代码

然后浏览器中输入http://localhost:8080,出现以下界面,证明安装成功

在centos服务器和mac上搭建rtmp推流服务

配置nginx

nginx 的配置文件在 /usr/local/etc/nginx 目录下,选择编辑器打开 nginx.conf 文件,在http节点后面添加rtmp配置。

http{
    ...
}

#在http节点后面加上rtmp配置
rtmp {
    server {
      # 监听端口
      listen 1935;
      # 分块大小
      chunk_size 4000;

      # RTMP 直播流配置
      application rtmplive {
        # 开启直播的模式
        live on;
        # 设置最大连接数
        max_connections 1024;
      }

      # hls 直播流配置
      application hls {
        live on;
        hls on;
        # 分割文件的存储位置
        hls_path /usr/local/var/www/hls;
        # hls分片大小
        hls_fragment 5s;
      }

    }
}

复制代码

在http节点内的server节点内增加配置:

http {
    ...
    server {
        ...
        location / {
            root   html;
            index  index.html index.htm;
        }

        location /hls {
          # 响应类型
            types {
              application/vnd.apple.mpegurl m3u8;
              video/mp2t ts;
            }
            root /usr/local/var/www;
            # 不要缓存
            add_header Cache-Control no-cache;
        }
        ...
    }
    ...
}
复制代码

配置完成后,使用下面命令重启nginx:

nginx -s stop   // 关闭nginx 
nginx           // 打开nginx

nginx -s reload // 重启nginx
复制代码

测试

  1. 测试rtmp推流

首先使用ffmpeg网rtmp服务器推流:

ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/rtmplive/room1
复制代码

利用ffplay或者vlc查看:

ffplay -i rtmp://localhost:1935/rtmplive/room1
复制代码
  1. 测试hls推流
ffmpeg -re -i Test.MOV -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/hls/stream
复制代码

利用ffplay或者vlc查看:

ffplay -i rtmp://localhost:1935/hls/stream
复制代码

也可以在浏览器中输入 http://localhost:8080/hls/stream.m3u8 地址查看hls流:

在centos服务器和mac上搭建rtmp推流服务

下篇文章将详细介绍如何在ios平台实现rtmp推流。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Chinese Authoritarianism in the Information Age

Chinese Authoritarianism in the Information Age

Routledge / 2018-2-13 / GBP 115.00

This book examines information and public opinion control by the authoritarian state in response to popular access to information and upgraded political communication channels among the citizens in co......一起来看看 《Chinese Authoritarianism in the Information Age》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具