Mac上搭建直播服務器 Nginx+rtmp
簡介
Nginx 是非常優秀的開源服務器,用它來做hls或者rtmp流媒體服務器是非常不錯的選擇,
1、安裝
增加對 nginx 的擴展;也就是從github上下載,home-brew對ngixn的擴展
執行克隆命令,github的項目(https://github.com/denji/homebrew-nginx)
$ brew tap denji/nginx
注意: brew tap homebrew/nginx
報下面的錯誤, 使用brew tap denji/nginx
替代
homebrew/nginx was deprecated. This tap is now empty as all its formulae were migrated.
2、執行安裝命令:
$ brew install nginx-full --with-rtmp-module
查看 nginx 安裝在哪里
$ brew info nginx-full
--HEAD
Install HEAD version
==> Caveats
Docroot is: /usr/local/var/www
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
nginx will load all files in /usr/local/etc/nginx/servers/.
- Tips -
Run port 80:
$ sudo chown root:wheel /usr/local/opt/nginx-full/bin/nginx
$ sudo chmod u+s /usr/local/opt/nginx-full/bin/nginx
Reload config:
$ nginx -s reload ## 重新加載配置文件
Reopen Logfile:
$ nginx -s reopen ## 再次打開配置文件
Stop process:
$ nginx -s stop ## 停止服務器
Waiting on exit process
$ nginx -s quit ## 退出服務器
To have launchd start denji/nginx/nginx-full now and restart at login:
brew services start denji/nginx/nginx-full
Or, if you don't want/need a background service you can just run:
nginx
- nginx安裝所在位置
/usr/local/opt/nginx-full/bin/nginx
- nginx配置文件所在位置
/usr/local/etc/nginx/nginx.conf
- nginx服務器根目錄所在位置
/usr/local/var/www
啟動nginx服務
$ nginx
在瀏覽器地址欄輸入:http://localhost:8080
出現Welcome to nginx ,代表nginx安裝成功了。
3、配置rtmp
打開配置文件 /usr/local/etc/nginx/nginx.conf
http {
……
}
#在http節點下面(也就是文件的尾部)加上rtmp配置:
rtmp {
server {
listen 1935;
application abcs {
live on;
record off;
}
}
}
- rtmp 是協議名稱
- server 說明內部中是服務器相關配置
- listen 監聽的端口號, rtmp協議的默認端口號是1935
- application 訪問的應用路徑是 abcs
- live on; 開啟實時
- record off; 不記錄數據
4、 保存文件后,重新加載nginx的配置文件
$ nginx -s reload
5、安裝ffmepg工具
$ brew install ffmpeg
安裝這個需要等一段時間, 這時你可以準備一個視頻文件作為來推流,然后安裝一個支持rtmp協議的視頻播放器.Mac下可以用 VLC
6、通過ffmepg命令進行推流
ffmpeg -re -i [你的視頻文件的絕對路徑] -vcodec copy -f flv rtmp://localhost:1935/abcs/room
// 如:ffmpeg -re -i /Users/caolongjian/Desktop/CCVideo.mp4 -vcodec copy -f flv rtmp://localhost:1935/abcs/room
這里abcs是上面的配置文件中,配置的應用的路徑名稱;后面的room可以隨便寫。
7、 驗證視頻
然后電腦上打開vlc這個播放器軟件 點擊File---->Open Network 在彈出來的框中選擇Network然后輸入URL:
rtmp://localhost:1935/abcs/room
參考
Mac上搭建直播服務器Nginx+rtmp
mac搭建naginx+rtmp服務器-帶你出坑
[toc]