mac搭建naginx-full+rtmp服务器

简介

1.想自己研究下视频直播app的开发,然后看到可以自己本地搭建服务器,要求使用nginx-full(网络上很多nginx的安装,两者不一样,具体不通,有知道的可以指导),几度安装均告失败。原因是需要从git上clone需要的homebrew-nginx,偶然看到

https://blog.csdn.net/zcvbnh/article/details/79495285 里面的资源,得以完成。

一、Homebrow安装

确认是否已经安装:

[html]  view plain   copy
  1. man brow  

如果出现下面的代码,证明已经安装过:

[html]  view plain   copy
  1. NAME  
  2.        brew - The missing package manager for macOS  
  3.   
  4. SYNOPSIS  
  5.        brew --version  
  6.        brew command [--verbose|-v] [options] [formula] ...  

安装命令:

[html]  view plain   copy
  1. ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  

如果已经安装过,而想要卸载:

[html]  view plain   copy
  1. ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"  

如果不想重装,升级一下brow

[html]  view plain   copy
  1. brew update  

二、安装nginx

执行克隆命令,github的项目(https://github.com/denji/homebrew-nginx)

[html]  view plain   copy
  1. 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.

执行安装命令:

[html]  view plain   copy
  1. brew install nginx-full --with-rtmp-module  

至此nginx和rtmp模块就安装好了,下面开始来配置nginx的rtmp模块

接下来看一下nginx安装在什么地方

[html]  view plain   copy
  1. brew info nginx-full  

nginx安装所在位置

[html]  view plain   copy
  1. /usr/local/opt/nginx-full/bin/nginx  

nginx配置文件所在位置

[html]  view plain   copy
  1. /usr/local/etc/nginx/nginx.conf  

三、运行nginx

启动nginx,执行命令:

[html]  view plain   copy
  1. nginx  

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


出现以上界面,说明安装成功.

如果终端上提示

[html]  view plain   copy
  1. nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)  

则表示8080端口被占用了, 查看端口PID

[html]  view plain   copy
  1. lsof -i tcp:8080  

kill掉占用8080端口的PID

[html]  view plain   copy
  1. kill 9603(这里替换成占用8080端口的PID)  

重新执行nginx...

nginx常用方法:重新加载配置文件:

[html]  view plain   copy
  1.   
[html]  view plain   copy
  1. nginx -s reload  

重新加载日志:

[html]  view plain   copy
  1. nginx -s reopen  

停止 nginx:

[html]  view plain   copy
  1. nginx -s stop  

有序退出 nginx:

[html]  view plain   copy
  1. nginx -s quit  

四、配置rtmp

修改nginx.conf这个配置文件,配置rtmp
复制nginx配置文件所在位置:
[html]  view plain   copy
  1. vi /usr/local/etc/nginx/nginx.conf  

执行上面命令直接编辑,或者直接前往当前文件用记事本打开.

[html]  view plain   copy
  1. http {  
  2.     ……  
  3. }  
在http节点后面加上rtmp配置
[html]  view plain   copy
  1.   
[html]  view plain   copy
  1. rtmp {  
  2.   server {  
  3.       listen 1935;  
  4.     #直播流配置  
  5.       application rtmplive {  
  6.           live on;  
  7.       #为 rtmp 引擎设置最大连接数。默认为 off  
  8.       max_connections 1024;  
  9.        }  
  10.       application hls{  
  11.       
  12.           live on;  
  13.           hls on;  
  14.           hls_path /usr/local/var/www/hls;  
  15.           hls_fragment 1s;  
  16.       }  
  17.    }  
  18. }  

编辑完成之后,执行一下重新加载配置文件命令:

[html]  view plain   copy
  1. nginx -s reload  

重启nginx:

[html]  view plain   copy
  1. sudo /usr/local/opt/nginx-full/bin/nginx -s reload  

六、安装ffmepg工具

注:ffmepg转码工具

[html]  view plain   copy
  1. brew install ffmpeg  

安装完成之后

安装一个支持rtmp协议的视频播放器,Mac下可以用VLC

本地下载一个视频文件路径为 /Users/ailvgo/Downloads/keep.mp4


执行以下命令
[html]  view plain   copy
  1. 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:
[html]  view plain   copy
  1. rtmp://localhost:1935/rtmplive/room  

然后进行播放

七、FFmpeg常用推流命令

1、桌面录制或者分享
[objc]  view plain   copy
  1. ffmpeg -f avfoundation -i "1" -vcodec libx264 -preset ultrafast -acodec libfaac -f flv rtmp://localhost:1935/rtmplive/room  

2、桌面+麦克风

[objc]  view plain   copy
  1. ffmpeg -f avfoundation -i "1:0" -vcodec libx264 -preset ultrafast -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost:1935/rtmplive/room  

3、桌面+麦克风,并且还要摄像头拍摄到自己

[objc]  view plain   copy
  1. 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地址即可:

[objc]  view plain   copy
  1. rtmp://10.0.0.17:1935/rtmplive/room  

猜你喜欢

转载自blog.csdn.net/wu347771769/article/details/80267977