物聯網架構成長之路(42)-直播流媒體入門(RTMP篇)

1. 安裝RTMP流媒體服務器
  之前其實我是利用Nginx-RTMP-module搭建過RTMP流媒體服務器,並實現了鑑權功能。參考https://www.cnblogs.com/wunaozai/p/9427730.html,看看發佈時間,已是一年多之前的事情了,感概時間過得好快啊。
  先在Nginx官網【http://nginx.org/en/download.html】下載源碼包,而後在github【https://github.com/arut/nginx-rtmp-module】下載插件包。javascript

 1 #下載
 2 wget http://nginx.org/download/nginx-1.16.1.tar.gz
 3 git clone https://github.com/arut/nginx-rtmp-module
 4 #解壓
 5 tar -zxvf nginx-1.16.1.tar.gz
 6 #編譯,在編譯檢查configure過程當中若是出現缺乏包的,請經過apt-get獲取便可
 7 cd nginx-1.16.1
 8 ./configure --prefix=/opt/nginx --with-stream --with-http_ssl_module --with-stream_ssl_module --with-debug --add-module=../nginx-rtmp-module
 9 make
10 make install

  基本的Nginx配置,以下,配置了RTMP實時流轉發和HLS實時流轉發兩種。更多配置參考官方文檔【https://github.com/arut/nginx-rtmp-module/blob/master/README.md】css

 1 rtmp {
 2     server {
 3         listen 1935;
 4         chunk_size 4096;
 5         access_log logs/rtmp_access.log;
 6         application rtmp {
 7             live on;
 8         }
 9     }
10 }

 

2. 測試推送RTMP流到服務器,並客戶端拉取流數據
  推流:html

ffmpeg -re -i 003.mov -c copy -f flv rtmp://172.16.23.202/rtmp/room


  拉流:html5

ffplay -i rtmp://172.16.23.202/rtmp/room


  也能夠使用微信小程序【騰訊視頻雲】裏面有推流和拉流工具
 
  拉流測試也能夠使用基於Flash的js庫。這種庫,仍是有不少的。ckplayer【www.ckplayer.com】GrindPlayer【https://github.com/kutu/GrindPlayer】videojs【https://videojs.com】這些庫基本都差很少,都是支持RTMP和HLS,RTMP使用Flash來播放。
  瀏覽器播放RTMP直播流(使用ckplayer)【http://www.ckplayer.com/down/】java

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title>ckplayer</title>
 6         <style type="text/css">body{margin:0;padding:0px;font-family:"Microsoft YaHei",YaHei,"微軟雅黑",SimHei,"黑體";font-size:14px}</style>
 7     </head>
 8     <body>
 9         <div id="video" style="width: 600px; height: 400px;"></div>
10         <script type="text/javascript" src="../ckplayer/ckplayer.js"></script>
11         <script type="text/javascript">
12             var videoObject = {
13                 container: '#video', //容器的ID或className
14                 variable: 'player',//播放函數名稱
15                 autoplay:false,
16                 live:true,
17                 video: 'rtmp://172.16.23.202/rtmp/room'
18             };
19             var player = new ckplayer(videoObject);
20         </script>
21     </body>
22 </html>

 

3. 測試推送RTMP流到服務器,服務器提供HLS直播流
  nginx.conf 配置文件nginx

 1 http{
 2 ...
 3     server {
 4         listen 1936;
 5         location /html {
 6             root /tmp;
 7         }
 8         location /hls {
 9             types {
10                 application/vnd.apple.mpegurl m3u8;
11                 video/mp2t ts;
12             }
13             root /tmp;
14             add_header Cache-Control no_cache;
15         }
16 
17         location /dash {
18             root /tmp;
19             add_header Cache-Control no-cache;
20         }
21     }
22 }
23 rtmp {
24     server {
25         listen 1935;
26         chunk_size 4096;
27         access_log logs/rtmp_access.log;
28         application rtmp {
29             live on;
30         }
31         application hls{
32             live on;
33             hls on;
34             hls_path /tmp/hls;
35 #hls_fragment 5s;
36         }
37         application dash{
38             live on;
39             dash on;
40             dash_path /tmp/dash;
41         }
42     }
43 }

  服務器緩存TS文件
git

  瀏覽器播放HLS直播流(這裏使用videojs,10秒左右延時)github

 1 <!DOCTYPE html>
 2 <html lang="cn">
 3     <head>
 4         <meta chartset="UTF-8">
 5         <meta http-equiv="X-UA-Compatible" content="ie=edge">
 6         <title>Test</title>
 7         <link href="https://cdn.bootcss.com/video.js/7.6.5/video-js.min.css" rel="stylesheet">
 8         <script src="https://cdn.bootcss.com/video.js/7.6.5/video.min.js"></script>
 9     </head>
10 
11     <body>
12         <h1>Live</h1>
13         <video id="example-video" width="960" height="540" class="video-js vjs-default-skin" controls poster="001.png">
14             <!--<source src="http://ivi.bupt.edu.cn/hls/lytv.m3u8"></source>-->
15             <source src="http://172.16.23.202:1936/hls/room.m3u8"></source>
16         </video>
17 
18         <script type="text/javascript">
19             var player = videojs('example-video')
20             player.play();
21         </script>
22     </body>
23 </html>

  瀏覽器運行界面小程序

1   ffplay -i http://172.16.23.202:1936/hls/room.m3u8

  運行效果圖微信小程序

Dash流(找不到合適的播放器)

http://172.16.23.202:1936/dash/room.mpd

 

4. RTSP轉RTMP協議
  ffmpeg 將rtsp流媒體轉換成RTMP流

1 ffmpeg -i "rtsp://172.16.20.197:5454/test.rtsp" -vcodec copy -acodec copy -f flv -an "rtmp://172.16.23.202:1935/rtmp/room"
2 ffmpeg -i "rtsp://172.16.20.197:5454/test.rtsp" -vcodec copy -acodec copy -f flv -an "rtmp://172.16.23.202:1935/hls/room"

  以下圖所示,FFServer先提供RTSP流。而後利用FFMpeg將RTSP流轉換爲RTMP流,並推送至Nginx-RTMP流媒體服務器。

  而後瀏覽器能夠利用上面的js庫,播放RTMP流,或者播放HLS流。

 

 

5. 本章小結
  最後總結一下,就是使用CKPlayer播放器,若是對時延不敏感的使用HLS,若是要求高的使用RTMP。雖然Flash慢慢被淘汰,可是在微信自帶瀏覽器中仍是有比較好的支持。並且各大廠商提供的直播流服務,基本都是以RTMP爲主流。

  RTSP和RTMP簡單推流拉流示意圖

 

參考資料:

  阿里雲提供的播放器: https://player.alicdn.com/aliplayer/setting/setting.html
  ffmpeg經常使用功能: https://www.jianshu.com/p/c141fc7881e7
  Nginx-rtmp 直播實時流搭建:http://www.javashuo.com/article/p-byzxxlrk-ct.html
  IPCamera RTSP格式: https://blog.csdn.net/hk121/article/details/83858480
  EasyDarwin提供PC工具:https://pan.baidu.com/s/1-7lZ3KM4wPl87OLx2tWjTQ
  很不錯的一個項目:https://gitee.com/Tinywan/html5-dash-hls-rtmp
  FFmpeg 處理RTMP流媒體命令:http://www.javashuo.com/article/p-hlqxteyz-kb.html

 

本文地址: http://www.javashuo.com/article/p-dvxxocso-db.html

相關文章
相關標籤/搜索