原帖地址:https://www.cnblogs.com/smallSevens/p/8191238.htmljavascript
小程序升級實時音視頻錄製及播放能力,開放 Wi-Fi、NFC(HCE) 等硬件鏈接功能。同時提供按需加載、自定義組件和更多訪問層級等新特性,加強了第三方平臺的能力,以知足日趨豐富的業務需求。html
爲更高效地鏈接用戶與商家,小程序提供了實時音視頻錄製及播放組件。符合類目要求的小程序自助開通後,可自建或使用雲服務,實現單向、雙向甚至多向的音視頻功能,如在線授課、遠程諮詢、視頻客服,以及多人會議等。java
2017-12-26微信小程序推送了這樣一條消息,文檔,代碼也是簡單的一筆帶過,用戶須要自建或使用雲服務,實現單向、雙向甚至多向的音視頻功能。目前僅支持 flv, rtmp 格式。nginx
本篇博客經過介紹使用nginx的rtmp模塊來使nginx服務支持rtmp協議,能夠完成推流和播放的功能。git
Linux (centos7),Nginx (openresty),nginx-rtmp-modulegithub
因爲以前已經安裝過了openresty,詳見博文。這裏咱們只須要安裝nginx-rtmp-module模塊便可。web
下載並解壓模塊:小程序
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip unzip master.zip
中止原來的Nginx服務:swift
nginx -s stop
配置並編輯:windows
./configure --add-module=/home/nginx-rtmp-module-master make
複製Nginx:
## 路徑根據本身軟件路徑自行配置 cp /home/openresty/build/nginx-1.11.2/objs/nginx /usr/local/openresty/nginx/sbin
配置rtmp服務:
rtmp { #RTMP服務 server { listen 1935; #//服務端口 chunk_size 4096; #//數據傳輸塊的大小 application vod { play /opt/video; #//視頻文件存放位置。 } application live{ #直播 live on; } } }
檢查並啓動服務:
nginx -t nginx
上傳一個視頻文件test.flv至/opt/video目錄。
下載一個VLC media player,工具欄-媒體-打開網絡串流-網絡,而後偶輸入網絡URL(rtmp://ip:port/vod/test.flv),點擊播放。
實時回看視頻服務器的配置:
events { use epoll;# 選擇epoll模型能夠達到最佳的IO性能 worker_connections 1024; } rtmp { #RTMP服務 server { listen 1935; #//服務端口 chunk_size 4096; #//數據傳輸塊的大小 application vod { play /opt/video; #//視頻文件存放位置。 } application live{ #直播 live on; hls on; #這個參數把直播服務器改形成實時回放服務器。 wait_key on; #對視頻切片進行保護,這樣就不會產生馬賽克了。 hls_path /opt/video/hls; #切片視頻文件存放位置。 hls_fragment 600s; #設置HLS片斷長度。 hls_playlist_length 10m; #設置HLS播放列表長度,這裏設置的是10分鐘。 hls_continuous on; #連續模式。 hls_cleanup on; #對多餘的切片進行刪除。 hls_nested on; #嵌套模式。 } } }
HTTP服務:
server { listen 80; server_name rtmp.52itstyle.com; location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /home/nginx-rtmp-module-master/; } location / { root html; index index.html index.htm; } location /live { #這裏也是須要添加的字段。 types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } alias /opt/video/hls; expires -1; add_header Cache-Control no-cache; add_header Access-Control-Allow-Origin *; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
<live-pusher webviewId="push" url="rtmp://ip:port/live/itstyle" mode="HD" autopush ="true" bindstatechange="statechange" beauty = "10" whiteness="10" style="width: 300px; height: 225px;" />
手機打開小程序頁面,進行視頻推流服務,稍後咱們會在/opt/video/hls/itstyle 目錄下發現一些ts文件和index.m3u8文件。
注意 url中的itstyle能夠隨便定義,至關於一個房間標識。
PC 直播是在 PC(windows/mac)上藉助安裝的推流軟件 OBS(推薦)或者 XSplit 向RTMP視頻雲的推流地址,推送通過壓縮編碼現場活動、教學、投影或者遊戲等畫面,同時觀衆能夠經過和推流地址相對應的播放地址收看實時畫面。
PC 直播流程很是簡單,主要步驟:
推薦閱讀:第三方推流工具使用指南
xiaozhibo.html:
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, shrink-to-fit=no"> <title>小直播</title> <meta name="keywords" content=""> <meta name="description" content="小直播——直播分享"> <meta name="format-detection" content="telephone=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="msapplication-tap-highlight" content="no"> <script src="//imgcache.qq.com/open/qcloud/video/vcplayer/TcPlayer-2.2.1.js" charset="utf-8"></script> </head> <body> <div id="id_test_video" style="width:100%; height:auto;"></div> <script type="text/javascript"> var player = new TcPlayer('id_test_video', { "m3u8": "http://rtmp.52itstyle.com/live/itstyle002/index.m3u8", "autoplay" : true,//iOS下safari瀏覽器,以及大部分移動端瀏覽器是不開放視頻自動播放這個能力的 "coverpic" : "https://blog.52itstyle.com/usr/uploads/2017/12/2652725976.jpg", "width" : '480',//視頻的顯示寬度,請儘可能使用視頻分辨率寬度 "height" : '320'//視頻的顯示高度,請儘可能使用視頻分辨率高度 }); </script> </body> </html>
跨域問題crossdomain.xml放置在網站根目錄:
<cross-domain-policy> <allow-access-from domain="*.qq.com" secure="false"/> </cross-domain-policy>
測試訪問地址:http://rtmp.52itstyle.com/xiaozhibo.html
參考文檔:
http://www.cnblogs.com/zx-admin/p/5783523.html
https://cloud.tencent.com/document/product/267
https://github.com/arut/nginx-rtmp-module/wiki/Directives#rtmp
做者: 小柒
出處: https://blog.52itstyle.com
分享是快樂的,也見證了我的成長曆程,文章大多都是工做經驗總結以及平時學習積累,基於自身認知不足之處在所不免,也請你們指正,共同進步。