網(wǎng)絡(luò)直播好火茅诱,自己來搭建個(gè)服務(wù)器玩一玩兒
我用虛擬機(jī)搭建的服務(wù)器春叫,裝的最新的Ubuntu18.04,由于要在局域網(wǎng)內(nèi)訪問量蕊,需要設(shè)置虛擬機(jī)的網(wǎng)絡(luò)為橋接適配器(相當(dāng)于直接通過路由器支配起暮,跟主機(jī)沒關(guān)系)
ps:附贈(zèng)Ubuntu18.04開機(jī)啟動(dòng)選項(xiàng)
在“Ubuntu軟件”中搜索并安裝GNOME Tweaks卖氨,在其中找“Startup Application”
1、下載插件
下載nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git
下載nginx_mod_h264_streaming
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
解壓后拷貝文件到/usr/local/src/
sudo cp -r nginx-rtmp-module /usr/local/src/
sudo cp -r nginx_mod_h264_streaming-2.2.7 /usr/local/src/
2负懦、下載nginx
nginx的官方網(wǎng)站為:http://nginx.org/en/download.html
選擇你要下載的版本筒捺,最新的穩(wěn)定版本nginx-1.14.2
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar -xzvf nginx-1.14.2.tar.gz
3、進(jìn)入nginx項(xiàng)目執(zhí)行編譯前的配置
#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc
#apt install libpcre3 libpcre3-de openssl libssl-dev ruby zlib1g zlib1g-dev
./configure --with-http_flv_module --with-http_mp4_module --add-module=/usr/local/src/nginx-rtmp-module --add-module=/usr/local/src/nginx_mod_h264_streaming-2.2.7
提示錯(cuò)誤:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
需要安裝
sudo apt install libpcre3 libpcre3-dev
sudo apt install openssl libssl-dev
提示錯(cuò)誤
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
需要安裝
sudo apt install ruby
sudo apt install zlib1g
sudo apt install zlib1g-dev
提示錯(cuò)誤
/usr/local/src/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c:158:10: error: ‘ngx_http_request_t {aka struct ngx_http_request_s}’ has no member named ‘zero_in_uri’; did you mean ‘plus_in_uri’?
if (r->zero_in_uri)
^~~~~~~~~~~
plus_in_uri
屏蔽出錯(cuò)的if判斷即可
4纸厉、配置通過之后把Makefile拷貝出來系吭,然后編譯
// 可能會(huì)遇到變量未使用的錯(cuò)誤,去掉Makefile文件中-Werror
cp objs/Makefile ./
make
sudo make install
5 CentOs安裝ngnix
rpm -Uvh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
yum install nginx
啟動(dòng)
sudo systemctl start nginx.service
開機(jī)啟動(dòng)
sudo systemctl enable nginx.service
配置目錄/etc/nginx
6颗品、配置nginx.conf
sudo gedit /usr/local/nginx/conf/nginx.conf
在最后加入:
rtmp {
server {
listen 10081; #直播端口號
chunk_size 4000;
application hls { #rtmp推流請求路徑
live on;
hls on;
hls_path /usr/local/nginx/html/hls;
hls_fragment 5s;
}
}
}
在原來的http中加入下面配置的server部分
http {
server {
listen 10080; #點(diǎn)播端口號
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/src/nginx-rtmp-module/;
}
location /control {
rtmp_control all;
}
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root html; #m3u8源文件放到/usr/local/nginx/html下肯尺,其他位置有問題
expires -1;
}
location ~\.flv {
root /home/xxxx; #點(diǎn)播源文件的目錄
flv;
}
location ~\.mp4$ {
root /home/xxxx; #點(diǎn)播源文件的目錄
mp4;
}
}
}
7、設(shè)置權(quán)限
需要先啟動(dòng)nginx躯枢,并重啟電腦之后執(zhí)行權(quán)限設(shè)置(也可以指定為其他路徑则吟,需要在nginx.conf的hls_path處配置-步驟5)
sudo chmod 755 /usr/local/nginx/html/hls
8、啟動(dòng)
sudo /usr/local/nginx/sbin/nginx
重啟
$ps -ef | grep nginx
root 18764 1 0 09:30 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 18765 18764 0 09:30 ? 00:00:01 nginx: worker process
nobody 18766 18764 0 09:30 ? 00:00:00 nginx: cache manager process
$sudo kill -QUIT 18764 18765 18766
9锄蹂、OBS推流
我用的是OBS
sudo apt install obs-studio
具體使用方法自己網(wǎng)上搜氓仲,一大片
給出幾個(gè)圖片自己悟吧
9、ffmpeg推流
sudo apt install ffmpeg
推流命令
ffmpeg -re -i ~/Video/test.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://localhost:11181/hls/
ffmpeg -re -i test_2.mp4 -c:a mp3 -ar 44100 -ab 48k -c:v libx264 -s:v 3840x2160 -f flv rtmp://localhost:10081/hls/
ffmpeg -re -i test_1/輕松.m3u8 -c:v libx264 -c:a aac -s:v 1920x1080 -strict -2 -f fls rtmp://localhost:10081/hls/
-re 以本地幀頻讀數(shù)據(jù)败匹,主要用于模擬捕獲設(shè)備
-i path 輸入視頻地址
-vcodec視頻流編碼方式
-b 視頻流幀碼率(默認(rèn)只有200k寨昙,一般都需要手動(dòng)設(shè)置,具體的數(shù)值視codec選擇而定)
-r 視頻流幀率(一般說來PAL制式通常用25,NTSC制式通常用29)
-s 視頻解析度(分辨率掀亩,也要視codec和你的需要而定。另:具體寫法使用“數(shù)字x數(shù)字”的形式)
-t 處理持續(xù)時(shí)間
-acodec 音頻流編碼方式
-ab 音頻流碼率(默認(rèn)是同源文件碼率欢顷,也需要視codec而定)
-ar 音頻流采樣率(大多數(shù)情況下使用44100和48000,分別對應(yīng)PAL制式和NTSC制式槽棍,根據(jù)需要選擇)
-ac channels 設(shè)置通道,缺省為1
-f 強(qiáng)制使用某種格式
-vn 屏蔽視頻流
-an 屏蔽音頻流
-b 設(shè)置比特率抬驴,缺省200kb/s
-r 設(shè)置幀頻炼七,缺省25
-s 設(shè)置幀大小,格式為WxH布持,缺省160x128.
-aspect 設(shè)置橫縱比4:3\16:9;1.3333\1.7777
-croptop/botton/left/right 設(shè)置頂部切除帶大小豌拙,像素單位
-padtop/botton/left/right size 設(shè)置頂部補(bǔ)齊的大小,像素單位
-padcolor color 設(shè)置補(bǔ)齊條顏色(hex,6個(gè)16進(jìn)制的數(shù),紅:綠:藍(lán)排列题暖,比如000000代表黑色
-vprofile baseline, extended, main, high(https://www.cnblogs.com/tinywan/p/6404411.html)