Qiniu.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Live\Lib;
  3. use Dever;
  4. Dever::apply('sdk/qiniu', 'live');
  5. class Qiniu
  6. {
  7. private $stream;
  8. private $config;
  9. private $space;
  10. # 连接
  11. public function __construct($config)
  12. {
  13. $this->config = $config;
  14. $mac = new \Qiniu\Pili\Mac($this->config['appkey'], $this->config['appsecret']);
  15. $client = new \Qiniu\Pili\Client($mac);
  16. $this->space = $client->hub($this->config['space']);
  17. }
  18. # 创建新流
  19. public function create($name)
  20. {
  21. $result = $this->space->create($name);
  22. return $this->getInfo($name);
  23. }
  24. # 查询流
  25. public function get($name)
  26. {
  27. if (!$this->stream) {
  28. $this->stream = $this->space->stream($name);
  29. }
  30. return $this->stream;
  31. }
  32. # 查询流的信息
  33. public function getInfo($name)
  34. {
  35. $stream = $this->get($name);
  36. return $stream->info();
  37. }
  38. # 获取直播状态
  39. public function getLiveStatus($name)
  40. {
  41. $stream = $this->get($name);
  42. return $stream->liveStatus();
  43. }
  44. # 列出所有流
  45. public function gets($prefix = '', $limit = 10000)
  46. {
  47. return $this->space->listStreams($prefix, $limit, '');
  48. }
  49. # 列出所有正在直播的流
  50. public function getLives($prefix = '', $limit = 10000)
  51. {
  52. return $this->space->listLiveStreams($prefix, $limit, '');
  53. }
  54. # 启动流
  55. public function start($name = '')
  56. {
  57. $stream = $this->get($name);
  58. $stream->enable();
  59. return $this->getLiveStatus($name);
  60. }
  61. # 禁用流
  62. public function stop($name = '', $num = 120)
  63. {
  64. $stream = $this->get($name);
  65. $stream->disable(time() + $num);
  66. return $this->getLiveStatus($name);
  67. }
  68. # 保存直播数据
  69. public function save($name, $start = 0, $end = 0)
  70. {
  71. $stream = $this->get($name);
  72. //$format = array("format" => "mp4");
  73. //return $stream->saveas($format, $start, $end);
  74. if ($end <= 0) {
  75. $end = time();
  76. }
  77. $data = $stream->save($start, $end);
  78. if (isset($data['fname'])) {
  79. return $data['fname'];
  80. }
  81. return '';
  82. }
  83. # 保存直播截图
  84. public function savePic($name)
  85. {
  86. $stream = $this->get($name);
  87. return $stream->snapshot(array("format" => "jpg"));
  88. }
  89. # 查看历史直播流
  90. public function history($name)
  91. {
  92. $stream = $this->get($name);
  93. return $stream->historyActivity(0, 0);
  94. }
  95. # 查询推流历史
  96. public function update($name, $convert)
  97. {
  98. $stream = $this->get($name);
  99. $stream->updateConverts($convert);
  100. return $stream->info();
  101. }
  102. # 获取推流地址
  103. public function getRtmp($name, $time = 3600)
  104. {
  105. $url = \Qiniu\Pili\RTMPPublishURL($this->config['domain_publish'], $this->config['space'], $name, $time, $this->config['appkey'], $this->config['appsecret']);
  106. return $url;
  107. }
  108. # 获取直播地址
  109. public function getPlay($name)
  110. {
  111. //高清、标清、普清
  112. $url['rtmp'] = $this->getRtmpPlay($name);
  113. $url['hls'] = $this->getHlsPlay($name);
  114. $url['hdl'] = $this->getHdlPlay($name);
  115. $url['pic'] = $this->getPicPlay($name);
  116. return $url;
  117. }
  118. # 获取直播地址
  119. public function getRtmpPlay($name)
  120. {
  121. $url = \Qiniu\Pili\RTMPPlayURL($this->config['domain_live_rtmp'], $this->config['space'], $name);
  122. return $url;
  123. }
  124. # HLS 直播地址
  125. public function getHlsPlay($name)
  126. {
  127. $url = \Qiniu\Pili\HLSPlayURL($this->config['domain_live_hls'], $this->config['space'], $name);
  128. return $url;
  129. }
  130. # HDL 直播地址
  131. public function getHdlPlay($name)
  132. {
  133. $url = \Qiniu\Pili\HDLPlayURL($this->config['domain_live_hdl'], $this->config['space'], $name);
  134. return $url;
  135. }
  136. # 截图直播地址
  137. public function getPicPlay($name)
  138. {
  139. $url = \Qiniu\Pili\SnapshotPlayURL($this->config['domain_live_pic'], $this->config['space'], $name);
  140. return $url;
  141. }
  142. }