美文网首页环境配置
Mac上搭建RTMP服务器

Mac上搭建RTMP服务器

作者: 顶级蜗牛 | 来源:发表于2021-08-30 15:43 被阅读0次

前言:不废话上实操。

步骤一:安装Homebrow

Homebrew简称brew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件,可以说Homebrew就是mac下的apt-getyum神器。

查看本地是否安装过

$ brew

安装brew

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

卸载brew

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

安装不成功自己查一下资料吧,这里不过多演示。

步骤二:安装nginx服务器

插看是否安装过nginx

$ brew search nginx

安装nginx

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

注意⚠️: --with-rtmp-module,一定要加上rtmp模块,不然添加rtmp服务时就会报错误:unknown directive "rtmp" in /usr/local/etc/nginx/nginx.conf:135
解决上述问题,只能卸载掉nginx-full,然后重装nginx-full

$ brew uninstall nginx-full  // 卸载nginx-full
$ brew install nginx-full --with-rtmp-module  // 安装nginx-full

步骤三:修改nginx配置文件

打开文件/usr/local/etc/nginx/nginx.conf

$ open /usr/local/etc/nginx/nginx.conf

使用Xcode/文件编辑器打开nginx.conf
修改配置文件:
1.找到http下的server添加HLS配置

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

2.文件拖拽到最后,在没有{}的地方添加rtmp配置

# 在http节点后面加上rtmp配置:
rtmp {
    server {
        listen 1935;
        ping 30s;
        notify_method get;
        
        application zbcs {
            live on;
            record off;
        }
        
        application live {
            live on;
            record off;
            max_connections 1024;
        }

        #增加对HLS支持开始
        application hls {
            live on;
            hls on;
            hls_path /usr/local/var/www/hls;
            hls_fragment 5s;
        }
        #增加对HLS支持结束
    }
}

修改后的整体内容如下:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       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 /usr/local/var/www;
            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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

# 在http节点后面加上rtmp配置:
rtmp {
    server {
        listen 1935;
        ping 30s;
        notify_method get;
        
        application zbcs {
            live on;
            record off;
        }
        
        application live {
            live on;
            record off;
            max_connections 1024;
        }

        #增加对HLS支持开始
        application hls {
            live on;
            hls on;
            hls_path /usr/local/var/www/hls;
            hls_fragment 5s;
        }
        #增加对HLS支持结束
    }
}

说明:
live on 开启实时
hls on 开启hls
hls_path ts文件存放路径
hls_fragment 5s 每个TS文件包含5秒的视频内容

让配置文件生效:
方法一:(亲测有坑,有时候不生效)

$ nginx -s reload // 亲测有坑

方法二:
查看启动的nginx

$ ps -ef|grep nginx

找到master process的进程端口号是549

正在运行的nginx

停掉这个master process进程

$ kill -QUIT 549

让配置文件生效

$ nginx -t -c /usr/local/etc/nginx/nginx.conf

然后就可以启动服务了。

$ nginx

在浏览器里打开http://localhost:8080
如果看到如下页面,说明配置成功了!

启动nginx成功.png

想要停止服务,命令:(stop是强制退出,quit是执行完任务后退出)

$ nginx -s quit
// 或者
$ nginx -s stop

第四步:ffmpeg

安装ffmpeg

$ brew install ffmpeg

查看安装成功了没

$ ffmpeg
ffmpeg

安装失败自己找办法。

RTMP的方式推拉流

测试推流

$ ffmpeg -re -i /Users/xxx/Desktop/sample_iPod.mp4 -vcodec h264 -acodec aac -strict -2 -f flv rtmp://localhost:1935/live/room
一帧一帧地推流.png

测试拉流
开启推流后,用VLC播放器播放下面直播地址视频:

image.png
$ rtmp://localhost:1935/live/room
image.png

注意⚠️: 要在推流完成之前play!!!因为这是直播流,推完就没啦!!!

这样一个简单的本地直播服务就搭建好了!

对于视频直播服务,如果需要支持多路流输入的话,很简单,在Nginx配置文件里多配几个Application就可以了,像下面这样:

image.png

HLS的方式推拉流

测试推流

$ ffmpeg -re -i /Users/xxx/Desktop/sample_iPod.mp4 -vcodec h264 -acodec aac -strict -2 -f flv rtmp://localhost:1935/hls/demo

此时在/usr/local/var/www/hls目录下会产生流媒体文件

image.png

测试推流
打开Safari浏览器输入

http://ip:8080/hls/demo.m3u8
image.png

HLS直播延时

我们知道hls协议是将直播流分成一段一段的小段视频去下载播放的,所以假设列表里面的包含5个ts文件,每个TS文件包含5秒的视频内容,那么整体的延迟就是25秒。因为当你看到这些视频时,主播已经将视频录制好上传上去了,所以时这样产生的延迟。当然可以缩短列表的长度和单个ts文件的大小来降低延迟,极致来说可以缩减列表长度为1,并且ts的时长为1s,但是这样会造成请求次数增加,增大服务器压力,当网速慢时回造成更多的缓冲,所以苹果官方推荐的ts时长时10s,所以这样就会大改有30s的延迟。参考资料:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/FrequentlyAskedQuestions/FrequentlyAskedQuestions.html

相关文章

网友评论

    本文标题:Mac上搭建RTMP服务器

    本文链接:https://www.haomeiwen.com/subject/zwfoiltx.html