一、clone nginx到本地,执行
brew tap denji/homebrew-nginx
二、安装nginx,执行
brew install nginx-full --with-rtmp-module
执行:nginx,在浏览器里打开http://localhost:8080 出现Welcome to nginx! 表示安装成功
三、查看nginx安装到哪了
brew info nginx-full
如下, 找到nginx.conf
文件所在位置
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
四、修改:nginx.conf
# 在http节点后面加上rtmp配置:
rtmp {
server {
listen 1935;
application rtmplive {
live on;
record off;
}
}
}
五、重启nginx
/usr/local/Cellar/nginx-full/1.17.0/bin/nginx -s reload
六、推流
ffmpeg -re -i aa.mp4 -vcodec libx264 -acodec aac -strict -2 -f flv rtmp://localhost:1935/rtmplive/room
七、vlc播放
将视频推流到服务器后,打开VLC,然后File->open network->输入:
rtmp://localhost:1935/rtmplive/room
配置对HLS的支持
HLS 直播流配置
找到http-->server,在花括号中增加
server {
listen 8089; #修改8080
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#HLS配置开始,这个配置为了`客户端`能够以http协议获取HLS的拉流
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root html;
add_header Cache-Control no-cache;
}
#HLS配置结束
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
找到rtmp下的server在花括号中增加
# 在http节点后面加上rtmp配置:
rtmp {
server {
listen 1935;
application rtmplive {
live on;
record off;
}
#增加对HLS支持开始
#live on; 开启实时
#hls on; 开启hls
#hls_path; ts文件存放路径
#如需要修改指定的目录下,比如"/tmp/hls"
#那么,我们也需要在http-->server中对root 路径更改为:/tmp 。要不然,会拉不到流。
#root html 是指使用当前nginx服务器根目录所在位置,指向的是 /usr/local/var/www 这个目录
#hls_fragment 5s; 每个TS文件包含5秒的视频内容
application hls {
live on;
hls on;
hls_path /usr/local/var/www/hls;
hls_fragment 5s;
}
#增加对HLS支持结束
}
}
进行推流
ffmpeg推流还是和上边的一样,不过,我们需要推到新配置的hls中,movie 关键字可以任何替换
ffmpeg -re -i demo.mp4 -vcodec copy -f flv rtmp://localhost:1935/hls/movie
测试拉流
通过上面的配置,我们可以同时通过rtmp和hls两种播放方式来看到推出来的流。注意,如果使用 http 方式,则是监听的 8080 端口,这个是在配置文件里写的
(1) 用rtmp:(使用VLC验证播放)
rtmp://192.168.1.100/hls/movie
(2) 用hls播放
http://192.168.1.100:8080/hls/movie.m3u8