Skip to content

Commit 9f5525c

Browse files
committed
feat: 添加同步音视频处理 api
1 parent e39da68 commit 9f5525c

File tree

5 files changed

+166
-1
lines changed

5 files changed

+166
-1
lines changed

src/Upyun/Api/Pretreat.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Upyun\Api;
33

4-
use GuzzleHttp\Psr7;
54
use GuzzleHttp\Client;
65
use Upyun\Config;
76
use Upyun\Signature;

src/Upyun/Api/SyncVideo.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* 同步视频处理
4+
*/
5+
6+
namespace Upyun\Api;
7+
8+
use GuzzleHttp\Client;
9+
use Upyun\Config;
10+
use Upyun\Signature;
11+
12+
13+
class SyncVideo {
14+
/**
15+
* @var Config
16+
*/
17+
protected $config;
18+
19+
public function __construct(Config $config)
20+
{
21+
$this->config = $config;
22+
}
23+
24+
public function process($params, $path) {
25+
$client = new Client([
26+
'timeout' => $this->config->timeout,
27+
]);
28+
29+
$path = '/' . $this->config->serviceName . $path;
30+
$method = 'POST';
31+
$signedHeaders = Signature::getHeaderSign($this->config, $method, $path);
32+
33+
$url = $this->config->getSyncVideoEndPoint() . $path;
34+
$response = $client->request($method, $url, [
35+
'headers' => $signedHeaders,
36+
'json' => $params
37+
]);
38+
39+
$body = $response->getBody()->getContents();
40+
return json_decode($body, true);
41+
}
42+
}

src/Upyun/Config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ class Config
9191
*/
9292
const ED_PURGE = 'http://purge.upyun.com/purge/';
9393

94+
/**
95+
* 同步视频处理接口地址
96+
*/
97+
const ED_SYNC_VIDEO = 'p1.api.upyun.com';
98+
9499
public function __construct($serviceName, $operatorName, $operatorPassword)
95100
{
96101
$this->serviceName = $serviceName;
@@ -130,6 +135,11 @@ public function getPretreatEndPoint()
130135
return $this->getProtocol() . self::ED_VIDEO;
131136
}
132137

