直播流程
視頻直播:采集、前處理、編碼、傳輸、解碼、渲染
采集: 一般是由客戶(hù)端(IOS、安卓、PC或其它工具,如OBS)完成的,iOS是比較簡(jiǎn)單的,Android則要做些機型適配工作,PC最麻煩各種奇葩攝像頭驅動(dòng)。
前期處理: 主要是處理直播美顏,美顏算法需要用到GPU編程,需要懂圖像處理算法的人,沒(méi)有好的開(kāi)源實(shí)現,要自己參考論文去研究。難點(diǎn)不在于美顏效果,而在于GPU占用和美顏效果之間找平衡。
編碼: 要采用硬編碼,軟編碼720p完全沒(méi)希望,勉強能編碼也會(huì )導致CPU過(guò)熱燙到攝像頭。編碼要在分辨率,幀率,碼率,GOP等參數設計上找到最佳平衡點(diǎn)。
傳輸: 一般交給了CDN服務(wù)商,如:阿里云、騰訊云。
解碼: 是對之前編碼的操作,進(jìn)行解碼,在 web 里需要解碼是hls。
渲染: 主要用播放器來(lái)解決,web中常用到的播放器有video.js,更多:html5-dash-hls-rtmp
下面是騰訊云直播方案的整個(gè)流程圖:

支持的直播流輸入協(xié)議是
RTMP 用于拉取和發(fā)布的流
RTSP 為拉和宣布的流
用于HTTP和UDP流的 MPEG-TS
SRT 用于聽(tīng),拉和集合模式
UDT 用于聽(tīng),拉和集合模式
HLS 為拉流
單路路實(shí)時(shí)編碼流傳遞(RTMP)

多路實(shí)時(shí)編碼流傳遞(RTMP)

服務(wù)與模塊
1、Openresty下載
https://openresty.org/download/openresty-1.11.2.3.tar.gz
2、nginx-ts-module下載
git clone https://github.com/arut/nginx-ts-module.git
3、ffmpeg 下載安裝
動(dòng)態(tài)編譯安裝
(1)nginx-rtmp-module下載
(2)和安裝nginx-ts-module模塊一樣動(dòng)態(tài)編譯安裝既可以,最后別忘記了的在配置文件load nginx-rtmp-module.so文件
(3)按照這個(gè)順序:OBS => nginx-rtmp => nginx-ts推流,OBS也可以是別的網(wǎng)絡(luò )推流設備
(4)通過(guò)以上我們可以不直接使用ffmpeg 去推流了,而是在Windows端口可以通過(guò)OBS很簡(jiǎn)單的去推流了
(5)使用VLC播放器測試,結果OK!
nginx.conf
# vim /opt/openresty/nginx/conf/nginx.conferror_log logs/error.log;pid logs/nginx.pid;load_module "/opt/openresty/nginx/modules/ngx_http_ts_module.so"; # 加載模塊events {}http { server { listen 8000; location / { root html; } location /publish/ { ts; ts_hls path=/var/media/hls segment=10s; ts_dash path=/var/media/dash segment=10s; client_max_body_size 0; } location /play/ { add_header Cache-Control no-cache; add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range'; add_header 'Access-Control-Allow-Headers' 'Range'; types { application/x-mpegURL m3u8; application/dash+xml mpd; video/MP2T ts; video/mp4 mp4; } alias /var/media/; } }}流媒體存放文件夾建立
1、Openresty環(huán)境配置
apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential
2、動(dòng)態(tài)編譯安裝
./configure --prefix=/opt/openresty --with-luajit --without-http_redis2_module --with-http_iconv_module --add-dynamic-module=/root/nginx-ts-module...make -j4...sudo make install
3、配置文件
cd /var & makedir media cd media & makedir hls & makedir dash
4、FFmpeg推流
ffmpeg -re -i rtmp://live.hkstv.hk.lxdns.com/live/hks -bsf:v h264_mp4toannexb -c copy -f mpegts http://127.0.0.1:8000/publish/sintel
5、客戶(hù)端播放
<script src="http://cdn.dashjs.org/latest/dash.all.min.js"></script><style> video { width: 640px; height: 360px; }</style><div> <video data-dashjs-player autoplay src="http://1127.0.0.1:8000/play/dash/sintel/index.mpd" controls></video></div>6、如果不使用 ffmpeg 直接拉流到http://127.0.0.1:8000/publish/sintel 服務(wù)的解決方案?
7、總結,一切順利通過(guò)。
通過(guò)SSL加密和公開(kāi)HLS媒體的來(lái)源(HLS)

HLS、MPEG-DASH多路輸入/輸出流(HLS、MPEG-DASH)

