1.Homebrew安裝
打開終端,輸入下列命令
man brew
如果Mac已經(jīng)安裝了, 會(huì)顯示一些命令的幫助信息. 此時(shí)輸入Q退出即可, 直接進(jìn)入第2步安裝nginx.
反之, 如果沒有安裝,執(zhí)行命令
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項(xiàng)目到本地
brew tap homebrew/nginx
執(zhí)行安裝rtmp模塊:
brew install nginx-full --with-rtmp-module
此時(shí), nginx和rtmp模塊就安裝好了 輸入命令:
nginx
在瀏覽器里打開http://localhost:8080 如果出現(xiàn)下圖, 則表示安裝成功
如果終端上提示
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
根據(jù)端口PID, kill掉(這兒的12407換成你自己8080端口的PID)

kill 12407
然后重新執(zhí)行nginx, 打開http://localhost:8080
3.配置nginx和rtmp
在終端輸入open -t /usr/local/etc/nginx/nginx.conf,直接滾到最后一行, 在最后一個(gè)},后面添加

rtmp {
server {
listen 1935;
application live1 {
live on;
record off;
}
}
}
然后重啟nginx
nginx -s reload
如果執(zhí)行命令的時(shí)候, 報(bào)如下異常,
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
在配置文件添加如下代碼,(紅框標(biāo)注的地方)

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;
}
創(chuàng)建hls文件夾
mkdir /usr/local/var/www/hls
5、安裝ffmpeg
執(zhí)行命令
brew install ffmpeg
ffmpeg推rtmp流
ffmpeg -re -i 視頻的據(jù)對(duì)路徑 -vcodec copy -acodec copy -f flv rtmp://localhost:1935/live1/room
將視頻推流到服務(wù)器后,打開VLC,rtmp://localhost:1935/live1/room
如果要運(yùn)行在手機(jī)上,則為:rtmp://電腦的ip:1935/live1/room
ffmpeg推hls流
ffmpeg -re -i 視頻的據(jù)對(duì)路徑 -vcodec copy -acodec copy -f flv rtmp://localhost:1935/hls/room
將視頻推流到服務(wù)器后,打開VLC,http://localhost:1935/hls/room.m3u8
如果要運(yùn)行在手機(jī)上,則為:http://電腦的ip:1935/hls/room.m3u8
6、http點(diǎn)播
前往文件夾 : /usr/local/var/www(如下圖)

將要點(diǎn)播的視頻拖入到www文件夾下:

打開瀏覽器 :http://電腦的ip:8080/4K.mp4