138+
public function getSyncVideoEndPoint()
139+
{
140+
return $this->getProtocol() . self::ED_SYNC_VIDEO;
141+
}
142+
133143
public function getProtocol()
134144
{
135145
return $this->useSsl ? 'https://' : 'http://';

src/Upyun/Upyun.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,101 @@ public function queryProcessResult($taskIds)
438438
$video = new Api\Pretreat($this->config);
439439
return $video->query($taskIds, '/result/');
440440
}
441+
442+
/**
443+
* 多个 m3u8 文件拼接
444+
* @param array $files 保存在又拍云云存储中的多个 m3u8 文件路径
445+
* @param string $saveAs 拼接生成的新 m3u8 文件保存路径
446+
*
447+
* @return array 见 [m3u8 拼接 - 响应](http://docs.upyun.com/cloud/sync_video/#_3)
448+
*/
449+
public function m3u8Concat($files, $saveAs)
450+
{
451+
$p = new Api\SyncVideo($this->config);
452+
return $p->process([
453+
'm3u8s' => $files,
454+
'save_as' => $saveAs,
455+
], '/m3u8er/concat');
456+
}
457+
458+
/**
459+
* 单个 m3u8 文件剪辑
460+
* @param string $file 需要剪辑的又拍云云存储中的 m3u8 文件路径
461+
* @param string $saveAs 剪辑完成后新的 m3u8 文件保存路径
462+
* @param array $slice 需要被保留或删除的片段。
463+
* @param bool $$isInclude 默认为 `true` 表示 `$slice` 参数描述的片段被保留,否则表示 `$slice` 参数描述的片段被删除
464+
* @param bool $index 指定 `$slice` 参数的格式,默认为 `false` 表示使用时间范围描述片段,单位秒:`[<开始时间>, <结束时间>]`;`true` 表示使用 `m3u8` 文件的分片序号,从 0 开始,这种方式可以一次对多个片段操作
465+
*
466+
* @return array 见 [m3u8 剪辑 - 响应](http://docs.upyun.com/cloud/sync_video/#_6)
467+
*/
468+
public function m3u8Clip($file, $saveAs, $slice = array(), $isInclude = true, $index = false)
469+
{
470+
$p = new Api\SyncVideo($this->config);
471+
$params = [
472+
'm3u8' => $file,
473+
'save_as' => $saveAs,
474+
'index' => $index,
475+
];
476+
if ($$isInclude) {
477+
$params['include'] = $slice;
478+
} else {
479+
$params['exclude'] = $slice;
480+
}
481+
return $p->process($params, '/m3u8er/clip');
482+
}
483+
484+
/**
485+
* 获取单个 m3u8 文件描述信息
486+
* @param string $file 又拍云云存储的中的 m3u8 文件路径
487+
*
488+
* @return array 见 [获取 m3u8 信息 - 响应](http://docs.upyun.com/cloud/sync_video/#_6)
489+
*/
490+
public function m3u8Meta($file)
491+
{
492+
$p = new Api\SyncVideo($this->config);
493+
return $p->process([
494+
'm3u8' => $file,
495+
], '/m3u8er/get_meta');
496+
}
497+
498+
/**
499+
* 视频截图,可以对 mp4、m3u8 等视频文件进行截图
500+
* @param string $file 需要截图的又拍云云存储中的视频文件路径
501+
* @param string $saveAs 截图保存路径
502+
* @param string $point 截图时间点,`HH:MM:SS` 格式
503+
* @param string $size 截图尺寸 `宽x高` 格式的字符串。默认和视频尺寸一致
504+
* @param string $format 截图保存的格式,默认根据 `$saveAs` 参数的后缀生成,可以指定 `jpg | png | webp` 三种格式
505+
*
506+
* @return array 见 [视频截图 - 响应](http://docs.upyun.com/cloud/sync_video/#m3u8_2)
507+
*/
508+
public function snapshot($file, $saveAs, $point, $size = '', $format = '')
509+
{
510+
$p = new Api\SyncVideo($this->config);
511+
$params = [
512+
'source' => $file,
513+
'save_as' => $saveAs,
514+
'point' => $point,
515+
];
516+
if ($size) {
517+
$params['size'] = $size;
518+
}
519+
if ($format) {
520+
$params['format'] = $format;
521+
}
522+
return $p->process($params, '/snapshot');
523+
}
524+
525+
/**
526+
* 获取音视频文件元信息
527+
* @param string $file 又拍云云存储的中的音视频文件路径
528+
*
529+
* @return array 见 [获取音视频文件信息 - 响应](http://docs.upyun.com/cloud/sync_video/#_16)
530+
*/
531+
public function avMeta($file)
532+
{
533+
$p = new Api\SyncVideo($this->config);
534+
return $p->process([
535+
'source' => $file,
536+
], '/avmeta/get_meta');
537+
}
441538
}

tests/UpyunTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,21 @@ public function testQueryProcessResult()
252252
$this->assertTrue($result[self::$taskId]['path'][0] === '/video/result.mp4');
253253
$this->assertTrue($result[self::$taskId]['status_code'] === 200);
254254
}
255+
256+
public function testAvMeta()
257+
{
258+
$source = 'php-sdk-sample.mp4';
259+
self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
260+
$result = self::$upyun->avMeta('/php-sdk-sample.mp4');
261+
$this->assertTrue(count($result) === 2);
262+
$this->assertTrue($result['streams'][0]['type'] === 'video');
263+
}
264+
265+
public function testSnapshot()
266+
{
267+
$source = 'php-sdk-sample.mp4';
268+
self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r'));
269+
$result = self::$upyun->snapshot('/php-sdk-sample.mp4', '/snapshot.jpg', '00:00:01', '720x480', 'jpg');
270+
$this->assertTrue($result['status_code'] === 200);
271+
}
255272
}

0 commit comments

Comments
 (0)