編譯安裝
1、下載nginx-rtmp-module模塊:
git clone https://github.com/arut/nginx-rtmp-module.git
2、配置 --with-http_xslt_module 時(shí)提示 the HTTP XSLT module requires the libxml2/libxslt libraries,安裝以下:
sudo apt-get install libxml2 libxml2-dev libxslt-devsudo apt-get install libgd2-xpm libgd2-xpm-dev
3、通過(guò)configure命令生成Makefile文件,為下一步的編譯做準備:
./configure --prefix=/opt/openresty --with-luajit --without-http_redis2_module --with-http_iconv_module \ --with-http_stub_status_module --with-http_xslt_module --add-dynamic-module=/root/nginx-ts-module --add-dynamic-module=/root/nginx-rtmp-module
4、如果報下面的錯誤
platform: linux (linux) you need to have ldconfig in your PATH env when enabling luajit.
是因為找不到命令ldconfig, 這個(gè)命令一般是在/sbin/目錄下的,所以先執行
export PATH=$PATH:/sbin
5、如果出現:./configure: error: the HTTP XSLT module requires the libxml2/libxslt 錯誤,安裝以下:
sudo apt-get install libxml2 libxml2-dev libxslt-dev
nginx.conf 配置
# vim /opt/openresty/nginx/conf/nginx.confuser www;worker_processes 1;error_log logs/error.log;pid logs/nginx.pid;load_module "/opt/openresty/nginx/modules/ngx_http_ts_module.so";load_module "/opt/openresty/nginx/modules/ngx_rtmp_module.so";events { worker_connections 1024;} http { server { listen 8000; # This URL provides RTMP statistics in XML location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root html; } location /hls { # Serve HLS fragments add_header Cache-Control no-cache; add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range'; add_header 'Access-Control-Allow-Headers' 'Range'; types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /tmp; } location /dash { # Serve DASH fragments add_header Cache-Control no-cache; add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range'; add_header 'Access-Control-Allow-Headers' 'Range'; types { application/dash+xml mpd; video/mp4 mp4; } root /tmp; } } } rtmp { listen 1935; chunk_size 4000; idle_streams off; ping 30s; notify_method get; server { listen 1935; chunk_size 4000; drop_idle_publisher 10s; idle_streams off; application live { live on; } application hls { live on; hls on; hls_path /tmp/hls; } # MPEG-DASH is similar to HLS application dash { live on; dash on; dash_path /tmp/dash; } } }拷貝xml文件:cp /root/nginx-rtmp-module/stat.xsl /opt/openresty/nginx/html
流狀態(tài)查看:http://127.0.0.1:8000/stat
OBS推流地址:rtmp://127.0.0.1/dash/123
VLC觀(guān)看RTMP直播流:rtmp://127.0.0.1/dash/123
DASH格式HTTP播放
<script src="http://cdn.dashjs.org/latest/dash.all.min.js"></script><style> video { width: 640px; height: 360px; }</style><div> <video data-dashjs-player autoplay src="http://127.0.0.1:8000/dash/123.mpd" controls></video></div>功能特點(diǎn)
支持RTMP、HTTP-FLV、HLS、HTML5等協(xié)議,面向Windows、iOS、Android等終端提供穩定流暢的視頻直播、點(diǎn)播服務(wù),支持微信直播和微信點(diǎn)播, 可部署在局域網(wǎng)和互聯(lián)網(wǎng),實(shí)現私有云和公有云應用,單設備高并發(fā)。
多屏播放 支持Flash、HTML5播放,兼容Windows、Android、iOS、Mac等操作系統;
轉碼、上傳 轉碼、上傳一體化設計,使視頻資源轉碼后可立即面向互聯(lián)網(wǎng)進(jìn)行發(fā)布;
嵌入、分享 上傳視頻后一鍵生成視頻地址,以供用戶(hù)分享、嵌入到網(wǎng)站、軟件系統中;
豐富接口 提供編程語(yǔ)言無(wú)關(guān)化的RESTful API接口,可以很簡(jiǎn)單的進(jìn)行二次開(kāi)發(fā);
強大的WEB管理系統
采用業(yè)界優(yōu)秀的流媒體框架模式設計,服務(wù)運行輕量、高效、穩定、可靠、易維護,是移動(dòng)互聯(lián)網(wǎng)時(shí)代貼近企業(yè)點(diǎn)播、直播需求的一款流媒體方案,可以形成一套完整的視頻直播、錄播解決方案,滿(mǎn)足用戶(hù)在各種行業(yè)場(chǎng)景的流媒體業(yè)務(wù)需求。
1、視頻上傳
2、點(diǎn)播管理
3、直播流管理
4、正在直播
5、分享集成
6、接口在線(xiàn)調試
應用場(chǎng)景
視頻門(mén)戶(hù)網(wǎng)站、會(huì )議活動(dòng)現場(chǎng)直播、網(wǎng)絡(luò )電視臺、遠程監控、在線(xiàn)實(shí)時(shí)課堂、 科研方向等。
聯(lián)系客服