Mac上搭建Nginx + rtmp

时间:2023-03-09 18:49:39
Mac上搭建Nginx + rtmp

介绍

nginx是非常优秀的开源服务器,用它来做hls或者rtmp流媒体服务器是非常不错的选择,本人在网上整理了安装流程,分享给大家并且作备忘。

安装步骤

1.先安装brew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

如果要下载brew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

2.安装Niginx

增加对 nginx 的扩展;也就是从github上下载,home-brew对ngixnx的扩展

brew tap homebrew/nginx

3.安装Nginx服务器和rtmp模块

brew install nginx-full --with-rtmp-module

这个安装,耗时相对来说比较长。通过操作以上步骤nginx和rtmp模块就安装好了,下面开始来配置nginx的rtmp模块。

查看nginx安装路径:

brew info nginx-full
  • nginx安装所在位置  /usr/local/Cellar/nginx-full/1.10.1/bin/nginx
  • nginx配置文件所在位置   /usr/local/etc/nginx/nginx.conf
  • nginx服务器根目录所在位置  /usr/local/var/www

4.启动Nginx

nginx

在浏览器地址栏输入:http://localhost:8080

关闭Nsinx:

nginx -s stop

5.配置RTMP

打开nginx.conf, 找到/usr/local/etc/nginx/nginx.conf 文件

http {
……
}
#在http节点下面(也就是文件的尾部)加上rtmp配置:
rtmp {
server {
listen 1935;
application zbcs {
live on;
record off;
}
}
}

说明:

    1. rtmp是协议名称
    2. server 说明内部中是服务器相关配置
    3. listen 监听的端口号, rtmp协议的默认端口号是1935
    4. application 访问的应用路径是 zbcs
    5. live on; 开启实时
    6. record off; 不记录数据

5.保存文件后重启Nginx

nginx -s reload

6.安装ffmpeg工具

brew install ffmpeg

7.安装支持RTMP播放的软件

VLC:http://rj.baidu.com/soft/detail/25680.html?ald

8.通过ffmpeg进行推流

ffmpeg -re -i /Users/****/Documents/Document/demo.mp4  -vcodec copy -f flv rtmp://localhost:1935/zbcs/room

这里zbcs是上面的配置文件中,配置的应用的路径名称;后面的room可以随便写

9.在vlc中播放RTMP视频

然后电脑上打开vlc这个播放器软件 点击File---->Open Network 在弹出来的框中选择Network然后输入URL:

rtmp://localhost:1935/zbcs/room

双击播放。

10.配置HLS

打开nginx.conf, 找到/usr/local/etc/nginx/nginx.conf 文件,找到http -> server,在花括号中增加:

 server {
listen 8080;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #HLS配置开始,这个配置为了`客户端`能够以http协议获取HLS的拉流
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root html;
add_header Cache-Control no-cache;
}
#HLS配置结束 #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

找到rtmp下的server,在花括号中增加:

#在http节点下面(也就是文件的尾部)加上rtmp配置:
rtmp {
server {
listen 1935;
application zbcs {
live on;
record off;
}
#增加对HLS支持开始
application hls {
live on;
hls on;
hls_path /usr/local/var/www/hls;
hls_fragment 5s;
}
#增加对HLS支持结束
}
}

说明:

  1. live on; 开启实时
  2. hls on; 开启hls
  3. hls_path; ts文件存放路径
  4. hls_fragment 5s; 每个TS文件包含5秒的视频内容

11.保存配置文件,重新启动nginx

nginx -s reload

12.利用ffmpeg推流到Nginx

ffmpeg -re -i /Users/jiangys/Documents/Document/demo.mp4 -vcodec copy -f flv rtmp://localhost:1935/hls/movie

然后就可以在/usr/local/var/www/hls(html默认配置文件)路径下看到ts的切片文件和m3u8文件

13.播放hls

通过上面配置,可以通过rtmp和hls两种方式来播放

1)rtmp方式:

用VCL打开:

rtmp://192.168.1.100/hls/movie

2)hls方式:

用VCL或者Safari浏览器:

http://192.168.1.100:8080/hls/movie.m3u8

14.如果想把ts文件存放到指定路径下,比如"/tmp/hls"

  application hls {
live on;
hls on;
hls_path /tmp/hls;
}

那么,我们也需要在http-->server中对root 路径更改为:/tmp 。要不然,会拉不到流。

10.错误处理

1)ffmepg命令推流的时候,提示连接失败。[tcp @ 0x7ff162504b60] Connection to tcp://localhost:1935 failed (Connection refused), trying next address

出现这个错,是因为配置了nginx.conf后,需要重启nginx。输入命令重启后就没有问题了

nginx -s reload

2)安装完nginx后,在浏览器地址栏输入:http://localhost:8080 显示打不开网页

解决方式:由于安装后nginx服务器,有时需要等上几分钟才生效。如果还不生效,测试下是否能成功启动nginx服务,命令

nginx