1.Homebrew安装
打开终端,输入下列命令
man brew
如果Mac已经安装了, 会显示一些命令的帮助信息. 此时输入Q退出即可, 直接进入第2步安装nginx.
反之, 如果没有安装,执行命令
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
如果安装后, 想要卸载
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
2.安装nginx
先clone nginx项目到本地
brew tap homebrew/nginx
执行安装rtmp模块:
brew install nginx-full --with-rtmp-module
此时, nginx和rtmp模块就安装好了 输入命令:
nginx
在浏览器里打开http://localhost:8080 如果出现下图, 则表示安装成功
如果终端上提示
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use).....
则表示8080端口被占用了, 查看端口PID
lsof -i tcp:8080
根据端口PID, kill掉(这儿的12407换成你自己8080端口的PID)
kill 12407
然后重新执行nginx, 打开http://localhost:8080
3.配置nginx和rtmp
在终端输入open -t /usr/local/etc/nginx/nginx.conf
,直接滚到最后一行, 在最后一个},后面添加
rtmp {
server {
listen 1935;
application live1 {
live on;
record off;
}
}
}
然后重启nginx
nginx -s reload
如果执行命令的时候, 报如下异常,
nginx: [emerg] unknown directive “rtmp” in /usr/local/etc/nginx/nginx.conf:119 说明没有安装rtmp模块.
解决方案:
卸载nginx,再次安装nginx
brew uninstall nginx // 删除nginx
brew install nginx-full --with-rtmp-module // 安装
4.配置nginx和hls
在配置文件添加如下代码,(红框标注的地方)
location /hls {
#Serve HLS config
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /usr/local/var/www;
add_header Cache-Control no-cache;
}
application hls{
live on;
hls on;
hls_path /usr/local/var/www/hls;
hls_fragment 1s;
}
创建hls文件夹
mkdir /usr/local/var/www/hls
5、安装ffmpeg
执行命令
brew install ffmpeg
ffmpeg推rtmp流
ffmpeg -re -i 视频的据对路径 -vcodec copy -acodec copy -f flv rtmp://localhost:1935/live1/room
将视频推流到服务器后,打开VLC,rtmp://localhost:1935/live1/room
如果要运行在手机上,则为:rtmp://电脑的ip:1935/live1/room
ffmpeg推hls流
ffmpeg -re -i 视频的据对路径 -vcodec copy -acodec copy -f flv rtmp://localhost:1935/hls/room
将视频推流到服务器后,打开VLC,http://localhost:1935/hls/room.m3u8
如果要运行在手机上,则为:http://电脑的ip:1935/hls/room.m3u8
6、http点播
前往文件夹 : /usr/local/var/www(如下图)
将要点播的视频拖入到www文件夹下:
打开浏览器 :http://电脑的ip:8080/4K.mp4