Ubuntu下搭建Nginx伺服器+整合RTMP視訊直播流處理(邊做邊做更新)
簡介:
要求:做一個網頁獲取攝像頭的視訊流,然後將視訊流經過流伺服器推送到後臺視訊處理伺服器,再由後臺視訊處理伺服器推送到流伺服器,最終推送到頁面。如圖:
經過查閱資料,目前有red5以及nginx+nginx-rtmp-module實現,選擇第二種方式來實現。
準備工作:
第一步:準備一個ubuntu的系統。本人使用的是ubuntu16.0.4的系統。
第二步:下載nginx以及相關的依賴,我已經準備好了,大家可以下載我的,安裝的過程以及命令可以直接用我的,網盤地址:XXX(用我的,直接看第三步),也可以去官網下載。如果自己安裝的話,請參考下面兩篇文章(編譯的版本以及路徑需要自己修改)
第三步:將下載下來的依賴包中的四個檔案拷貝到/usr/local目錄下。
第四步:將這四個檔案解壓。使用 tar -zxvf +壓縮檔名 來進行解壓
開始編譯安裝nginx:
第一步:nginx編譯依賴pcre,zlib,openssl,編譯前請確保g++編譯器已安裝,若未安裝使用sudo apt-get install build-essential
安裝。
第二步:編譯openssl,進入目錄:cd openssl-1.0.2j/ (tips:如果在使用下面命令報類似許可權的錯誤,請使用sudo)
./config
./config -t
make depend
make
make test
make install
第三步:編譯pcre,進入目錄: cd /usr/local/pcre-8.38
切換至pcre目錄,sudo apt-get install libpcre3-dev
安裝libpcre3-dev依賴庫
./configure --prefix=/usr/local/pcre-8.38
make
make install
第四步:編譯nginx
cd /usr/local/nginx-1.8.1 sudo ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/pcre-8.38 --with-zlib=/usr/local/zlib-1.2.11 --with-openssl=/usr/local/openssl-1.0.2j --with-http_ssl_module sudo make sudo make install
第五步:測試nginx是否順利的搭建
如編譯成功,/usr/local
下將有nginx
資料夾,cd /usr/local/nginx/sbin
切換到該目錄,sudo ./nginx
,執行nginx,在瀏覽器中輸入本機ip,如看到下圖則nginx搭建成功,sudo ./nginx -s stop
可退出伺服器
新增rtmp視訊流伺服器
第一步:從github上克隆nginx-rtmp-moudle到/usr/local目錄下(注意,需要將這個檔案克隆/usr/lcoal目錄下)
git clone https://github.com/arut/nginx-rtmp-module.git
第二步:編譯安裝(注意:下面標記色的地方,你要換一下你自己使用者的目錄!!!!)
cd /usr/local/nginx-1.8.1
sudo ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/pcre-8.38 --with-zlib=/usr/local/zlib-1.2.11 --with-openssl=/usr/local/openssl-1.0.2j --with-http_ssl_module
--add-module=/home/wangwei/nginx-rtmp-module
sudo make
sudo make install
wangwei/nginx-rtmp-module
sudo make
sudo make install
第三步:切換至conf目錄cd /usr/local/nginx/conf
,備份一個nginx.conf,編輯nginx.confsudo nano nginx.conf
,將下面的配置替換原來nginx.conf文字中的內容。
#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;
}
rtmp {
server {
listen 1935;
application myapp{
live on;
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
}
}
http {
server {
listen 80;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx/html/nginx-rtmp-module/;
}
location /control {
rtmp_control all;
}
location /rtmp-publisher {
root /usr/local/nginx/html/nginx-rtmp-module/test;
}
location / {
root /usr/local/nginx/html/nginx-rtmp-module/test/www;
}
}
}
第四步:現在可以測試直播推流。
1、首先必須安裝ffmpeg,如果沒有安裝,安裝ffmpeg,ubuntu超簡單安裝ffmpeg
2、上傳一個視訊到/usr/local目錄下,例如上傳的為test.mp4
3、啟動nginx,開始推流:
ffmpeg -re -i /usr/local/test.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://localhost:1935/myapp/test123
4、windows中使用potplayer開啟rtmp://你的ip:1935/test/test123 ,開啟成功的話,表示安裝完成。
第五步:使用webcam來直播推流
1、nginx.conf配置檔案需要的內容放在對應的目錄下
mkdir /usr/local/nginx/html/nginx-rtmp-module
cp /root/nginx-rtmp-module/test /usr/local/nginx/html/nginx-rtmp-module/
cp /root/nginx-rtmp-module/stat.xsl /usr/local/nginx/html/nginx-rtmp-module/
2、然後修改/usr/local/nginx/html/nginx-rtmp-module/test/www目錄下的index.html和record.html,將這個裡面的localhost修改為本機的ip,例如我的ip是192.168.25.133,我就把localhost換為如下。
3、重啟nginx。使用ie瀏覽器開啟你的ip,然後點選recoder,點選啟動攝像頭,你就可以在index.html看到直播的效果了。如圖:
4、基本的已經處理完了,以後接著更新