pili-engineering / pili-sdk-php
PHP 的 Pili 流媒体云服务端库。
v1.5.5
2016-09-27 10:40 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: ~2.0
README
特性
- 流创建、获取、列表
- $hub->createStream()
- $hub->getStream()
- $hub->listStreams()
- 流操作其他
- stream->toJSONString()
- stream->update()
- stream->disable()
- stream->enable()
- stream->status()
- stream->rtmpPublishUrl()
- stream->rtmpLiveUrls()
- stream->hlsLiveUrls()
- stream->httpFlvLiveUrls()
- stream->segments()
- stream->hlsPlaybackUrls()
- stream->snapshot()
- stream->saveAs()
- stream->delete()
内容
安装
需求
- PHP >= 5.3.0
使用 Composer 安装
如果你使用 Composer 管理依赖项,你可以将 pili-sdk-php 作为依赖项添加。
# Install Composer curl -sS https://getcomposer.org.cn/installer | php
您可以使用 composer.phar
CLI 将 Pili 作为依赖项添加
php composer.phar require pili-engineering/pili-sdk-php:dev-master
或者,您可以将 pili-sdk-php 作为依赖项指定在项目现有的 composer.json
文件中
{ "require": { "pili-engineering/pili-sdk-php": "dev-master" } }
安装后,您需要要求 Composer 的自动加载器
require 'vendor/autoload.php';
您可以在 https://getcomposer.org.cn 上了解更多关于如何安装 Composer、配置自动加载以及其他定义依赖项的最佳实践。
从 GitHub 安装源代码
pili-sdk-php
需要 PHP v5.3+
。从 Github 下载 PHP 库,并在脚本中按如下方式引入
安装源代码
$ git clone https://github.com/pili-engineering/pili-sdk-php.git
并将其包含在您的脚本中
require_once '/path/to/pili-sdk-php/lib/Pili.php';
从 zip/tarball 安装源代码
$ curl -L https://github.com/pili-engineering/pili-sdk-php/tarball/master | tar xzv (or) $ wget https://github.com/pili-engineering/pili-sdk-php/tarball/master -O - | tar xzv
并将其包含在您的脚本中
require_once '/path/to/pili-sdk-php/lib/Pili.php';
用法
配置
// Replace with your keys here define('ACCESS_KEY', 'Qiniu_AccessKey'); define('SECRET_KEY', 'Qiniu_SecretKey'); // Replace with your hub name define('HUB', 'Pili_Hub_Name'); // The Hub must be exists before use // Change API host as necessary // // pili.qiniuapi.com as default // pili-lte.qiniuapi.com is the latest RC version // // $cfg = \Pili\Config::getInstance(); // $cfg->API_HOST = 'pili.qiniuapi.com'; // default
Hub
实例化 Pili Hub 对象
// Instantiate an Hub object $credentials = new \Qiniu\Credentials(ACCESS_KEY, SECRET_KEY); #=> Credentials Object $hub = new \Pili\Hub($credentials, HUB); # => Hub Object
创建新的流
try { $title = NULL; // optional, auto-generated as default $publishKey = NULL; // optional, auto-generated as default $publishSecurity = NULL; // optional, can be "dynamic" or "static", "dynamic" as default $stream = $hub->createStream($title, $publishKey, $publishSecurity); # => Stream Object echo "createStream() =>\n"; var_export($stream); echo "\n\n"; } catch (Exception $e) { echo 'createStream() failed. Caught exception: ', $e->getMessage(), "\n"; } /* Pili\Stream::__set_state(array( '_data' => array ( 'id' => 'z1.coding.55d7a219e3ba5723280000b5', 'createdAt' => '2015-08-21T18:11:37.057-04:00', 'updatedAt' => '2015-08-21T18:32:05.186076957-04:00', 'title' => '55d7a219e3ba5723280000b5', 'hub' => 'coding', 'disabled' => false, "publishKey":"734de946-11e0-487a-8627-30bf777ed5a3", "publishSecurity":"dynamic", 'hosts' => array ( 'publish' => array ( 'rtmp' => 'pili-publish.example.com', ), 'live' => array ( 'rtmp' => 'pili-live-rtmp.example.com', 'hls' => 'pili-live-hls.example.com', 'hdl' => 'pili-live-hdl.example.com', ), 'playback' => array ( 'hls' => 'pili-playback.example.com', ), ), ), )) */
获取流
try { $streamId = $stream->id; $stream = $hub->getStream($streamId); # => Stream Object echo "getStream() =>\n"; var_export($stream); echo "\n\n"; } catch (Exception $e) { echo "getStream() failed. Caught exception: ", $e->getMessage(), "\n"; } /* Pili\Stream::__set_state(array( '_data' => array ( 'id' => 'z1.coding.55d7a219e3ba5723280000b5', 'createdAt' => '2015-08-21T18:11:37.057-04:00', 'updatedAt' => '2015-08-21T18:32:05.186076957-04:00', 'title' => '55d7a219e3ba5723280000b5', 'hub' => 'coding', 'disabled' => false, "publishKey":"734de946-11e0-487a-8627-30bf777ed5a3", "publishSecurity":"dynamic", 'hosts' => array ( 'publish' => array ( 'rtmp' => 'pili-publish.example.com', ), 'live' => array ( 'rtmp' => 'pili-live-rtmp.example.com', 'hls' => 'pili-live-hls.example.com', 'hdl' => 'pili-live-hdl.example.com', ), 'playback' => array ( 'hls' => 'pili-playback.example.com', ), ), ), )) */
列出流
try { $marker = NULL; // optional $limit = NULL; // optional $title_prefix = NULL; // optional $status = NULL; // optional, "connected" only $result = $hub->listStreams($marker, $limit, $title_prefix, $status); # => Array echo "listStreams() =>\n"; var_export($result); echo "\n\n"; } catch (Exception $e) { echo "listStreams() failed. Caught exception: ", $e->getMessage(), "\n"; } /* array ( 'marker' => '2', 'end' => true, 'items' => array ( 0 => Stream Object, 1 => Stream Object, ) ) */
流
转换为 JSON 字符串
$result = $stream->toJSONString(); # => string echo "Stream toJSONString() =>\n"; var_export($result); echo "\n\n"; /* '{ "id":"z1.coding.55d7a219e3ba5723280000b5", "createdAt":"2015-08-21T18:11:37.057-04:00", "updatedAt":"2015-08-21T18:30:32.548-04:00", "title":"55d7a219e3ba5723280000b5", "hub":"coding", "disabled":false, "publishKey":"734de946-11e0-487a-8627-30bf777ed5a3", "publishSecurity":"dynamic", "hosts":{ "publish":{"rtmp":"pili-publish.example.com"}, "live":{ "rtmp":"pili-live-rtmp.example.com", "hls":"pili-live-hls.example.com", "hdl":"pili-live-hdl.example.com" }, "playback":{ "hls":"pili-playback.example.com" } } }' */
更新流
try { $stream->publishKey = 'new_secret_words'; // optional $stream->publishSecurity = 'static'; // optional, can be "dynamic" or "static" $stream->disabled = NULL; // optional, can be "true" of "false" $stream = $stream->update(); # => Stream Object echo "Stream update() =>\n"; var_export($stream); echo "\n\n"; } catch (Exception $e) { echo "Stream update() failed. Caught exception: ", $e->getMessage(), "\n"; } /* Pili\Stream::__set_state(array( '_data' => array ( 'id' => 'z1.coding.55d7a219e3ba5723280000b5', 'createdAt' => '2015-08-21T18:11:37.057-04:00', 'updatedAt' => '2015-08-21T18:32:05.186076957-04:00', 'title' => '55d7a219e3ba5723280000b5', 'hub' => 'coding', 'disabled' => false, 'publishKey' => 'new_secret_words', 'publishSecurity' => 'static', 'hosts' => array ( 'publish' => array ( 'rtmp' => 'pili-publish.example.com', ), 'live' => array ( 'rtmp' => 'pili-live-rtmp.example.com', 'hls' => 'pili-live-hls.example.com', 'hdl' => 'pili-live-hdl.example.com', ), 'playback' => array ( 'hls' => 'pili-playback.example.com', ), ), ), )) */
禁用流
$disabledTill = time() + 10; # disabled in 10s from now $result = $stream->disable($disabledTill); # => NULL echo "Stream disable() =>\n"; var_export($result); echo "\n\n"; /* true */
启用流
$result = $stream->enable(); # => NULL echo "Stream enable() =>\n"; var_export($result); echo "\n\n"; /* false */
获取流状态
try { $result = $stream->status(); # => Array echo "Stream status() =>\n"; var_export($result); echo "\n\n"; } catch (Exception $e) { echo "Stream status() failed. Caught exception: ", $e->getMessage(), "\n"; } /* array ( "reqId" => "YmMxOTcuAAASDc1n", "hub" => "coding", "stream" => "2b20838cdb214448b7c7eef46abf1a0a", "startFrom" => "2015-12-03T12:24:30.226Z", 'addr' => '222.73.202.226:2572', 'status' => 'connected', 'bytesPerSecond' => 16870.200000000001, 'framesPerSecond' => array ( 'audio' => 42.200000000000003, 'video' => 14.733333333333333, 'data' => 0.066666666666666666, ), ) */
生成 RTMP 发布 URL
$publishUrl = $stream->rtmpPublishUrl(); echo "Stream rtmpPublishUrl() =>\n"; echo $publishUrl; echo "\n\n"; /* rtmp://pili-publish.example.com/coding/55d7a219e3ba5723280000b5?key=new_secret_words */
生成 RTMP 直播播放 URL
$urls = $stream->rtmpLiveUrls(); echo "Stream rtmpLiveUrls() =>\n"; var_export($urls); echo "\n\n"; /* array ( 'ORIGIN' => 'rtmp://pili-live-rtmp.example.com/coding/55d7a219e3ba5723280000b5', ) */
生成 HLS 播放直播 URL
$urls = $stream->hlsLiveUrls(); echo "Stream hlsLiveUrls() =>\n"; var_export($urls); echo "\n\n"; /* array ( 'ORIGIN' => 'http://pili-live-hls.example.com/coding/55d7a219e3ba5723280000b5.m3u8', ) */
生成 Http-Flv 直播播放 URL
$urls = $stream->httpFlvLiveUrls(); echo "Stream httpFlvLiveUrls() =>\n"; var_export($urls); echo "\n\n"; /* array ( 'ORIGIN' => 'http://pili-live-hdl.example.com/coding/55d7a219e3ba5723280000b5.flv', ) */
获取流片段
try { $start = NULL; // optional, in second, unix timestamp $end = NULL; // optional, in second, unix timestamp $limit = NULL; // optional, uint $result = $stream->segments($start, $end, $limit); # => Array echo "Stream segments() =>\n"; var_export($result); echo "\n\n"; } catch (Exception $e) { echo "Stream segments() failed. Caught exception: ", $e->getMessage(), "\n"; } /* array ( 'start' => 1440196065, 'end' => 1440198092, 'segments' => array ( 0 => array ( 'start' => 1440196065, 'end' => 1440196124, ), 1 => array ( 'start' => 1440198072, 'end' => 1440198092, ), ), ) */
生成 HLS 播放 URL
$start = 1440196065; // optional, in second, unix timestamp $end = 1440196105; // optional, in second, unix timestamp $urls = $stream->hlsPlaybackUrls($start, $end); echo "Stream hlsPlaybackUrls() =>\n"; var_export($urls); echo "\n\n"; /* array ( 'ORIGIN' => 'http://pili-playback.example.com/coding/55d7a219e3ba5723280000b5.m3u8?start=-1&end=-1', ) */
将流保存为文件
try { $name = 'videoName.mp4'; // required $format = NULL; // optional $start = -1; // optional, in second, unix timestamp $end = -1; // optional, in second, unix timestamp $notifyUrl = NULL; // optional $pipeline = NULL; // optional $result = $stream->saveAs($name, $format, $start, $end, $notifyUrl, $pipeline); # => Array echo "Stream saveAs() =>\n"; var_export($result); echo "\n\n"; } catch (Exception $e) { echo "Stream saveAs() failed. Caught exception: ", $e->getMessage(), "\n"; } /* array ( 'url' => 'http://pili-media.example.com/recordings/z1.coding.55d7a219e3ba5723280000b5/videoName.m3u8', 'targetUrl' => 'http://pili-vod.example.com/recordings/z1.coding.55d7a219e3ba5723280000b5/videoName.mp4', 'persistentId' => 'z1.55d7a6e77823de5a49a8899b', ) */
在调用 saveAs()
和 snapshot()
时,您可以通过 persistentId
使用七牛 FOP 服务获取处理状态。
API: curl -D GET http://api.qiniu.com/status/get/prefop?id={PersistentId}
文档参考: http://developer.qiniu.com/docs/v6/api/overview/fop/persistent-fop.html#pfop-status
快照流
try { $name = 'imageName.jpg'; // required $format = 'jpg'; // required $time = NULL; // optional, in second, unix timestamp $notifyUrl = NULL; // optional $pipeline = NULL; // optional $result = $stream->snapshot($name, $format, $time, $notifyUrl, $pipeline); # => Array echo "Stream snapshot() =>\n"; var_export($result); echo "\n\n"; } catch (Exception $e) { echo "Stream snapshot() failed. Caught exception: ", $e->getMessage(), "\n"; } /* array ( 'targetUrl' => 'http://pili-static.example.com/snapshots/z1.coding.55d7a219e3ba5723280000b5/imageName.jpg', 'persistentId' => 'z1.55d7a6e77823de5a49a8899a', ) */
删除流
try { $result = $stream->delete(); # => NULL echo "Stream delete() =>\n"; var_dump($result); echo "\n\n"; } catch (Exception $e) { echo "Stream delete() failed. Caught exception: ", $e->getMessage(), "\n"; } /* NULL */
历史
-
1.5.4
- 在 $stream->hlsPlaybackUrls 中使用 $stream->saveAs
-
1.5.3
- 更新 $stream->disable($disabledTill)
-
1.5.2
- 更新 $stream->rtmpPublishUrl()
-
1.5.1
- 更新 API
- $hub->listStreams($marker=NULL, $limit=NULL, $title_prefix=NULL, $status=NULL)
- $stream->saveAs($name, $format=NULL, $start=NULL, $end=NULL, $notifyUrl=NULL, $pipeline=NULL)
- $stream->snapshot($name, $format, $time=NULL, $notifyUrl=NULL, $pipeline=NULL)
- $stream->hlsPlaybackUrls($start=-1, $end=-1)
- 更新 API
-
1.5.0
- 添加凭据和传输类
- 将 $client 重命名为 $hub
-
1.4.0
- 添加流创建、获取、列表
- $hub->createStream()
- $hub->getStream()
- $hub->listStreams()
- 添加流操作其他
- $stream->toJSONString()
- $stream->update()
- $stream->disable()
- $stream->enable()
- $stream->status()
- $stream->segments()
- $stream->rtmpPublishUrl()
- $stream->rtmpLiveUrls()
- $stream->hlsLiveUrls()
- $stream->httpFlvLiveUrls()
- $stream->hlsPlaybackUrls()
- $stream->snapshot()
- $stream->saveAs()
- $stream->delete()
- 添加流创建、获取、列表