Nginx搭建流媒体服务器详解(RTMP和HLS)

本文所讲开发环境以Linux为准:

本文demo架构是android端采用RTMP推流,播放用H5播放器video.js播放RTMP协议或HLS协议。

实验步骤:

  1. 先下载安装 nginx 和 nginx-rtmp 编译依赖工具

sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

  1. 创建一个工作目录,并切换到工作目录

mkdir /usr/jason/nginx
cd /usr/jason/nginx

  1. 下载 nginx 和 nginx-rtmp源码(wget是一个从网络上自动下载文件的自由工具)

wget http://nginx.org/download/nginx-1.7.5.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

  1. 安装unzip工具,解压下载的安装包

sudo apt-get install unzip

  1. 解压 nginx 和 nginx-rtmp安装包

tar -zxvf nginx-1.7.5.tar.gz
-zxvf分别是四个参数
x : 从 tar 包中把文件提取出来
z : 表示 tar 包是被 gzip 压缩过的,所以解压时需要用 gunzip 解压
v : 显示详细信息
f xxx.tar.gz : 指定被处理的文件是 xxx.tar.gz
unzip master.zip

  1. 切换到 nginx-目录

cd nginx-1.7.5

  1. 添加 nginx-rtmp 模板编译到 nginx

./configure --with-http_ssl_module --add-module=…/nginx-rtmp-module-master

  1. 编译安装

make
sudo make install

  1. 安装nginx init 脚本

sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults

  1. 启动和停止nginx 服务,生成配置文件

sudo service nginx start
sudo service nginx stop

  1. 安装 FFmpeg

make
make install

  1. 配置 nginx-rtmp 服务器或HLS服务器

打开 /usr/local/nginx/conf/nginx.conf

在末尾添加如下 配置

复制代码:


rtmp {

    server {

            listen 1935;

            chunk_size 4096;

            application live {

                    live on;

                    record off;

                    exec ffmpeg -i rtmp://localhost/live/$name -threads 1 -c:v libx264 -profile:v baseline -b:v 350K -s 640x360 -f flv -c:a aac -ac 1 -strict -2 -b:a 56k rtmp://localhost/live360p/$name;

            }

            application live360p {

                    live on;

                    record off;

        }

            #RTMP直播流配置

             application rtmplive{

               live on;

               # max_connections:1024;

             }

             #HLS直播流配置

             application hls {

                     live on;

                    hls on;

                     hls_path /usr/local/nginx/html/hls;

                     hls_fragment 5s;

            }

    }

}
  1. 保存上面配置文件,然后重新启动nginx服务

sudo service nginx restart

  1. 如果你使用了防火墙

请允许端口 tcp 1935

  1. 使用 客户端,使用 rtmp协议进行视频实时采集

Field 1: rtmp://your.vultr.ip/live/
Field 2: stream-key-your-set

服务器配置测试播放器:

将播放器复制到目录:/usr/local/nginx/html/,然后修改播放地址

用ffplay播放RTMP直播流:

ffplay “rtmp://49.4.11.26:8088/hls/test.m3u8”

用ffplay播放HLS直播流:

ffplay “http://49.4.11.26:8088/hls/test.m3u8”

实验结论:

RTMP的延时明显低于HLS。

android推流的项目地址:

https://github.com/wxp19940506/dn_live.git
https://github.com/wxp19940506/JasonPushStream.git

链接:https://www.jianshu.com/p/ee5f4734b5ab

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值