直播平臺大體流程php
該圖片轉自其餘網絡css
實時傳輸協議有:RTMP、HLS、HDL(HTTP-FLV)html
編譯環境linux
apt-get install build-essential
nginx安裝nginx
安裝pcre(目前最新8.44)git
./configure make && make install pcre-config --version //查看版本
下載nginx-rtmp-module
源github
git 下載https://github.com/arut/nginx-rtmp-module
服務器
安裝nginx(目前最新1.17.9)網絡
./configure --add-module=../nginx-rtmp-module --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.44 make make install
啓動nginxsession
./sbin/nginx -c /usr/local/nginx/conf/nginx.conf
obs studio視頻直播錄製軟件安裝(debian儘可能9以上,由於ops有要求,否則本身編譯有點麻煩)
https://obsproject.com/download
ffmpeg轉碼安裝
https://obsproject.com/wiki/install-instructions#linux裏有安裝流程
或者低版本的
sh -c 'echo "deb http://www.deb-multimedia.org jessie main" >> /etc/apt/sources.list' apt-get update apt-get install deb-multimedia-keyring apt-get install ffmpeg
rtmp參數配置
nginx.conf點播配置
#播放地址示例: rtmp://localhost/live/qq.mp4 rtmp { #RTMP服務 server { listen 1935; #//服務端口 chunk_size 4096; #//數據傳輸塊的大小 application live { play /usr/local/nginx/video; #//視頻文件存放位置。 } } }
nginx.conf直播配置
user root; 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; } #my start 點播視頻 服務器的配置 #播放地址示例: rtmp://localhost/vod/qq.mp4 rtmp { server { listen 1935; chunk_size 4000; application show { #live on; #enable HLS #hls on; play /usr/local/nginx/video; #hls_fragment 3; #hls_playlist_length 20; } application hls { live on; #直播 #回看功能 視頻切片變成ts文件 hls on; #這個參數把直播服務器改形成實時回放服務器。 wait_key on; #對視頻切片進行保護,這樣就不會產生馬賽克了。 hls_path /usr/local/nginx/video/hls; #切片視頻文件存放位置。 hls_fragment 10s; #每一個視頻切片的時長。 hls_playlist_length 120; #總共能夠回看的事件,這裏設置的是1分鐘。 hls_continuous on; #連續模式。 #hls_cleanup on; #對多餘的切片進行刪除。 #hls_nested on; #嵌套模式。 } } } 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; location /hls { alias /usr/local/nginx/video/hls; # Disable cache #add_header Cache-Control no-cache; # CORS setup add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Expose-Headers' 'Content-Length'; # allow CORS preflight requests if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Content-Length' 0; return 204; } types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } } #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
sudo ./sbin/nginx -s reload
開啓 ffmpeg
ffmpeg -re -i /usr/local/nginx/video/test.mp4 -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -f flv rtmp://localhost/hls/stream
結果
http直接播放
<!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>直播</title> <link href="https://unpkg.com/video.js/dist/video-js.css" rel="stylesheet"> <script src="https://unpkg.com/video.js/dist/video.js"></script> <script src="https://unpkg.com/videojs-contrib-hls/dist/videojs-contrib-hls.js"></script> </head> <body> <h1>直播</h1> <video id="my_video_1" class="video-js vjs-default-skin" controls width="640" data-setup='{}'> <source src="hls/stream.m3u8" type="application/x-mpegURL"> </video> </body> </html>