当前位置:Gxlcms > PHP教程 > 利用云视频实现在线教育和主播系统

利用云视频实现在线教育和主播系统

时间:2021-07-01 10:21:17 帮助过:40人阅读

最近主播跟游戏主播很火啊,相对其他web应用,视频直播相对来说还是有点复杂。使用FMS搭建了服务端测试一下,直播还是不够稳定。后来试了下阿里云视频服务,感觉还可以,但是它没有提供客户端。然后找到了网易云视频,它有提供了客户端,试用一下,网易云延迟比阿里云会低点,然后就选他作为视频直播服务。网易云的api示例是java的,问客服有没有php的,然后发给我一个网易云信的api。没办法自己写个,接口也简单。

  1. class v163Class{
  2. private $AppKey; //开发者平台分配的AppKey
  3. private $AppSecret; //开发者平台分配的AppSecret,可刷新
  4. private $Nonce; //随机数(最大长度128个字符)
  5. private $CurTime; //当前UTC时间戳,从1970年1月1日0点0 分0 秒开始到现在的秒数(String)
  6. private $CheckSum; //SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写)
  7. const HEX_DIGITS = "0123456789abcdef";
  8. public function __construct($AppKey,$AppSecret){
  9. $this->AppKey = $AppKey;
  10. $this->AppSecret = $AppSecret;
  11. }
  12. /**生成验证码**/
  13. public function checkSumBuilder(){
  14. //此部分生成随机字符串
  15. $hex_digits = self::HEX_DIGITS;
  16. $this->Nonce;
  17. for($i=0;$i<128;$i++){ //随机字符串最大128个字符,也可以小于该数
  18. $this->Nonce.= $hex_digits[rand(0,15)];
  19. }
  20. $this->CurTime = (string)(time()); //当前时间戳,以秒为单位
  21. $join_string = $this->AppSecret.$this->Nonce.$this->CurTime;
  22. $this->CheckSum = sha1($join_string);
  23. }
  24. /*****post请求******/
  25. public function postDataCurl($url,$data=array()){
  26. $this->checkSumBuilder(); //发送请求前需先生成checkSum
  27. if(!empty($data)){
  28. $json=json_encode($data);
  29. }else{
  30. $json="";
  31. }
  32. $timeout = 5000;
  33. $http_header = array(
  34. 'AppKey:'.$this->AppKey,
  35. 'Nonce:'.$this->Nonce,
  36. 'CurTime:'.$this->CurTime,
  37. 'CheckSum:'.$this->CheckSum,
  38. 'Content-Type: application/json;charset=utf-8;',
  39. 'Content-Length: ' . strlen($json)
  40. );
  41. $ch = curl_init();
  42. curl_setopt ($ch, CURLOPT_URL, $url);
  43. curl_setopt ($ch, CURLOPT_POST, 1);
  44. curl_setopt ($ch, CURLOPT_POSTFIELDS, $json);
  45. curl_setopt ($ch, CURLOPT_HEADER, false);
  46. curl_setopt ($ch, CURLOPT_HTTPHEADER,$http_header);
  47. curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,false);
  48. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  49. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  50. $result = curl_exec($ch);
  51. if (false === $result) {
  52. $result = curl_errno($ch);
  53. }
  54. curl_close($ch);
  55. return json_decode($result,true) ;
  56. }
  57. /***频道添加***/
  58. public function channel_add($name,$type=0){
  59. $url="https://vcloud.163.com/app/channel/create";
  60. return $data=$this->postDataCurl($url,array("name"=>$name,"type"=>$type));
  61. }
  62. /****频道更新*****/
  63. public function channel_update($name,$cid,$type=0){
  64. $url="https://vcloud.163.com/app/channel/update";
  65. return $data=$this->postDataCurl($url,array("name"=>$name,"cid"=>$cid,"type"=>$type));
  66. }
  67. /****频道删除******/
  68. public function channel_delete($cid){
  69. $url="https://vcloud.163.com/app/channel/delete";
  70. return $data=$this->postDataCurl($url,array("cid"=>$cid));
  71. }
  72. /****获取频道信息******/
  73. public function channel_get($cid){
  74. $url="https://vcloud.163.com/app/channelstats";
  75. return $data=$this->postDataCurl($url,array("cid"=>$cid));
  76. }
  77. /***
  78. 获取频道列表
  79. records int 单页记录数,默认值为10 否
  80. pnum int 要取第几页,默认值为1 否
  81. ofield String 排序的域,支持的排序域为:ctime(默认) 否
  82. sort int 升序还是降序,1升序,0降序,默认为desc 否
  83. **/
  84. public function channel_list($option=array("records"=>10,"pnum"=>1,"ofield"=>"ctime","sort"=>1)){
  85. $url="https://vcloud.163.com/app/channellist";
  86. return $data=$this->postDataCurl($url,$option);
  87. }
  88. /**重新获取推流地址***/
  89. public function channel_reset($cid){
  90. $url="https://vcloud.163.com/app/address";
  91. return $data=$this->postDataCurl($url,array("cid"=>$cid));
  92. }
  93. /*****
  94. 设置频道为录制状态
  95. cid String 频道ID 是
  96. needRecord int 1-开启录制; 0-关闭录制 是
  97. format int 1-flv; 0-mp4 是
  98. duration int 录制切片时长(分钟),默认120分钟 否
  99. filename String 录制后文件名,格式为filename_YYYYMMDD-HHmmssYYYYMMDD-HHmmss,
  100. 文件名录制起始时间(年月日时分秒) -录制结束时间(年月日时分秒) 否
  101. ****/
  102. public function channel_setRecord($cid,$option=array()){
  103. $url="https://vcloud.163.com/app/channel/setAlwaysRecord";
  104. return $data=$this->postDataCurl($url,$option);
  105. }
  106. /****暂停频道*****/
  107. public function channel_pause($cid){
  108. $url="https://vcloud.163.com/app/channel/pause";
  109. return $data=$this->postDataCurl($url,array("cid"=>$cid));
  110. }
  111. /****批量暂停频道****/
  112. public function channel_pauselist($cidList){
  113. $url="https://vcloud.163.com/app/channellist/pause";
  114. return $data=$this->postDataCurl($url,array("cidList"=>$cidList));
  115. }
  116. /****恢复频道*****/
  117. public function channel_resume($cid){
  118. $url="https://vcloud.163.com/app/channel/resume";
  119. return $data=$this->postDataCurl($url,array("cid"=>$cid));
  120. }
  121. /****批量恢复频道****/
  122. public function channel_resumelist($cidList){
  123. $url="https://vcloud.163.com/app/channellist/resume";
  124. return $data=$this->postDataCurl($url,array("cidList"=>$cidList));
  125. }
  126. /****获取频道的视频地址*****/
  127. public function channel_videolist($cid){
  128. $url="https://vcloud.163.com/app/videolist";
  129. return $data=$this->postDataCurl($url,array("cid"=>$cid));
  130. }
  131. }

网易有提供window客户端,在使用的时候出现卡顿现象,所以还是直接使用OBS。OBS是款免费的视频直播客户端,配置也简单。在串流里填下url即可开始直播。

160665-20160713134444904-568690032.png

160665-20160713134458654-630134502.png

这样就可以开始直播。

播放器的话使用video.js即可。【推荐阅读:Node.js视频教程】

  1. <video id="zbvideo" class="video-js vjs-default-skin" controls preload="none" width="90%" height="398" poster="/static/images/videobg.jpg" data-setup="{}">
  2. <source src="{$data.zb_http}" />
  3. <source src="{$data.zb_hls}" type="application/x-mpegURL" />
  4. <source src="{$data.zb_rtmp}" type="rtmp" />
  5. </video>
  6. <link href="/plugin/videojs/video-js.css" rel="stylesheet">
  7. <script src="/plugin/videojs/ie8/videojs-ie8.min.js"></script>
  8. <script src="/plugin/videojs/video.js"></script>

这样就完成一个直播服务了.

添加公开课根据api自动生成直播地址,刷新直播地址,到期自动删除直播地址。

人气教程排行