背景介紹
本文使用的流媒體服務器的搭建是基于rtmp(Real Time Message Protocol)協議的,rtmp協議是應用層的協議,要依靠底層的傳輸層協議,好比tcp協議來保證信息傳輸的可靠性。最后提供了一個不錯的測試方案。nginx
流媒體服務器依賴的服務:
1.nginx 服務器;
2.nginx服務器安裝須要依賴的服務 OpenSSL、pcre、zlib、 c++、gcc等。c++服務器環境是Centos 8.1 64 位,用的vmvare,也可以使用現在流行的云服務器 。
安裝nginx
首先須要注意的是雖然nginx可使用yum安裝,可是yum安裝使用的都是編譯后的文件進行安裝,而且后面所須要的rtmp模塊通常在yum源里面是找不到的,因此要想將rtmp也安裝進來,咱們選擇源碼安裝,通過步驟配置,編譯,安裝。
本文是使用nginx源碼,自行編譯安裝。首先是下載源碼,咱們先用wget將nginx次新版本下載下來,而后將web模塊源碼從github上下載下來。
cd
mkdir rtmpsc #建立源碼目錄 后面的源碼都放在這個目錄
cd rtmpsc
wget http://nginx.org/download/nginx-1.20.1.tar.gz #從github服務器上將nginx的源代碼下載下來
tar -zxvf nginx-1.20.1.tar.gz
yum -y install git #安裝git
git clone https://github.com/arut/nginx-rtmp-module.git #將rtmp模塊的源碼下載下來
下載依賴模塊源碼pcre、OpenSSL、zlib 若是機器上已經安裝了這些模塊就不須要了
rpm -qa|grep 模塊名字 #查詢安裝的模塊的包信息
#下載OpenSSL源碼包,用來做加密
wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1j.tar.gz
tar -zxvf openssl-1.1.1j.tar.gz
#用來做正則表達式,可以重寫流路徑的指向
wget https://ftp.pcre.org/pub/pcre/pcre-8.45.tar.gz #下載pcre源碼包,2.0不行,更換了
tar -zxvf pcre-8.45.tar.gz
#做視頻流的壓縮
wget http://www.zlib.net/zlib-1.2.11.tar.gz #下載zlib包源碼
tar -zxvf zlib-1.2.11.tar.gz
tar -zxvf 包名 #解壓各個包源碼
附一張目錄結構圖
找到nginx源碼包中 configure,如下腳本就是基于configure來實現的,在本機上configure 命令在nginx的目錄下,當前目錄/home/atguigu/rtmpsc/nginx-1.20.1 就在這個目錄下編寫腳本vi build.sh 加入如下內容:
./configure --prefix=/home/root/rtmpsc/nginx \
--with-pcre=../pcre-8.45 \
--with-openssl=../openssl-1.1.1j \
--with-zlib=../zlib-1.2.11 \
--with-http_v2_module \
--with-http_flv_module \
--with-http_mp4_module \
--add-module=/home/root/rtmpsc/nginx-rtmp-module/
注意:
1.網上有些教程是pcre進行make makeinstall等操作后,然后把生成的目錄作為with--xx后跟的路徑,試了下不可行
2.這里pcre選擇pcre會報錯,使用pcre就行
保存后給文件賦予操做權限,再執行
chmod 777 build.sh #賦予權限
./build.sh #執行腳本
make
檢查配置沒有問題
make install
安裝成功
現在的安裝路徑是nginx這個
配置下nginx的環境變量,可以全局使用nginx命令
#當前不是root用戶,得取得管理權限
sudo vim /etc/profile
#在最下面加入
export NGINX_HOME="/home/root/rtmpsc/nginx/sbin" # do not add "bin" at the end of the path**
export PATH="$NGINX_HOME:$PATH"
#保存后(關于vim的編輯命令,自行百度)
#然后讓profile生效
source /etc/profile
nginx -v
nginx安裝完畢了。
現在需要做些變更:
1.云服務器控制臺對相應的端口開放;
2.而且Linux機器對默認端口80開放(若是想修改默認端口自行百度),因為本機80端口被Apache 服務器占用了,因此將nginx默認端口改成8086
#找到nginx/conf/nginx.conf文件,然后加入rtmp的配置
#注意可能像live或者server前面,有來自html或者其他的奇怪空格,需要刪除然后重新敲出來
rtmp {
server{
listen 1935;
chunk_size 4000;
application cctvf {
#rtmp推流請求路徑 (切記路徑錯了會推不上流)
live on; #開啟實時
#hls on #開啟hls
##hls_path /home/atguigu/rtmpsc/cctvf; #rtmp推流請求路徑,文件存放路徑
##hls_fragment 5s; #每個TS文件包含5秒的視頻內容
}}
}
#全文
#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;
chunk_size 4000;
application cctvf {
#rtmp推流請求路徑 (切記路徑錯了會推不上流)
live on; #開啟實時
#hls on #開啟hls
##hls_path /home/atguigu/rtmpsc/cctvf; #rtmp推流請求路徑,文件存放路徑
##hls_fragment 5s; #每個TS文件包含5秒的視頻內容
}}
}
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 8086;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
# }
#}
}
#保存后,讓nginx生效
nginx -s reload
從這里下載ffmpeg for windows,FFmpeg Windows Builds - gyan.dev
下載編譯好的可用的ffmpeg.exe,推流到服務器
.\ffmpeg.exe -re -i d:\01.自己動手搭建流媒體服務器.wmv -vcodec libx264 -acodec aac -f flv rtmp://192.168.10.201:1935/cctvf/mystream
#mystream 為自定義的流標識,隨意起,不需要在nginx中標出來
這樣就可以推流了
需要最下面一行,在不斷的刷新上傳數據,表示正常上傳。
驗證
可以使用vlc Downloads - VideoLAN,下載安裝下,然后媒體->網絡串流,輸入鏈接,來進行驗證
能正常播放音視頻就算完成了。