1.直播是如今最熱門的,尤爲是電競的發展成功的帶動的直播的發展,各類遊戲直播月入XXX,常常聽到的一句話:某主播XXX月入百萬,不知道是真是假暫且無論,看看直播究竟是怎麼實現的,直播使用的是RTMP協議(實時消息傳輸協議),實現這個協議的方式有不少種,這裏使用nginx(一個超級強大的服務器)的rtmp-moudle模塊來實現。php
首先準備nginx安裝包和nginx-rtmp-module模塊的安裝包html
nginx安裝包:http://nginx.org/,請自行下載nginx
wget http://nginx.org/download/nginx-1.11.3.tar.gzgit
nginx-rtmp-module模塊下載
github
wget https://github.com/arut/nginx-rtmp-module/archive/master.zipthinkphp
下載完畢解壓shell
tar -zxvf nginx-1.11.3.tar.gz數組
unzip master.zip瀏覽器
解壓完畢進入nginx-1.11.3文件夾服務器
.
/configure
--add-module=../arut-nginx-rtmp-module
make && make install
安裝完畢!
運行nginx:
/usr/local/nginx/sbin/nginx
運行:ifconfig查看ubantu的ip地址:
打開瀏覽器輸入:你本機的ip看到nginx的歡迎頁面,說明安裝成功,如圖:
而後:
cd /usr/local/nginx/conf
nginx的配置文件可能沒有修改的權限
修改權限:
sudo chmod 777conf/
編輯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; } 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 80; server_name localhost; #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 /stat { #第二處添加的location字段。
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl { #第二處添加的location字段。
root /usr/local/nginx/nginx-rtmp-module/; #nginx-rtmp-module是模塊下載的位置
}
#==========瀏覽器監視直播流信息配置結束==============
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; # } #} } rtmp { server { listen 1935; chunk_size 4096; application live { live on; publish_notify on; on_publish http://192.168.0.199/rtmp/index.php; //受權驗證地址 record off; } } }
index.php
<?php
/*
$_POST返回數組值
array (
'app' => 'live',
'flashver' => 'FMLE/3.0 (compatible; FMSc/1.0)',
'swfurl' => 'rtmp://192.168.0.115/live',
'tcurl' => 'rtmp://192.168.0.115/live',
'pageurl' => '',
'addr' => '192.168.0.199',
'clientid' => '79',
'call' => 'publish',
'name' => 'test3',
'type' => 'live',
'pass' => '123456',
)
*/ $name = $_POST['name']; #$name = test3 $pass = $_POST['pass']; $pass = 123456 if(empty($name) || empty($pass)){ echo "串碼流不正確"; // 這個是thinkphp5的返回頭信息的函數 header("HTTP/1.1 404 Not Found"); header("Status: 404 Not Found"); exit; } echo "正常"; ?>
用ffmpeg推流方式:
ffmpeg -re -i 1255199778_3032750917_20170326113341.mp4 -c copy -f flv rtmp://192.168.0.115/live/test4?pass=123456
用obs推流方式:
test3至關於帳號,pass至關於密碼
檢測直播流信息 nginx.conf配置方式
location /stat { #第二處添加的location字段。 rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { #第二處添加的location字段。 root /usr/local/nginx/nginx-rtmp-module/; #nginx-rtmp-module是模塊下載的位置 }
配置完成後,在瀏覽器輸入:http://192.168.0.115/stat
關於nginx-stmp模塊的回調接口參考:http://www.ptbird.cn/nginx-rtmp-multi-channel.html
關於ffmpeg相關命令 :http://www.xuebuyuan.com/1740527.html