1.原想用mac中自带的Apache搭建,但是naginx是轻量级的,同样起web 服务,也比apache 占用更少的内存及资源,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能保持低资源低消耗高性能,用它来做hls或者rtmp流媒体服务器是非常不错的选择.
一、Homebrow安装
确认是否已经安装:
man brow
如果出现下面的代码,证明已经安装过:
-
NAME
-
brew
-
The
missing
package
manager
for
macOS
-
-
SYNOPSIS
-
brew
--version
-
brew
command
[--verbose|-v]
[options]
[formula] ...
安装命令:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
如果已经安装过,而想要卸载:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
如果不想重装,升级一下brow
brew update
执行克隆命令,github的项目(https://github.com/denji/homebrew-nginx)
brew tap denji/nginx
注意brew tap homebrew/nginx报下面的错误,homebrew/nginx已经启用.
报错:Error: homebrew/nginx was deprecated. This tap is now empty as all its formulae were migrated.
执行安装命令:
brew install nginx-full --with-rtmp-module
至此nginx和rtmp模块就安装好了,下面开始来配置nginx的rtmp模块
接下来看一下nginx安装在什么地方
brew info nginx-full
nginx安装所在位置
/usr/local/opt/nginx-full/bin/nginx
nginx配置文件所在位置
/usr/local/etc/nginx/nginx.conf
启动nginx,执行命令:
nginx
浏览器地址栏输入:http://localhost:8080
出现以上界面,说明安装成功.
如果终端上提示
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
则表示8080端口被占用了, 查看端口PID
lsof -i tcp:8080
kill掉占用8080端口的PID
kill 9603(这里替换成占用8080端口的PID)
重新执行nginx...
nginx常用方法:重新加载配置文件:
nginx -s reload
重新加载日志:
nginx -s reopen
停止 nginx:
nginx -s stop
有序退出 nginx:
nginx -s quit
vi /usr/local/etc/nginx/nginx.conf
执行上面命令直接编辑,或者直接前往当前文件用记事本打开.
在http节点后面加上rtmp配置
http { …… }
-
rtmp {
-
server {
-
listen 1935;
-
#直播流配置
-
application rtmplive {
-
live on;
-
#为 rtmp 引擎设置最大连接数。默认为 off
-
max_connections 1024;
-
}
-
application hls{
-
-
live on;
-
hls on;
-
hls_path /usr/local/var/www/hls;
-
hls_fragment 1s;
-
}
-
}
-
}
编辑完成之后,执行一下重新加载配置文件命令:
nginx -s reload
重启nginx:
sudo /usr/local/opt/nginx-full/bin/nginx -s reload
注:ffmepg转码工具
brew install ffmpeg
安装完成之后
安装一个支持rtmp协议的视频播放器,Mac下可以用VLC
本地下载一个视频文件路径为 /Users/ailvgo/Downloads/keep.mp4
ffmpeg -re -i /Users/ailvgo/Downloads/keep.mp4 -vcodec libx264 -acodec aac -strict -2 -f flv rtmp://localhost:1935/rtmplive/room
用vlc
然后打开 VLC 中 的 file -- Open Network, 直接输入代码中的 url:
rtmp://localhost:1935/rtmplive/room
然后进行播放
ffmpeg -f avfoundation -i "1" -vcodec libx264 -preset ultrafast -acodec libfaac -f flv rtmp://localhost:1935/rtmplive/room
2、桌面+麦克风
ffmpeg -f avfoundation -i "1:0" -vcodec libx264 -preset ultrafast -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost:1935/rtmplive/room
3、桌面+麦克风,并且还要摄像头拍摄到自己
ffmpeg -f avfoundation -framerate 30 -i "1:0" \-f avfoundation -framerate 30 -video_size 640x480 -i "0" \-c:v libx264 -preset ultrafast \-filter_complex 'overlay=main_w-overlay_w-10:main_h-overlay_h-10' -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost:2016/rtmplive/room
可以用 LFLiveKit 集成到工程进行推流,只需把localhost:8080换成自己电脑的ip地址即可:
rtmp://10.0.0.17:1935/rtmplive/room