zoukankan      html  css  js  c++  java
  • MAC上使用nginx搭建直播服务器

    第一步:安装nginx-full

    安装

    brew tap homebrew/nginx
    Error: homebrew/nginx was deprecated. This tap is now empty as all its formulae were migrated.

    报错原因是homebrew/nginx被弃用了,git路径变了。改用

    brew tap denji/nginx
    brew install nginx-full --with-rtmp-module 

    如果之前安装过nginx,那执行install的时候会报错,提示需要执行

    brew unlink nginx

    在此执行install就会成功。

    参考:https://blog.csdn.net/fengsh998/article/details/79942775 

    查看安装信息

    执行 brew info nginx-full,可以看到

    版本:denji/nginx/nginx-full: stable 1.15.8, HEAD
    安装所在位置:/usr/local/Cellar/nginx-full/1.15.8
    配置文件所在位置:/usr/local/etc/nginx/nginx.conf
    服务器根目录所在位置:/usr/local/var/www
    服务启动方式:brew services start denji/nginx/nginx-full or nginx

    启动nginx

    /usr/local/Cellar/nginx-full/1.15.8/bin/nginx

    访问http://localhost:8080/成功说明nginx-full安装成功。

    扩展:

    brew tap [options] user/repo [URL]

    tap是采用,利用的意思,即采用规定的仓库。当未指定URL时,使用HTTPS采用来自GitHub的规定的仓库。由于有太多的taps被托管在GitHub上,这个命令是 brew tap user/repo https://github.com/user/homebrew-repo 简写。

    第二部:配置rtmp

    编辑配置文件

    编辑/usr/local/etc/nginx/nginx.conf,添加rtmp配置

    ...
    
    http {
        ...
    }
    
    rtmp {
        server {
            listen 1935;
    
            application myapp {
                live on;
                record off;
            }
        }
    }

    重新加载nginx

    /usr/local/Cellar/nginx-full/1.15.8/bin/nginx -s reload 

    检查1935端口是否被监听

    lsof -i tcp:1935

    第三步:rtmp推流直播

    安装ffmpeg

    brew install ffmpeg

    使用ffmpeg进行推流

    ffmpeg -re -i /Users/lipengfei/Downloads/1549850041799339.mp4 -vcodec copy -f flv rtmp://localhost:1935/myapp/room

    URL中的myapp对应nginx.conf中配置application,后面的room是随意加的。

    未报错说明推流成功。

    如果报错说连接失败

    则需要检查1935端口是否被监听,nginx.conf配置文件是否正常,nginx是否已reload。

    验证rtmp直播

    下载安装VLC

    https://www.videolan.org/vlc/

    点击菜单【File】->【Open Network】,在弹出框中Network下的URL中输入rtmp://localhost:1935/myapp/room,点击Open即可。

    点击播放按钮,查是否能够播放。

    第四步:HLS直播

    编辑 /usr/local/etc/nginx/nginx.conf ,在http服务中配置中添加hls配置

    ...

    http {
         ...

    location
    / { root html; index index.html index.htm; } # HLS配置开始,这个配置为了‘客户端’能够以http协议获取HLS的拉流 location /hls { # Serve HLS fragments types { application/vnd.apple.mpegurl m3u8; video/mp2t 5s; } root html; add_header Cache-Control no-cache; } # HLS配置结束 }

    在rtmp服务中添加HLS支持

    rtmp {
        server {
            listen 1935;
    
            application myapp {
                live on;
                record off;
            }
    
            # 增加对HLS支持
            application hls {
                live on;
                hls on;
                hls_path /usr/local/var/www/hls;
                hls_fragment 5s;
            }
        }
    }

    保存配置后,重新加载nginx

    /usr/local/Cellar/nginx-full/1.15.8/bin/nginx -s reload 

    进行推流

    ffmpeg -re -i /Users/lipengfei/Downloads/1549850041799339.mp4 -vcode copy -f flv rtmp://localhost:1935/hls/movie

    url中当myapp需要换成hls,movie可以随意替换。

    推流后在/usr/local/var/www/hls/目录下可以看到生成的m3u8和ts文件

    测试拉流

    使用rtmp播放

    在VLC中输入http://localhost:1935/hls/movie进行播放。

    使用hls播放

    在Safari浏览器中输入http://localhost:8080/hls/movie.m3u8进行播放。

    参考:

    https://blog.csdn.net/fengsh998/article/details/79942775

    https://www.cnblogs.com/jys509/p/5649066.html

    http://www.cnblogs.com/jys509/p/5653720.html

  • 相关阅读:
    挺喜欢的一幅摄影作品,不知道作者 不知道出处...
    使用触发器来监控表的使用情况
    SQL Server 针对表的只读权限分配
    tnslsnr.exe进程占用大量内存的解决.
    记录一次MYSQL的备份(浅尝辄止型)
    记录temp被撑爆的一次SQL tuning
    Bug 5880921 V$SYSMETRIC_HISTORY 的时间错乱
    sqlite3学习记录
    指针 数组指针 指针数组 函数指针等说明。
    c/c++ 运算符 优先级 结合性 记录
  • 原文地址:https://www.cnblogs.com/zcqkk/p/10364140.html
Copyright © 2011-2022 走看看