Mac上搭建RTMP服務器

前言:不廢話上實操。

步驟一:安裝Homebrow

Homebrew簡稱brew,是Mac OSX上的軟件包管理工具,能在Mac中方便的安裝軟件或者卸載軟件,可以說Homebrew就是mac下的apt-get丑瞧、yum神器。

查看本地是否安裝過

$ 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協(xié)議獲取HLS的拉流
        #HLS配置開始,這個配置為了‘客戶端‘能夠以http協(xié)議獲取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配置結(jié)束
    }
}

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

# 在http節(jié)點后面加上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支持結(jié)束
    }
}

修改后的整體內(nèi)容如下:


#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協(xié)議獲取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配置結(jié)束
        

        #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節(jié)點后面加上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支持結(jié)束
    }
}

說明:
live on 開啟實時
hls on 開啟hls
hls_path ts文件存放路徑
hls_fragment 5s 每個TS文件包含5秒的視頻內(nèi)容

讓配置文件生效:
方法一:(親測有坑,有時候不生效)

$ 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是執(zhí)行完任務后退出)

$ 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

注意??: 要在推流完成之前playU龆?娲骸看疙!因為這是直播流豆拨,推完就沒啦!D芮臁施禾!

這樣一個簡單的本地直播服務就搭建好了!

對于視頻直播服務搁胆,如果需要支持多路流輸入的話弥搞,很簡單,在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目錄下會產(chǎn)生流媒體文件

image.png

測試推流
打開Safari瀏覽器輸入

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

HLS直播延時

我們知道hls協(xié)議是將直播流分成一段一段的小段視頻去下載播放的攀例,所以假設列表里面的包含5個ts文件,每個TS文件包含5秒的視頻內(nèi)容顾腊,那么整體的延遲就是25秒粤铭。因為當你看到這些視頻時,主播已經(jīng)將視頻錄制好上傳上去了杂靶,所以時這樣產(chǎn)生的延遲梆惯。當然可以縮短列表的長度和單個ts文件的大小來降低延遲,極致來說可以縮減列表長度為1吗垮,并且ts的時長為1s垛吗,但是這樣會造成請求次數(shù)增加,增大服務器壓力烁登,當網(wǎng)速慢時回造成更多的緩沖怯屉,所以蘋果官方推薦的ts時長時10s,所以這樣就會大改有30s的延遲饵沧。參考資料:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/FrequentlyAskedQuestions/FrequentlyAskedQuestions.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蚀之,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子捷泞,更是在濱河造成了極大的恐慌足删,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件锁右,死亡現(xiàn)場離奇詭異失受,居然都是意外死亡,警方通過查閱死者的電腦和手機咏瑟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門拂到,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人码泞,你說我怎么就攤上這事兄旬。” “怎么了?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵领铐,是天一觀的道長悯森。 經(jīng)常有香客問我,道長绪撵,這世上最難降的妖魔是什么瓢姻? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮音诈,結(jié)果婚禮上幻碱,老公的妹妹穿的比我還像新娘。我一直安慰自己细溅,他們只是感情好褥傍,可當我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著喇聊,像睡著了一般摔桦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上承疲,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天邻耕,我揣著相機與錄音,去河邊找鬼燕鸽。 笑死兄世,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的啊研。 我是一名探鬼主播御滩,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼党远!你這毒婦竟也來了削解?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤沟娱,失蹤者是張志新(化名)和其女友劉穎氛驮,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體济似,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡矫废,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了砰蠢。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蓖扑。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖台舱,靈堂內(nèi)的尸體忽然破棺而出律杠,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布柜去,位于F島的核電站灰嫉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏诡蜓。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一胰挑、第九天 我趴在偏房一處隱蔽的房頂上張望蔓罚。 院中可真熱鬧,春花似錦瞻颂、人聲如沸豺谈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽茬末。三九已至,卻和暖如春盖矫,著一層夾襖步出監(jiān)牢的瞬間丽惭,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工辈双, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留责掏,地道東北人。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓湃望,卻偏偏與公主長得像换衬,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子证芭,可洞房花燭夜當晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容