基于Nginx的流媒體服務(wù)搭建

編譯Nginx

下載最新的nginx源碼
wget http://nginx.org/download/nginx-1.19.2.tar.gz

如果nginx是用yum安裝的砰逻,一定不要用網(wǎng)上其余文章所謂的基于現(xiàn)有的nginx編譯,yum自帶的1.14.1版本bug很多懈息,全是坑,想要基于原有的版本編譯出來(lái)基本不可能

下載rmtp模塊
https://github.com/arut/nginx-rtmp-module
獲取當(dāng)前的nginx的配置芙代,復(fù)制下來(lái)
nginx -V
解壓下載的nginx鼓黔,進(jìn)入準(zhǔn)備編譯

tar -xzvf nginx-1.19.2.tar.gz
cd nginx-1.19.2

將剛才拿到的nginx配置組合成參數(shù),進(jìn)行編譯
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=/root/nginx-rtmp-module

后面增加了--add-module=/root/nginx-rtmp-module谎脯,路徑為剛才下載的模塊解壓后的文件夾
如果執(zhí)行出錯(cuò)了一般是缺少一些東西導(dǎo)致的葱跋,可以上網(wǎng)搜一下,這里就不一一贅述了

執(zhí)行make
make

此處肯定會(huì)報(bào)錯(cuò),需要修改objs/Makefile,在CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection后添加-Wno-implicit-fallthrough

替換nginx
make install

網(wǎng)上的文章都說(shuō)此處不要使用make install娱俺,但按照他們的方法替換過(guò)去稍味,會(huì)報(bào)一些庫(kù)文件不匹配,所以不如直接install

重啟nginx
systemctl restart nginx
至此荠卷,支持流媒體的nginx就部署好了

配置HLS

編輯nginx.conf,增加rtmp配置

rtmp {
    server {
        listen 1935;
        application rtmplive {
            live on;
        }
        application vod{
            play /home/work/qiuniu/vod;
        }
    }
}

注意此處要放在最外層模庐,不要放置到http里面

編輯http配置文件,增加location配置

location /hls {
        types{
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }
        root /usr/share/nginx/html;
        add_header Cache-Control no-cache;
    }

視頻切片

視頻切片使用ffmpeg工具,將mp4文件轉(zhuǎn)為ts
ffmpeg -y -i /home/work/qiuniu/mp4/demo.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb /home/work/qiuniu/ts/demo.ts
然后切分成小段ts,并放置在nginx配置的目錄下
ffmpeg -i /home/work/qiuniu/ts/demo.ts -c copy -map 0 -f segment -segment_list /usr/share/nginx/html/hls/demo.m3u8 -segment_time 30 /usr/share/nginx/html/hls/demo%03d.ts

如果分段切得太小僵朗,會(huì)導(dǎo)致視頻一卡一卡的赖欣,所以使用-segment_time 30增大分段

測(cè)試播放

使用VLC軟件,點(diǎn)擊打開(kāi)網(wǎng)絡(luò)验庙,將配置好的路徑放進(jìn)去


VLC

點(diǎn)擊open顶吮,就可以播放了


VLC 播放測(cè)試

H5播放

使用vhs播放hls流

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link  rel="stylesheet" />
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
    <video-js id="example-video" class="vjs-default-skin" width="960" height="400" controls>
        <source src="https://localhost/hls/demo.m3u8" type="application/x-mpegURL" />
    </video-js>
    </div>

    <script src="https://unpkg.com/video.js/dist/video.js"></script>
    <script src="https://unpkg.com/@videojs/http-streaming/dist/videojs-http-streaming.js"></script>
    <script>
        var player = videojs('example-video');
        player.play();
    </script>
</body>

</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市粪薛,隨后出現(xiàn)的幾起案子悴了,更是在濱河造成了極大的恐慌,老刑警劉巖违寿,帶你破解...
    沈念sama閱讀 211,194評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件湃交,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡藤巢,警方通過(guò)查閱死者的電腦和手機(jī)搞莺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)掂咒,“玉大人才沧,你說(shuō)我怎么就攤上這事∩芄危” “怎么了温圆?”我有些...
    開(kāi)封第一講書人閱讀 156,780評(píng)論 0 346
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)孩革。 經(jīng)常有香客問(wèn)我岁歉,道長(zhǎng),這世上最難降的妖魔是什么膝蜈? 我笑而不...
    開(kāi)封第一講書人閱讀 56,388評(píng)論 1 283
  • 正文 為了忘掉前任锅移,我火速辦了婚禮,結(jié)果婚禮上饱搏,老公的妹妹穿的比我還像新娘帆啃。我一直安慰自己,他們只是感情好窍帝,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,430評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布努潘。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪疯坤。 梳的紋絲不亂的頭發(fā)上报慕,一...
    開(kāi)封第一講書人閱讀 49,764評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音压怠,去河邊找鬼眠冈。 笑死,一個(gè)胖子當(dāng)著我的面吹牛菌瘫,可吹牛的內(nèi)容都是我干的蜗顽。 我是一名探鬼主播,決...
    沈念sama閱讀 38,907評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼雨让,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼雇盖!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起栖忠,我...
    開(kāi)封第一講書人閱讀 37,679評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤崔挖,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后庵寞,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體狸相,經(jīng)...
    沈念sama閱讀 44,122評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,459評(píng)論 2 325
  • 正文 我和宋清朗相戀三年捐川,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了脓鹃。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,605評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡古沥,死狀恐怖瘸右,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情渐白,我是刑警寧澤,帶...
    沈念sama閱讀 34,270評(píng)論 4 329
  • 正文 年R本政府宣布逞频,位于F島的核電站纯衍,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏苗胀。R本人自食惡果不足惜襟诸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,867評(píng)論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望基协。 院中可真熱鬧歌亲,春花似錦、人聲如沸澜驮。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 30,734評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至悍缠,卻和暖如春卦绣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背飞蚓。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 31,961評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工滤港, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人趴拧。 一個(gè)月前我還...
    沈念sama閱讀 46,297評(píng)論 2 360
  • 正文 我出身青樓溅漾,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親著榴。 傳聞我的和親對(duì)象是個(gè)殘疾皇子添履,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,472評(píng)論 2 348